compare date in vba (1 Viewer)

dai_lo

Registered User.
Local time
Yesterday, 21:39
Joined
Jan 10, 2008
Messages
30
Hi,

I would like to compare an input date and current date in a IF statement

I have no idea why the following code doesn't work properly

If Format(Itm_Date_of_Shipment, "mm/dd/yyyy") >= format(now(), "mm/dd/yyyy") then

' update the table...

else

' display error message

end if

please advise.

thanks

ALice
 

chergh

blah
Local time
Today, 05:39
Joined
Jun 15, 2004
Messages
1,414
try:

Code:
If clng(Itm_Date_of_Shipment) >= clng(date())
 

Moniker

VBA Pro
Local time
Yesterday, 23:39
Joined
Dec 21, 2006
Messages
1,567
Also, look into the DateDiff function. (Look in Access Help.) It does all the math for you.
 

dai_lo

Registered User.
Local time
Yesterday, 21:39
Joined
Jan 10, 2008
Messages
30
DateDiff function does the work!

thanks
 

RoyVidar

Registered User.
Local time
Today, 06:39
Joined
Sep 25, 2000
Messages
805
The reason why the original statement didn't work as expected, is that through the Format statement, you converted two valid dates to strings, then you compared those two strings (mm/dd/yyyy) -> "12/01/2007" > "01/11/2008"

If the Itm_Date_of_Shipment is a variable of datatype Date/Time, then there's nothing wrong with doing date comparisons.

If Itm_Date_of_Shipment >= Date Then

Note usage of Date (gives todays date) vs Now (gives current Date and Time)
 

Users who are viewing this thread

Top Bottom