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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If...Then statement 1

Status
Not open for further replies.

snub

IS-IT--Management
Sep 21, 2005
20
0
0
US
I am setting up a form and I want to be able to display a message box and have a certain control receive focus if an incorrect date is entered. For example: If a date in the future is entered I want my message box to come up and state that the date is incorrect and then have that field receive the focus until it is an acceptable date. I used a macro to make the message box happen but not the focus issue, shouldn't an If...Then statement work more efficiently?
 
In the BeforeUpdate event procedure of the date control:
If Not IsDate([date control]) Or [date control] > Date Then
MsgBox "Invalid date, please retry"
Cancel = True
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
When you mean control, I'm assuming that you mean the textbox where the date has been entered, if so try:


Code:
If Format(DateField, "mm/dd/yyyy") > Format(Now(), "mm/dd/yyyy") Then
   MsgBox "You have entered an incorrect date", vbOKOnly, "Incorrect Date"
    DateField.SetFocus
End If
 
ter79, what happens if I enter 01/01/2029 ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV, I'll go back and try it again, I was putting it in the On Exit section of the event, I'll try it in the BeforeUpdate. How do I get it to go back to the same control field until an acceptable date is entered, or will the incorrect date force it back automatically?
 
The magic is here: Cancel = True

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,

I didn't think about that, so disregard my suggestion
 
Thanks PHV,
I used the code and it worked perfectly, I also added to it so that if the date that was entered went too far in the past it gave another error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top