Comparing dates in VBA

trigirl67

Registered User.
Local time
Yesterday, 17:34
Joined
Dec 10, 2011
Messages
10
I am trying to compare 2 dates, see if one is less than the other, then do something.

If [Min Request Date] < Now Then

Me.[Request Type] = "New"
Else
Me.[Request Type] = "Old"
End If

BUT I always get "Old" no matter what the min Request Date is. Does the Min Request Date need to be formated somehow.
 
if the [Min Request Date] is a date field and your code is in the after update of the Min Request Date ] i did a test and made my date field a short date at table level and it worked fine .
 
The only way I can reproduce this is with [Min Request Date] defined as Text, rather than as a Date/Time Field. You'll need to go into your Table, if this is a Bound field, or into the Format Property of the Textbox, if it is Unbound, and change it to a DateTime Datatype or one of the Date Formats.

You also need to change Now to Date. Now(), which includes a Time component, should only be used when the Time as well as the Date is pertinent. Using Now(), as you are doing here, can lead to unexpected results, because if you enter only the Date component into a DateTime Field, Access automatically fills in Midnight in the Time component.

If you, for instance, had a Field named

MyDate

and its Value was

6/16/13

and you executed this code, today, 6/16/13 at 22:00

MyDate = Now

it would evaluate as False, because Access would add the Time of 1200 to MyDate, so you end up with

6/16/13 12:00 = 6/16/13 22:00

even though the Date itself is the same/equal.

Linq ;0)>
 

Users who are viewing this thread

Back
Top Bottom