HI treadMill
Here is some sample code that I have used in the past.
This function is used to add an address to the database.
The connection string is held in another function named dbConnString() and is therefore used to return the connection details.
[Not included here]
Its not perfect but it may help!
Hope it makes sense.
Regards
Private Function addAddress(strAddrL1,strAddrL2,strAddrL3,strHomeTo wn, strHomeCity,intCountry, strAddrCode, strHPhone, strMPhone, strWPhone, _
intAddrType, lngNewAddressID, strErrMsg)
Dim objConn, objCmd, objParam, objRS, intParamIndex
set objConn = server.CreateObject("ADODB.Connection")
objConn.ConnectionString = dbConnString()
objConn.CursorLocation = adUseServer
objConn.Open
If objConn.State And adStateOpen Then 'the connection is open
set objCmd = Server.CreateObject("ADODB.Command")
With objCmd
.ActiveConnection = objConn
.CommandType = adCmdStoredProc
.CommandText = "usp_ins_param_address" 'name of QRY or SP
set objParam = .CreateParameter("Line1",adChar,adParamInput,"100" ,strAddrL1)
.Parameters.Append objParam
set objParam = .CreateParameter("Line2",adChar,adParamInput,"100" ,strAddrL2)
.Parameters.Append objParam
set objParam = .CreateParameter("Line3",adChar,adParamInput,"100" ,strAddrL3)
.Parameters.Append objParam
set objParam = .CreateParameter("Town",adChar,adParamInput,"100", strHomeTown)
.Parameters.Append objParam
set objParam = .CreateParameter("City",adChar,adParamInput,"100", strHomeCity)
.Parameters.Append objParam
set objParam = .CreateParameter("Country",adInteger,adParamInput, ,intCountry)
.Parameters.Append objParam
set objParam = .CreateParameter("AddrCode",adChar,adParamInput,"1 5",strAddrCode)
.Parameters.Append objParam
set objParam = .CreateParameter("HomePhone",adChar,adParamInput," 20",strHPhone)
.Parameters.Append objParam
set objParam = .CreateParameter("MobilePhone",adChar,adParamInput ,"20",strMPhone)
.Parameters.Append objParam
set objParam = .CreateParameter("WorkPhone",adChar,adParamInput," 20",strWPhone)
.Parameters.Append objParam
set objParam = .CreateParameter("AddrType",adInteger,adParamInput ,,intAddrType)
.Parameters.Append objParam
'completed creating the params, so discard the object
set objParam = nothing
On Error Resume Next
objConn.BeginTrans()
.Execute 'the command to insert the address
If Err.number <> 0 Then
objConn.RollBackTrans()
strErrMsg = Err.Description & "Source : " & Err.Source & _
"<br />Error in Module: dbFuncs.addAddress()" & _
"<br />Caused a RollBackTrans() while inserting customer address."
addAddress = 1
set objConn = Nothing
set objCmd = Nothing
Exit Function
End If
'now get the address ID just created using pre-written query
.CommandText = "usp_sel_getMaxAddressID" 'name of MS Access query
set objRS = .Execute 'the command
If not objRS.EOF then
'get the new id and assign it to the parameter passed in
lngNewAddressID = objRS.Fields("MaxOfAddr_ID")
addAddress = 0
Else
'force a rollback
objConn.RollBackTrans()
strErrMsg = "Error in Module: dbFuncs.addAddress()" & _
"Unable to retrieve AddressID. Caused a RollBackTrans()<br />" & _
"The recordset was empty.<br />"
lngNewAddressID = -1
addAddress = 1
set objConn = Nothing
set objCmd = Nothing
Exit Function
End If
'commit the changes to the database
objConn.CommitTrans()
On Error GoTo 0 'turn error handling back on
End With 'objCmd
set objRS = nothing
set objCmd.ActiveConnection = nothing
set objCmd = nothing
Else
'no connection
strErrMsg = "Error in Module: dbFuncs.addAddress()" & _
"Unable to connect to the database."
addAddress = 1 'bad return code
End If
On Error GoTo 0 'turn error handling back on
If not objConn is nothing then
if objConn.State then objConn.Close()
set objConn = nothing
End if
End Function 'addAddress
|