Using less than a date in Code

P

Pamela

To prevent date mistyping of the new year, I'd like code to run on the
AfterUpdate event of my DateInspected control if the user accidentally inputs
a date before 2/28/09 (for instance, 1/4/09 - when it probably should be
1/4/10) My table is storing the date in 2/28/09 format. Here is my code:
Private Sub DateInspected_AfterUpdate()
If Me.DateInspected < 2 / 28 / 2009 Then
MsgBox "Please double check the date", vbOKOnly, "Attention"
End If
End Sub
Thanks so much for your help!
Pamela
 
D

Dorian

You need
If Me.DateInspected < #2/28/2009#

You always need the #...# delimiters for a date literal.
However, it's not a good idea to make this a fixed date. I'd research the
DateDiff function in Access Help.
-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and they
eat for a lifetime".
 
J

John W. Vinson

To prevent date mistyping of the new year, I'd like code to run on the
AfterUpdate event of my DateInspected control if the user accidentally inputs
a date before 2/28/09 (for instance, 1/4/09 - when it probably should be
1/4/10) My table is storing the date in 2/28/09 format. Here is my code:
Private Sub DateInspected_AfterUpdate()
If Me.DateInspected < 2 / 28 / 2009 Then
MsgBox "Please double check the date", vbOKOnly, "Attention"
End If
End Sub
Thanks so much for your help!
Pamela

How did you decide on 2/28/09? Will that same date apply in January 2013
(assuming that the Mayan calendar doesn't make the question irrelevant)?

Note that dates in Access are NOT stored as text strings. A date is actually
stored internally as a Double Float number, a count of days and fractions of a
day (times) since midnight, December 30, 1899:

?Now(); CDbl(Now())
1/4/2010 10:35:23 AM 40182.4412384259

I would suggest instead that you check to see if the date if earlier than
(say) ten months in the past:

If Me.DateInspected < DateAdd("m", -10, Date()) Then
MsgBox...

You can use the DateAdd, DateDiff, DateSerial and other Date functions to do
all sorts of checking; see the VBA help on these functions for details.
 

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