On Exit question

G

Guest

Hello,

I have a Text Box called txtDate & I want to validate if the user enters in
a valid date & if not to display a msgbox saying "Invalid Date" & keep the
focus until a valid date is entered.

I put the following code which works....

Private Sub txtDate_Exit(Cancel As Integer)
If Not IsDate(txtDate) Then
MsgBox "Invalid Date", vbCritical
Cancel = True
End If
End Sub

However,

If the user enters an invalid date & then clicks the X on the form to close
out the form the msgbox then displays twice & then the Form closes. For some
reason the On Exit Event is getting triggered twice. Is their a way to stop
this from happeneing ? Or idealy for the On Exit Event to not be triggered
at all when the user only closes out the Form ?

I know one way is to use the ValidationRule & ValidationText properties but
I would rather not because you then cant control the way the msgbox looks, as
an example you cant have the vbCritical.

So is their another way ?

Any help would be greatly appreciated.

Thank you,
Jeff
 
A

Allen Browne

If txtDate is bound to a field, Access will validate it automatically.

If it is unbound, set the Format property of the text box to Short Date or
similar, and Access will still validate it.

If you do need to validate it yourself, use the Before Update event of the
text box. Set Cancel to True, and the user will not be allowed out of the
box before the entry is satisfactory.

The message about the invalid entry will still occur if the form is being
closed. It is Access' way of giving the user the choice to close anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top