Checking Dates

Sigma248

Registered User.
Local time
Today, 03:10
Joined
Oct 6, 2008
Messages
24
I am having a problem with this line of code:

If (rstDates!fldValidFromDate <= Format(Now(), "mm/dd/yyyy")) And (rstDates!fldValidToDate >= Format(Now(), "mm/dd/yyyy")) Then

It works most of the time but not when one of the fldValid dates is exactly equal to Format(Now(), "mm/dd/yyyy"). It as if the <= and >= are not working properly.

Any suggestions?
 
Note that the Format() Function returns a string, not a date. To do a reliable date comparison use two dates.
Try the Date() function, which returns the system date...
Code:
If (rst!FromDate <= Date()) And (rst!ToDate >= Date()) Then
You can also use CDate() to convert something to a date.
Code:
Debug.Print TypeName(CDate("1/1/09 11:37p"))
If you still get unexpected results then your table may contain dates that include times. In this case use the DateValue() function, which returns only the date portion of a date variable and truncates the time...
Code:
If DateValue(rst!SomeDateAndTime) = Date() Then
 

Users who are viewing this thread

Back
Top Bottom