Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do..while loop in VB

Status
Not open for further replies.

sonikudi

Technical User
Sep 9, 2007
81
US
hey guys,

I would like to create a loop where, if the user does't enter the right format i want to display a message showing the date format and would like to give user a chance to re-enter the dates..The code i wrote is as follows:

Code:
Do

MsgBox "Please enter the date format as MM/DD/YY, in both boxes"

Loop While (Format(Me.txtStartDate <> "MMDDYY") Or Format(Me.txtEndDate <> "MMDDYY"))

This code doesn't allow the user to re-enter the information it jus loops..
How do i give user the control to allow them to re-enter the dates..and then check again to make sure the dates were right, also is the Do..while loop the best way to do this?

Thanks for any help in advance,
 
Hi...

Possible choices:

1.In the text box label, instruct the format you wish...
Start Date (MM/DD/YY)

2.Use the Input Mask Property of the TextBox to set an Input Format 99/99/99;0

3. Ultimately, don't let the user type the date in, use a Calendar Control to pick the date you want entered.

me.textbox = format(me.calendar.value,"mm/dd/yy")
 
This is not the sort of thing to put in a loop. Validation is best performed in an event, most often the BeforeUpdate or BeforeInsert events.


 



Hi,

Your editing has inherent flaws, let along the loop.

You are asking the user to enter data in the form mm/dd/yy, which you intend to be a date.

Suppose the user intends to enat the string for jan 12, 2004, and enters 12/01/04 and you interpret it as dec 1 2004.

The best you can do with a single textbox, is, check to see if the string converts to a date.

A better way is to capture the DAY, MONTH and YEAR in separate boxes.


Skip,

[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue]
 
What I usually do is let the user enter in any format they want, as long as it passes as a date. But then I change the display format so that the month is not a number, usually I use dd-mmm-yy format (e.g. 12-Jan-07). That way they see what my program is going to interpret the date as.

I also almost always give the option of using a pop-up calendar control.


 
Hey ..

Thanks everyone! ...the suggestions were helpful!
 

I agree with Lewds! What I do for this type of thing is to place a calendar control on the form positioned as you like and set it's Visible Proprty to NO.

Then, using the Double Click property of your text box, have the calendar "popup" for date selection. You'll need to replace YourCalendarName and YourTextBoxName with the actual names of your calendar and text box.

Code:
Private Sub Form_Load()
  YourCalendarName.Value = Date
End Sub

Private Sub YourCalendarName_Click()
  YourTextBoxName.Value = YourCalendarName.Value  
  YourTextBoxName.SetFocus
  YourCalendarName.Visible = False
End Sub

Private Sub YourTextBoxName_DblClick(Cancel As Integer)
  YourCalendarName.Visible = True
End Sub

Now, all your user has to do is DoubleClick on the text box and up pops the calendar! When the user clicks on the date, the calendar disappears and the text box is populated with the date.

Because of the problems associated with ActiveX controls, in moving from one version of Access to another, I'd use one of the free 3rd party calendars that are available.

Using a calendar eliminates all the hassles often associated with entering dates! You know you have a valid date entered, in the correct format, ready to be used by Access!

Linq


The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top