error comparing dates

G

Guest

Hi,
Error when comparing a range of dates, returns false instead of true.

code:
dim hrMax as date
Do While CDate(lstMax.List(lstMax.ListCount - 1)) < hrMax
.....
loop

explanation:
lstMax (listbox control) has an array of of times ("11:00 AM", "12:00 AM",
"1:00 PM", etc), in the loop, the camparison goes like this:

Do While CDate("10:00 AM") < "10:00:00 AM" -- result: true
Do While CDate("11:00 AM") < "10:00:00 AM" -- result: false
Do While CDate("12:00 AM") < "10:00:00 AM" -- result: false
Do While CDate("1:00 PM") < "10:00:00 AM" -- result: false

why does the 1st returns true?.
when I debug step by step, I can see that the it should return false

This file manages data files, and this error is only present on very few files

thanks for your help
 
M

Myrna Larson

You are comparing a number (the result of CDate is a number between 0 and
0.99999999) with a text string and relying on VBA to properly interpret the
latter as a time and convert it to a time. IMO, that's risky business. I like
to be in control as much as possible, so I always convert the two variables to
the same type. IOW, if you convert one to a date, you should do the same with
the 2nd.

That said, in the immediate window I executed the following statement: the
result was False, not True

? cdate("10:00 AM") < "10:00:00 AM"
False

Note that

? cDate("10:00 AM") < CDate("10:00:00 AM")

also prints False
 

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

Similar Threads


Top