Handle Invalid Dates

Ever run into a problem where a data would be entered into a form field, passed over to an SQL Query to be executed and get the following error:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][SQL Driver] Syntax error in date in query expression

I started looking around to see why I was getting this error, when I happened to notice the date range I was inputting into the form fields for a Start Date was 02/01/99 and had an Ending Date of 02/30/99. Well once I saw the problem I had to come up with a way to handle the problem. What was the problem, well there is no date of 02/30/99 and SQL could not handle the expression and error out. The following is the code I used to correct the problem, the file is called process_selections.asp and handles lot's of other items passed to it from the form's page.

Date1 = Request("Start_Date")
Date2 = Request("Stop_Date")
DateCheck1 = IsDate(Date1)
DateCheck2 = IsDate(Date2)

If DateCheck1 = "False" Then
 Response.Redirect "bad_date.asp"
End If
If DateCheck2 = "False" Then
 Response.Redirect "bad_date.asp"
End If

What this does is the following:

  1. Sets Date1 & Date2 to the Dates sent across from the forms page..
  2. Sets DateCheck1 and DateCheck2 to either True or False using the IsDate() Function
  3. Performs an If..Then Statement which if the value of DateCheck1 or DateCheck2 is False it redirects to a page called bad_date.asp which tells the user that a date they entered in was wrong.
This example is over exaggerated for demonstation purposes, streamlined code is always better in production!

asp invalid dates

Back To Top
© 1998 - 2017 psacake.com | My3C's

Version 7.2 | Advertise on this site