Check for valid date on input box

G

Guest

Acc03/WinXp

I am using an input box for a user to enter in a date. The variable is
defined as a date field, but I'm guessing that InputBox automatically reads
as string. I would like to verify that the information entered by the user
is a valid date.

I understand that if I do the following....

InvDate = CDate(InputBox("Please enter a date. Use mm/dd/yy format."))

....and the user inputs an invalid date, an Error 13: Type Mismatch occurs.

Is there a way to trap for that error and return to the input box to have
the user try again? Or do I create a function to check the entry and run a
simple loop on the input box until a valid date is entered (or the user hits
cancel)?

Thanks!
 
R

Rick Brandt

Pendragon said:
Acc03/WinXp

I am using an input box for a user to enter in a date. The variable
is defined as a date field, but I'm guessing that InputBox
automatically reads as string. I would like to verify that the
information entered by the user is a valid date.

I understand that if I do the following....

InvDate = CDate(InputBox("Please enter a date. Use mm/dd/yy format."))

...and the user inputs an invalid date, an Error 13: Type Mismatch
occurs.

Is there a way to trap for that error and return to the input box to
have the user try again? Or do I create a function to check the
entry and run a simple loop on the input box until a valid date is
entered (or the user hits cancel)?

Thanks!

Use the IsDate() function and only use CDate() after IsDate() indicates that
what they entered can in fact be interpretted as a date.
 
G

Guest

Here is a way to do that:

Dim strCheckDate As String
Dim dtmGoodDate As Date

Do While True
strCheckDate = InputBox("Enter Date")
If IsDate(strCheckDAte) Then
dtmGoodDate = CDate(strCheckDAte)
Exit Do
Else
If MsgBox("Bad Date - Re Enter", vbQuestion + vbRetryCancel) =
vbCancel Then
Exit Do
End if
End If
Loop
 

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

Top