Adding to database using subroutine

Results 1 to 3 of 3

Thread: Adding to database using subroutine

  1. #1
    Join Date
    Dec 1969

    Default Adding to database using subroutine

    I would like to add records to serval databases withing a script using a subroutine. <BR><BR>Sub AddToDatabase(strDB,strField,strData)<BR> &#039;strDB is Table name<BR> &#039;strField is the Field name<BR> &#039;strData is the data adding<BR><BR> Dim DSNtemp, objConn, objCmd, objRecSet<BR> Dim strSQL1<BR><BR> &#039;Create and open the Database object<BR> set objConn = server.createobject("adodb.connection")<BR> DSNtemp="DRIVER={Microsoft Access Driver (*.mdb)}; "<BR> DSNtemp=dsntemp & "DBQ=" & server.mappath ("Someplace")<BR> objConn.Open DSNtemp<BR> <BR> strSQL1 = "Insert into " & strDB & " (" & strField & ") "<BR> &#039;Add the Value to the SQL string<BR> strSQL1 = strSQL1 & "Values (&#039;" & strData & "&#039;)"<BR> <BR> set objCmd=server.createobject("adodb.command")<BR> set objCmd.ActiveConnection = objConn<BR> objCmd.CommandText = strSQL1<BR> objCmd.CommandType = adCmdText<BR> <BR> &#039;Ececute the command<BR> objCmd.Execute<BR><BR> &#039;some error checking code<BR> set objCmd = Nothing<BR> objConn.Close<BR> set objConn = nothing<BR>End Sub<BR><BR>I keep getting error "Operation is not allowed when the object is closed." It adds the records to must of the Tables but I don&#039;t know what is causeing error.<BR>Am I missing something or is this not possible.<BR><BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: Adding to database using subroutine

    My guess is:<BR><BR>objConn.Close<BR>set objConn = nothing<BR>set objCmd = Nothing<BR>

  3. #3
    Chaveiro Guest

    Default RE: Adding to database using subroutine

    strange.... try also this:<BR>Set objCmd.ActiveConnection = Nothing <BR>&#039;then you close... also, where you use the execute:<BR>objCmd.Execute , ,adExecuteNoRecords<BR>&#039;just to ensure tha is not returning recordset

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts