-
Sub Addrecord
dim objConn, objRS, objComm
dim start
Set objConn = Server.CreateObject("ADODB.Connection")
set objComm = Server.CreateObject("ADODB.Command")
set objRS = Server.CreateObject ("ADODB.Recordset")
objConn.Open "File Name=D:\VirtueBroadcastingIntranet\HR\Conn\objConn.udl"
objComm.ActiveConnection = objConn
objComm.CommandText = "INSERT INTO vtvemployee(emp_fname,emp_sname,emp_dept,emp_salary,emp_hrspw,emp_stdrate,emp_start,emp_holiday,emp_ bank,emp_sort,emp_acc)VALUES(" & Request.Form ("fname") & "," & Request.Form ("sname") & "," & Request.Form ("dept") & "," & Request.Form ("salary") & "," & Request.Form ("hrspw") & "," & Request.Form ("stdrate") & "," & Request.Form ("start") & "," & Request.Form ("holiday") & "," & Request.Form ("bank") & "," & Request.Form ("sort") & "," & Request.Form ("acc") & ")""
set objRS = objComm.Execute
I don't get it. I'm thick.
-
You have to put single quotes around a string.
Quick example:
Code:
VALUES('" & Request.Form ("fname") & "')"
and for dates in access they have to be enclosed with 2 "#". for sql server a single quotes are needed instead for the date.
This should solve it a little.
-
thanks but still no joy
I've changed some of my code to try different things but still no joy. Any clues?
I think the msgbox thing won't work either. I've just been moved over from VB6 and and getting used to scripting.. Help meeeee.
Thx.
Sub Addrecord
dim objConn, objRS, objComm
dim sql
dim start
start = Request.Form ("startd") & "/" & Request.Form ("startm") & "/" & Request.Form ("starty")
Set objConn = Server.CreateObject("ADODB.Connection")
set objComm = Server.CreateObject("ADODB.Command")
set objRS = Server.CreateObject ("ADODB.Recordset")
objConn.Open "File Name=D:\VirtueBroadcastingIntranet\HR\Conn\objConn.udl"
objComm.ActiveConnection = objConn
'object string for SQL
'objComm.CommandText = "INSERT INTO vtvemployee(emp_fname,emp_sname,emp_dept,emp_salary,emp_hrspw,emp_stdrate,emp_start,emp_holiday,emp_ bank,emp_sort,emp_acc)VALUES('" & Request.Form ("fname") & "','" & Request.Form ("sname") & "','" & Request.Form ("dept") & "','" & Request.Form ("salary") & "','" & Request.Form ("hrspw") & "','" & Request.Form ("stdrate") & "','" & start & "','" & Request.Form ("holiday") & "','" & Request.Form ("bank") & "','" & Request.Form ("sort") & "','" & Request.Form ("acc") & "')"
'debugging string for SQL
sql = "INSERT INTO"
sql = sql & " vtvemployee"
sql = sql & " (emp_fname,emp_sname,emp_dept,emp_salary,emp_hrspw,emp_stdrate,emp_start,emp_holiday,emp_bank,emp_so rt,emp_acc)"
sql = sql & " VALUES"
sql = sql & " ('" & Request.Form ("fname") & "','" & Request.Form ("sname") & "','" & Request.Form ("dept") & "','" & Request.Form ("salary") & "','" & Request.Form ("hrspw") & "','" & Request.Form ("stdrate") & "','" & start & "','" & Request.Form ("holiday") & "','" & Request.Form ("bank") & "','" & Request.Form ("sort") & "','" & Request.Form ("acc") & "')"
set objRS.Open sql,objConn,adOpenKeyset,adLockOptimistic ,adcmdtext
'set objRS = objComm.Execute
%>
<P align=center><B>-- Record Added --</B>
<table align="center" CLASS=smalltext width=100% border=1>
<tr>
<td><B>ID</B></td> <td><B>First Name</B></td> <td><B>Surname</B></td> <td><B>Department</B></td> <td><B>Start Date</B></td> <td><B>Salary P/A</B></td> <td><B>Hourly Rate</B></td> <td><B>Hours P/W</B></td> <td><B>Paid Leave</B></td> <td><B>Bank Name</B></td> <td><B>Sort Code</B></td> <td><B>Account No</B></td>
</tr>
<tr>
<td><%=objConnRS("emp_id")%></td><td ><%=objConnRS("emp_fname")%></td><td ><%=objConnRS("emp_sname")%></td><td ><%=objConnRS("emp_dept")%></td><td ><%=objConnRS("emp_start")%></td><td ><%=objConnRS("emp_salary")%></td><td><%=objConnRS("emp_stdrate")%></td><td><%=objConnRS("emp_hrspw")%></td><td><%=objConnRS("emp_holiday")%></td><td><%=objConnRS("emp_bank")%></td><td><%=objConnRS("emp_sort")%></td><td><%=objConnRS("emp_acc")%></td>
</tr>
</table><br>
<input type= "button" value="Save" id=cmdSave name=cmdSave><input type="button" value="Cancel" id=cmdSave name=cmdSave>
<%
if cmdSave = "Save" then
objRS.Update
objRS.close
objConn.close
set objConnRS= nothing
set objConn = nothing
makeselection
elseif cmdSave = "Cancel" then
msgbox ("Employee not added to database",0,"Entry Aborted")
objRS.close
objConn.close
set objConnRS= nothing
set objConn = nothing
makeselection
end sub
-
Command buttons do not work the same way in script as they do in VB. You have to either redirect to the same page and put your code for command button clicks (the database stuff which must be done on the server side) before your HTML tag, or redirect to a new page that contains the button code. You should not put it at the end of the page it will not be called because the user has not clicked anything when that code it reached (on the server since it is server side code).
Remember:
All server side code is processed before the user sees the page.
Only client side code can be executed on a button click.
You can use that client side code to redirect to a page and use a querystring to send info back to the page on what action to take.