Marshall Barton said:
Douglas J. Steele wrote:
[]
dtmDate = DateSerial(intYear, intMonth, intDay)
ValidDate = (Err.Number = 0)
"Marshall Barton" wrote
Careful Doug. It's kinda difficult to get an error out of
DateSerial:
?DateSerial(99,99,99)
6/7/107
Douglas said:
Good point, Marshall.
In actual fact, I'd intended to write logic that checked If intMonth > 0 And
intMonth < 13 Then, but I decided it was going to take too long to type in
the code to validate the value for intDay (I couldn't find an
already-written version in the sample database I use while posting)
Guess I shouldn't have bothered responding if I was going to be so lazy.
OTOH, I'm not sure you can rely on IsDate(intmonth & "/" & intDay & "/" &
intYear) either. If intmonth is greater than 12, it'll still pass if intDay
is less than 13. (In other words, it'll try dd/mm/yy if it's not valid as
mm/dd/yy, and say it's good if it's okay in that format)
Another good point, Doug.
I'm too lazy to write the day verification code too ;-)
Scratching head now . . . {light bulb over head}
How about this:
IsDate(Format(intYear, "\2\000") & "-" & intMonth & "-" &
intDay)
That'll probably work. I was going to complain that it wouldn't work for
past dates (such as birthdays) because of forcing the year to 20xx, but
since the intent is validation only, as opposed to actual conversion, it's
probably okay. Of course, it'll wrongly indicate that 29 Feb, 1900 is
valid...
OTOH, the day validation isn't that bad:
Select Case intMonth
Case 1, 3, 5, 7, 8, 10, 12
If intDay < 1 Or intDay > 31 Then
' Invalid
End If
Case 2
If intYear Mod 4 = 0 Then
If (intYear Mod 400 = 0) Or (intYear Mod 100 <> 0) Then
If intDay < 1 Or intDay > 29 Then
' Invalid
End If
Else
If intDay < 1 Or intDay > 28 Then
' Invalid
End If
End If
Else
If intDay < 1 Or intDay > 28 Then
' Invalid
End If
End If
Case 4, 6, 9, 11
If intDay < 1 Or intDay > 30 Then
' Invalid
End If
Case Else
' Invalid
End Select
I've put the full logic for leap years there. Since the OP indicated only 2
digit year, it's probably not correct.