How to trap date field entry error

lana

Registered User.
Local time
Today, 22:36
Joined
Feb 10, 2010
Messages
92
Hi there,
I need to trap the date field entry errors on my forms.
The date format is mm/dd/yyyy
If it is entered as 08/20/2021, everything is OK, but if it is changed to 13/20/2021, I could not trap the error because the field value is still the old one (08/20/21).
I need to trap the error and show a message that the month can not be more than 12, or ....

Any ideas?

Thanks so much.
 
Just set the format to Short Date and let Access handle it?
 
can you use the BeforeUpdate event of the date textbox?

private sub dte_BeforeUpdate(Cancel As Integer)
dim sDate As String
sDate = Me!dte & ""
Cancel = (IsDate(sDate)=False)
If Cancel Then
Msgbox date must be in the format 'm/d/yyyy'"
End If
end sub
 
Just set the format to Short Date and let Access handle it?
I need to show my own messages, If possible!
can you use the BeforeUpdate event of the date textbox?
Before the beforeUpdate event fires, the error message appears.

I trap the error in the OnError event but the value of the text box is still the old one because it is not updated.

Thanks
 
after the message appears, you Undo the control.

private sub dte_BeforeUpdate(Cancel As Integer)
dim sDate As String
sDate = Me!dte & ""
Cancel = (IsDate(sDate)=False)
If Cancel Then
Msgbox date must be in the format 'm/d/yyyy'"
Me!dte.Undo
End If
end sub
 
It does not work.
When the date is entered incorrectly, the _onError event fires, the _BeforeUpdate event does not fire.
When I look at the field value in the OnError event, it's null, or the old value, not the incorrect entry.
Thanks again for your time.
 
In that context, look at the control-name.text property rather than the control-name.value, because that error occurred before the update went through. I.e. if you have a form On_Error event, it fires before any update-related event. And the .Value is not updated in that case.
 
Hi Lana, I think that you may need to change the Short Date Format in Windows. Access should then follow that.

Since the year 2000 it has been easier to use dates with a four digit year. I know it wastes screen space but it
avoids having to tell clients how, or worse, explaining step by step about making the change in Windows.
Before that everyone used the two digit year. Then from 2000 with a two digit year it wouldn't be possible to
know the difference between 1910 and 2010. As far as I know Access will store the date as YYYY regardless
of the Windows setting, but you may want to test it.

Some software from major companies only allow a four digit year, so it is unclear if changing the short date
in Windows to MMDDYYYY would cause issues in other software.
 
Does this mean you're not using the date picker? Just wondering...
 
Thank you all.
control-name.text worked like a charm.

Many thanks again.
 

Users who are viewing this thread

Back
Top Bottom