validating a date

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What is the best way to validate a date? I want the user to type in
mm/dd/yyyy without any letters and only use the slashes. I tried inStr and
that wasn't working to well and I have a for loop but I'm not sure on syntax
to see first off if every character is a number and to make sure the slashes
are in there.
Thanks.
 
I implement an input mask. The IM will force the user to enter a valid
date. If you're capturing the value from some other means (not
recommend) such as InputBox() or a parameter popup window (VERY not
recommened), then use IsDate() to the value
 
bball said:
What is the best way to validate a date? I want the user to type in
mm/dd/yyyy without any letters and only use the slashes. I tried inStr and
that wasn't working to well and I have a for loop but I'm not sure on syntax
to see first off if every character is a number and to make sure the slashes
are in there.


David answered the question you asked.

I have to ask Why? If you used a Date/Time field, without a
mask, you would neither know nor care what the user typed as
long as it's a legal date (in any of the many formats that
Access accepts). The text box would display the date/time
back to the user however you specified in the Format
property.

For a Text field, you can use a little VBA code in the text
box's BeforeUpdate event procedure to check it using the
IsDate function. Then use the CDate function to convert the
text string to a date/time value, which can in turn be
displayed (whenever you need to see the mm/dd//yyyy style)
by specifying the format property or function.
 
Thanks for the responses but I'm curious because i use the CDate function
which works great but if the user types in 071805 it will not convert it to
07/18/05 it makes it like 11/02/2066 or something totally wrong. Is there a
reason why?

Thanks.
 
Never mind. The input mask worked good. I didn't know of that feature, I'm
new to vb in access.
Thanks.
 
Access stores dates as 8 byte floating point numbers, where the integer
portion is the number of days relative to 30 Dec, 1899, and the decimal
portion is the time as a fraction of a day. CDate is treating 071805 as a
number, so it should be returning 03 Aug, 2096, which is 71805 days after 30
Dec, 1899. CDate needs to know that you're trying to pass it a date: you
need the slashes (or other delimiters).

CDate(Format$(071805, "00/00/00")) will work.
 
Back
Top