%@ Language=VBScript %> <% option explicit %> <% 'this is a complex example showing selection, adding, updating and deleting 'following gets user input information dim lastname dim firstname dim address dim stid lastname = request.form("lastname") firstname = request.form("firstname") address = request.form("address") 'now lets fix the single quote issues... lastname = replace(lastname,"'","''") firstname = replace(firstname,"'","''") address = replace(address,"'","''") if request.form("listbox") <> "" then stid = request.form("listbox") elseif request.form("studentid") <> "" then stid = request.form("studentid") end if 'following is an aid to track progress (can be removed for final) response.write "studentid is " & stid & "
" 'following uses the form inputs to figure out what action to take if request.form("addbtn") = "Add" then addrec elseif request.form("updbtn") = "Update" then updaterec elseif request.form("delbtn") = "Delete" then deleterec end if sub addrec() on error resume next opendb dim sql if lastname <> "" and firstname <> "" and address <> "" then sql = "insert into students (lastname, firstname, address) values ('" & lastname & "', '" & firstname & "', '" & address & "');" response.write sql 'remove later conn.execute sql if err.description <> "" then response.write("
Database Add Error " & sql) sql = "select studentid from students where lastname = '" & lastname & "' and firstname = '" & firstname & "' and address = '" & address & "';" rs.open sql,conn,1,1 if not rs.eof then stid = rs.fields("studentid") rs.close else response.write ("Please enter all required information") end if end sub sub deleterec() on error resume next opendb dim sql sql = "delete * from students where studentid = " & stid & ";" response.write sql conn.execute sql stid = "" if err.description <> "" then response.write("
Database Delete Error " & sql) rs.open "students",conn,1,1 rs.close end sub sub updaterec() on error resume next opendb dim sql if lastname <> "" and firstname <> "" and address <> "" then sql = "update students set lastname = '" & lastname & "', firstname = '" & firstname & "', address = '" & address & "' where studentid = " & stid & ";" response.write sql conn.execute sql else response.write ("Please enter all required information") end if if err.description <> "" then response.write("
Database Update Error " & sql) rs.open "students",conn,1,1 rs.close end sub %>
Note: code messages can be hidden later...
<% if stid <> "" then rs.close closedb %>