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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data validation problem 1

Status
Not open for further replies.

spaulding

Technical User
Jan 10, 2001
123
US
I'm trying to teach myself ASP while building a work order system for our school. Just about got it done and have learned that making sure you get valid data from the user is "somewhat" important So I've borrowed and adapted this script from a book. The following is the data entry file for the user. The script is intended to insure that each field is filled out and properly in the case of the date. If not, it is supposed to return the user to the page with the focus on the proper field and display a dialog box. It almost works. When I push submit with blank data, I get the warning box. However, when I push ok on it, I get sent to my ASP page with this error message:

Microsoft OLE DB Provider for ODBC Drivers error '80004005'

[Microsoft][ODBC Microsoft Access Driver]Error in row

/workorder/WorkOrder_Info.asp, line 89

The relevant section of code from WorkOrder_Info.asp is as follows (line 89 is the oRS.Update):

Code:
Set oConn=Server.CreateObject("ADODB.Connection")
oConn.Open "DSN=WorkOrder"
Set oRS=Server.CreateObject("ADODB.Recordset")

'******************************************
'Execute this code If New work order Is submitted
'******************************************
If Request.Form("Action")="Add" Then

'********************************************
'Select Recordset
'********************************************
sqltext="Select * FROM results"
oRS.Open sqltext, oConn,adOpenkeyset,adLockOptimistic
'***************************************************
'Print out the recordset (In place for debug only)
'***************************************************
'Do While Not oRS.EOF
'    Response.Write oRS(&quot;ID&quot;) &&quot; &quot;& oRS(&quot;Name&quot;) &&quot;<br>&quot;
'    oRS.MoveNext
'Loop
'****************************************************
ORS.AddNew
oRS(&quot;Name&quot;)=Request.Form(&quot;Name&quot;)
oRS(&quot;submitDate&quot;)=Request.Form(&quot;submitDate&quot;)
oRS(&quot;Priority&quot;)=Request.Form(&quot;Priority&quot;)
oRS(&quot;Campus&quot;)=Request.Form(&quot;Campus&quot;)
oRS(&quot;Building&quot;)=Request.Form(&quot;Building&quot;)
oRS(&quot;Room&quot;)=Request.Form(&quot;Room&quot;)
oRS(&quot;WorkRequested&quot;)=Request.Form(&quot;WorkRequested&quot;)
oRS.Update


My data entry form code is as follows:

Code:
<html>

<head>
<title>FISD Maintenance Work Order Request Form</title>
<link rel=&quot;stylesheet&quot; href=&quot;WorkOrder.css&quot; type=&quot;text/css&quot;>
<Script Language=vbscript>
Sub WOSubmit_OnClick()
 If Len(WorkOrderAdd.Name.value)=0 Then
   Alert &quot;Please enter your name.&quot;
   WorkOrderAdd.Name.focus
 Exit Sub 
 ElseIf Len(WorkOrderAdd.submitDate.value)=0 Then
   Alert &quot;Please enter the Date.&quot;
   WorkOrderAdd.submitDate.focus
   
 Exit Sub
 ElseIf NOT IsDate(WorkOrderAdd.submitDate.value) Then
   Alert &quot;Please enter Date as mm/dd/yy&quot;
   WorkOrderAdd.submitDate.focus
    Exit Sub
 ElseIf Len(WorkOrderAdd.Building.value)=0 Then
   Alert &quot;Please enter the Building (number or description).&quot;
   WorkOrderAdd.Building.focus
 Exit Sub
 ElseIf Len(WorkOrderAdd.Room.value)=0 Then
   Alert &quot;Please enter the Room.&quot;
   WorkOrderAdd.Room.focus
 Exit Sub
 ElseIf Len(WorkOrderAdd.WorkRequested.value)=0 Then
   Alert &quot;Seems sort of silly to submit a work order and not tell us what needs fixing, Doesn't it?&quot;
   WorkOrderAdd.WorkRequested.focus
 Exit Sub
End if
Call WorkOrderAdd.submit()
End Sub
</Script>

</head>

<body style=&quot;text-align: center&quot;>

<h1 class=&quot;wo&quot; align=&quot;center&quot;>Work Order Request Form</h1>

<form method=&quot;POST&quot; action=&quot;WorkOrder_Info.asp&quot; Name=&quot;WorkOrderAdd&quot;>
<input Type=&quot;hidden&quot; name=&quot;Action&quot; value=&quot;Add&quot;>
 <table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
    <tr>
      <td width=&quot;33%&quot;>Name:<br>
      <input type=&quot;text&quot; name=&quot;Name&quot; size=&quot;20&quot;></td>
      <td width=&quot;33%&quot;>Date:<br>
      <input type=&quot;text&quot; name=&quot;submitDate&quot; size=&quot;20&quot;></td>
      <td width=&quot;34%&quot;>Priority:<br>
      <select size=&quot;1&quot; name=&quot;Priority&quot;>
      <option selected value=&quot;3-Repair&quot;>Repair</option>
      <option value=&quot;1-Life/Safety&quot;>Life/Safety</option>
      <option value=&quot;2-Emergency&quot;>Emergency</option>
      <option value=&quot;4-Prevention&quot;>Prevention</option>
      <option value=&quot;5-Improvement&quot;>Improvement</option>
      </select></td>
    </tr>
    <tr>
      <td width=&quot;33%&quot;>Campus:<br>
      <select size=&quot;1&quot; name=&quot;Campus&quot;>
      <option selected>FHS</option>
      <option>FMS</option>
      <option>FES</option>
      <option>FPS</option>
      <option>Stonewall</option>
      <option>Special Ed</option>
      <option>AEP</option>
      <option>GCLC</option>
      <option>Central Office</option>
      <option>Transportation          </option>
</select></td>
      <td width=&quot;33%&quot;>Building:<br>
      <input type=&quot;text&quot; name=&quot;Building&quot; size=&quot;20&quot;></td>
      <td width=&quot;34%&quot;>Room:<br>
      <input type=&quot;text&quot; name=&quot;Room&quot; size=&quot;14&quot;></td>
    </tr>
    <tr>
      <td width=&quot;100%&quot; colspan=&quot;3&quot;>
      <p align=&quot;center&quot;>Work Requested:<br>
      <textarea rows=&quot;4&quot; name=&quot;WorkRequested&quot; cols=&quot;44&quot;></textarea></td>
    </tr>
   </table>
  <p><input type=&quot;submit&quot; value=&quot;Enter this Work Order&quot; name=&quot;WOSubmit&quot;><input type=&quot;Button&quot; value=&quot;Reset this information&quot; name=&quot;WOReset&quot;></p>

<!----><Input type=&quot;button&quot; value=&quot;Edit Existing Work Order&quot; onClick=window.location=&quot;[URL unfurl="true"]http://intra/workorder/edit.htm&quot;>[/URL]
</form>
</body>

</html>

The program works great as long as the data is valid. I appreciate any help anyone can give.
Thanks
Rick
 
Ok, without looking to deeply into your code, I'm pretty sure I know the answer already (experience, I've made at least 3/4's of the errors in the book, and several that weren't)

Anytime it gives you an error message on the rs.Update or .Execute method, your generally looking at a problem with either a) the data types your entering, or b) the sql stmt. Since your not using an SQL statement this means you have datatype issues. The easiest way to resolve this is to check your database fields and then make sure that you are converting to the correct type before putting them in the field:
Code:
If submitDate is a date type, convert the value before assigning it, like so:
oRS(&quot;submitDate&quot;)=cDate(Request.Form(&quot;submitDate&quot;)) 'convert to date type

If room is a numeric field, try this:
oRS(&quot;Room&quot;)=cInt(Request.Form(&quot;Room&quot;)) 'convert to integer

Another possibility, as you mentioned is that one of these form fields could be blank. You should set up variables and assign the form values to them, checking if they are blank and converting in the process:
Code:
'before your addnew section
Dim tDate
If Request.Form(&quot;submitDate&quot;) <> &quot;&quot; Then
   'you may want to use another if stmt here and the isDate to make sure this is a valid date
   tDate = cDate(Request.Form(&quot;submitDate&quot;)
End If

'then if any of your if stmts don't succeed you can print an error message and not execute the AddNew

'inside the AddNew
oRS(&quot;Room&quot;)=tDate   'again, just the date field, as it is a good example for type conversion

Anyways, hope that helps,
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 
Tarwn,
I appreciate the quick feedback. The second half of your answer is what I'm trying to do with my script in the second section of code. I'd like to validate that data client side and (given my limited experience) I think yours does it server side. But that's not really the issue. My script is supposed to check the data, display a message if it's not valid and stop the page from being submitted by exiting the subroutine before the Call WorkOrderAdd.submit()-- but it doesn't appear to be doing that. When I intentionally put in blanks, it looks like it checks the data, recognizes the invalid data, displays the message and STILL submits the form. This isn't my only project so understanding &quot;why&quot; this isn't working is almost as important as &quot;how&quot; to fix it.
Again, I appreciate your help.
 
Oops, sorry for the confusion, coffee intake is slow today :)
I'll have to bow out of this one, I generally write all my client-side code in javascript and haven't written client-side VBScript in a couple years (ish).
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 
Since I am just learning this, out of curiosity, why javascript client side instead of VBscript? What are you using server side?
 
may I
spaulding, the reasoning behind the javascript use on the client level is purely IE is the only browser that supports vbscript on this level.
Unless you are in a closed does company then you really don't want to write any client side scripting in vbscript for this one reason. any user that views with any other browser will not have any of the functionality of the script Just a suggestion: faq183-874
admin@onpntwebdesigns.com
 
Thanks,
Not a problem here since this will be on a closed intranet with only IE running, but an important safety tip for future projects.
 
I don't think that you can use an updatable recordset here... Try writing a straight INSERT statement.

strSQL = &quot;INSERT INTO myTable (field1, field2) VALUES (&quot;&_
request(&quot;field1&quot;) & &quot;,'&quot; & request(&quot;field2) & &quot;')&quot;

oConn.execute(strSQL) -----------------------------------------------------------------
[pc] Be nice. It's only doing what you tell it to do.
mikewolf@tst-us.com
 
well then we have something to work with
alright. in order to validate a form onsubmit with vbscript there are a few steps you need to take
first, change the sub to a function and you need to set the form to false OnSubmit()
so after that it should look like this
Function WorkOrderAdd_OnSubmit()
WorkOrderAdd_OnSubmit = false

now lets make the Focus a bit easier with
Set form = Document.WorkOrderAdd

next we do your validation
If Len(WorkOrderAdd.Name.value)=0 Then
Alert &quot;Please enter your name.&quot;
form.Name.Focus

ElseIf Len(WorkOrderAdd.submitDate.value)=0 Then
Alert &quot;Please enter the Date.&quot;
form.submitDate.Focus

ElseIf NOT IsDate(WorkOrderAdd.submitDate.value) Then
Alert &quot;Please enter Date as mm/dd/yy&quot;
form.submitDate.Focus

ElseIf Len(WorkOrderAdd.Building.value)=0 Then
Alert &quot;Please enter the Building (number or description).&quot;
form.Building.Focus

ElseIf Len(WorkOrderAdd.Room.value)=0 Then
Alert &quot;Please enter the Room.&quot;
form.Room.Focus

ElseIf Len(WorkOrderAdd.WorkRequested.value)=0 Then
Alert &quot;Seems sort of silly to submit a work order and not tell us what needs fixing, Doesn't it?&quot;
form.WorkRequested.Focus

see how much easier it is to type form instead of document.WorkOrderAdd. which you could also use in the value extraction.

now if we have gotten that far we go back to the boolean value we set for our form with the OnSubmit event and set it to true so it actually submit's
Else
WorkOrderAdd_OnSubmit = True
End if
End Function

and there you have it.

also noticed that your reset button isn't a reset button
<input type=&quot;reset&quot; value=&quot;Reset this information&quot;>

and you really don't need to name them unless you are doing some dynamic changes to the attributes.


hope that helps out Just a suggestion: faq183-874
admin@onpntwebdesigns.com
 
ONPNT

Thanks, that worked.

MWolf00
Looks like the problem wasn't with the SQL statement. Your's however is cleaner than mine so I am using it. Thanks




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top