G
Gordon Bentley-Mix at news.microsoft.com
Many times in a UserForm I find I have to validate a date value entered into
a TextBox control to make sure it is either before (or on) or after (or on) a
certain date - usually the current date. I usually do this by using CDate to
convert the value entered in the TextBox to a date and then comparing it to
the value of 'Now'. However, because 'Now' includes the time, I sometimes get
"false negatives" - especially if the value has to be _on or after_ the
current date - because CDate converts the TextBox value to '[date] 00:00:00'
while 'Now' converts to '[current date] [current time]'.
For example, as I write this, it is 12:47 pm on 9 March 2009. If I enter a
value of "9 March 2009" in the TextBox and I want to ensure that the value is
on or after the current date, the comparison returns false because CDate
converts the value to '9 March 2009 00:00:00', is _before_ 9 March 2009
12:47:ss.
To get around this, I've tried adding or subtracting half a day (or even a
full day) to 'Now', but I've found this to be a bit risky and subject to
failure under certain conditions. (Exactly what the conditions are that cause
the problem is immaterial, but it definitely doesn't always work reliably.)
Consequently, I've resorted to using a construction like:
If CDate(txtMyDate.Value) > CDate(Format(Now, "d mm yy")) Then
'Throw an error
End If
In this construction, if I enter "9 March 2009" in 'txtMyDate', CDate
returns "9 March 2009 00:00:00" for the value from the TextBox and "9 March
2009 00:00:00" for the formatted value of 'Now', and the validation passes.
However, this seems a bit clunky, and I'm wondering if there's an easier way
to do this comparison without resorting to the Format function to strip out
the time part of 'Now'. Suggestions?
--
Cheers!
Gordon Bentley-Mix
Word MVP
Please post all follow-ups to the newsgroup.
Read the original version of this post in the Office Discussion Groups - no
membership required!
a TextBox control to make sure it is either before (or on) or after (or on) a
certain date - usually the current date. I usually do this by using CDate to
convert the value entered in the TextBox to a date and then comparing it to
the value of 'Now'. However, because 'Now' includes the time, I sometimes get
"false negatives" - especially if the value has to be _on or after_ the
current date - because CDate converts the TextBox value to '[date] 00:00:00'
while 'Now' converts to '[current date] [current time]'.
For example, as I write this, it is 12:47 pm on 9 March 2009. If I enter a
value of "9 March 2009" in the TextBox and I want to ensure that the value is
on or after the current date, the comparison returns false because CDate
converts the value to '9 March 2009 00:00:00', is _before_ 9 March 2009
12:47:ss.
To get around this, I've tried adding or subtracting half a day (or even a
full day) to 'Now', but I've found this to be a bit risky and subject to
failure under certain conditions. (Exactly what the conditions are that cause
the problem is immaterial, but it definitely doesn't always work reliably.)
Consequently, I've resorted to using a construction like:
If CDate(txtMyDate.Value) > CDate(Format(Now, "d mm yy")) Then
'Throw an error
End If
In this construction, if I enter "9 March 2009" in 'txtMyDate', CDate
returns "9 March 2009 00:00:00" for the value from the TextBox and "9 March
2009 00:00:00" for the formatted value of 'Now', and the validation passes.
However, this seems a bit clunky, and I'm wondering if there's an easier way
to do this comparison without resorting to the Format function to strip out
the time part of 'Now'. Suggestions?
--
Cheers!
Gordon Bentley-Mix
Word MVP
Please post all follow-ups to the newsgroup.
Read the original version of this post in the Office Discussion Groups - no
membership required!