Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using cdate with bad date 1

Status
Not open for further replies.

imarosel

Technical User
Jun 23, 2005
149
US
Probably an error trapping question, something I am weak on. I have an input box for a string that I take and then turn into a date with cdate. If the user submits a string that cannot be turned into a date the code stops and returns "runtime error 13 type mismatch." I'd rather the code keep running and throw up a custom error message.

Private Sub Command3_Click()

Dim datestring As String
Dim datedate As Date

datestring = InputBox("What day? MM/DD/YYYY", "Date")

If Not IsNull(datestring ) And datestring <> "" Then

datedate = CDate(datestring)
MsgBox datedate

Else
MsgBox "empty"
End If
 
Replace this:
If Not IsNull(datestring ) And datestring <> "" Then
with this:
If IsDate(datestring) Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
And IsDate(datestring)


Note that depending on how your users enter the date field its possible to get a valid date, although not the date they are expecting.

e.g. 12/13/2006 - is a mistake from your user according to the sugested format, but it will be accepted as being 13 december 2006

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Thanks PHV, so many functions avaliable to use that I don't know about, so useful it's annoying.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top