|
 |
access_asp thread: Inserting into database
Message #1 by "Brooke Alexander" <noni_j@h...> on Wed, 3 Oct 2001 20:51:25
|
|
Can someone help me? I'm a newbie to asp, and I am trying to insert
information into 2 separate Access tables in a database. I followed an
example in the Beginning ASP Database book I bought, but I'm getting an
error that I don't know how to fix. Below is my code along with the error
message:
<%
'Declare variables needed
Dim strInsert
Dim strValues
Dim adCmdText
'Set required variables
adCmdText = 1
'***********************************************************
'* If an Add was requested, add the new club to the database
'***********************************************************
'Inserting into the first table in the database
If Request.Form("Action") = "Add" Then
'Start building the SQL strings with the required fields
strInsert = "Insert into Employees (EmployeeID"
strValues = "Values('" & CStr(Request.Form("txtEmployeeID")) & "'"
'Inserting into the second table in the database
If Request.Form("Action") = "Add" Then
'Start building the SQL strings with the required fields
strInsert = "Insert into Future Curve (FutureCurve"
strValues = "Values('" & CStr(Request.Form("txtFutureCurve")) & "'"
'Add First names
If Len (Request.Form("txtFirstName")) >0 Then
'Add the column name to the insert string
strInsert = strInsert & ",FirstName"
strValues = strValues & "," & _
Cstr(Request.Form("txtFirstName")) & "'"
End If
'Add Last names
If Len (Request.Form("txtLastName")) >0 Then
'Add the column name to the insert string
strInsert = strInsert & ",LastName"
strValues = strValues & "," & _
Cstr(Request.Form("txtLastName")) & "'"
End If
'Add Job Title
If Len (Request.Form("txtJobTitle")) >0 Then
'Add the column name to the insert string
strInsert = strInsert & ",JobTitle"
strValues = strValues & "," & _
Cstr(Request.Form("txtJobTitle")) & "'"
End If
'Add Code
If Len (Request.Form("txtcode")) >0 Then
'Add the column name to the insert string
strInsert = strInsert & ",Code"
strValues = strValues & "," & _
Cstr(Request.Form("txtCode")) & "'"
End If
'Add Supervisor
If Len (Request.Form("txtSupervisor")) >0 Then
'Add the column name to the insert string
strInsert = strInsert & ",Supervisor"
strValues = strValues & "," & _
Cstr(Request.Form("txtSupervisor")) & "'"
End If
'Create and open the database object
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "DSN=noni"
'Create the command object
Set objCmd = Server.CreateObject("ADODB.Command")
'Set the command object properties
Set objCmd.ActiveConnection = objConn
objCmd.CommandText = strInsert & ") " & strValues & ")"
objCmd.CommandType = adCmdText
'Execute the command
objCmd.Execute
'Display the insert string
Response.Write "The following insert string was executed and " & _
"the values inserted into the Employees and Future Curve
tables.<P>"
Response.Write strInsert & ") " & strValues & ")"
End If
'Close and dereference database objects
Set objCmd = Nothing
objConn.Close <----------Error is pointing to this line.
Set objConn = Nothing
%>
ERROR MESSAGE:
Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required: 'objConn'
/noniweb/curve2.asp, line 102
I don't know what else to do. HELP
Thank you in advance.
Brooke Alexander
Message #2 by "Zimmer Computer Consulting" <zee@t...> on Wed, 3 Oct 2001 15:48:53 -0700
|
|
A workaround:
' From ADOVBS.inc
adStateOpen = 1
' Check if connection open before closing
IF objConn.State = adStateOpen THEN
objConn.Close
END IF
You could also try moving the statement
set objComm = nothing
to after the objConn.close statement.
----- Original Message -----
From: Brooke Alexander <noni_j@h...>
To: Access ASP <access_asp@p...>
Sent: Wednesday, October 03, 2001 8:51 PM
Subject: [access_asp] Inserting into database
> Can someone help me? I'm a newbie to asp, and I am trying to insert
> information into 2 separate Access tables in a database. I followed an
> example in the Beginning ASP Database book I bought, but I'm getting an
> error that I don't know how to fix. Below is my code along with the error
> message:
>
> <%
> 'Declare variables needed
> Dim strInsert
> Dim strValues
> Dim adCmdText
>
> 'Set required variables
> adCmdText = 1
>
> '***********************************************************
> '* If an Add was requested, add the new club to the database
> '***********************************************************
> 'Inserting into the first table in the database
> If Request.Form("Action") = "Add" Then
>
> 'Start building the SQL strings with the required fields
> strInsert = "Insert into Employees (EmployeeID"
> strValues = "Values('" & CStr(Request.Form("txtEmployeeID")) & "'"
>
> 'Inserting into the second table in the database
> If Request.Form("Action") = "Add" Then
>
> 'Start building the SQL strings with the required fields
> strInsert = "Insert into Future Curve (FutureCurve"
> strValues = "Values('" & CStr(Request.Form("txtFutureCurve")) & "'"
>
>
>
> 'Add First names
> If Len (Request.Form("txtFirstName")) >0 Then
> 'Add the column name to the insert string
> strInsert = strInsert & ",FirstName"
> strValues = strValues & "," & _
> Cstr(Request.Form("txtFirstName")) & "'"
> End If
>
> 'Add Last names
> If Len (Request.Form("txtLastName")) >0 Then
> 'Add the column name to the insert string
> strInsert = strInsert & ",LastName"
> strValues = strValues & "," & _
> Cstr(Request.Form("txtLastName")) & "'"
> End If
>
> 'Add Job Title
> If Len (Request.Form("txtJobTitle")) >0 Then
> 'Add the column name to the insert string
> strInsert = strInsert & ",JobTitle"
> strValues = strValues & "," & _
> Cstr(Request.Form("txtJobTitle")) & "'"
> End If
>
> 'Add Code
> If Len (Request.Form("txtcode")) >0 Then
> 'Add the column name to the insert string
> strInsert = strInsert & ",Code"
> strValues = strValues & "," & _
> Cstr(Request.Form("txtCode")) & "'"
> End If
>
> 'Add Supervisor
> If Len (Request.Form("txtSupervisor")) >0 Then
> 'Add the column name to the insert string
> strInsert = strInsert & ",Supervisor"
> strValues = strValues & "," & _
> Cstr(Request.Form("txtSupervisor")) & "'"
> End If
>
>
> 'Create and open the database object
> Set objConn = Server.CreateObject("ADODB.Connection")
> objConn.Open "DSN=noni"
>
> 'Create the command object
> Set objCmd = Server.CreateObject("ADODB.Command")
>
> 'Set the command object properties
> Set objCmd.ActiveConnection = objConn
> objCmd.CommandText = strInsert & ") " & strValues & ")"
> objCmd.CommandType = adCmdText
>
> 'Execute the command
> objCmd.Execute
>
> 'Display the insert string
> Response.Write "The following insert string was executed and " & _
> "the values inserted into the Employees and Future Curve
> tables.<P>"
> Response.Write strInsert & ") " & strValues & ")"
>
>
>
> End If
>
> 'Close and dereference database objects
> Set objCmd = Nothing
> objConn.Close <----------Error is pointing to this line.
> Set objConn = Nothing
> %>
>
> ERROR MESSAGE:
> Error Type:
> Microsoft VBScript runtime (0x800A01A8)
> Object required: 'objConn'
> /noniweb/curve2.asp, line 102
>
>
> I don't know what else to do. HELP
> Thank you in advance.
>
> Brooke Alexander
|
|
 |