This function inserts a row into a SQL database and needs to return the identity number created:
Function WriteDatabase(backupTypeID, numImages, folderSize, success, errorMessage, strLogFileName)
On Error Resume Next
err.clear
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
objConnection.Open "Provider=SQLOLEDB;Data Source=x.x.x.x;Initial Catalog=DB;User ID=sa;Password=xxxxxx"
sqlquery = "INSERT INTO tblImageCopies (BackupCopyDate, BackupCopyTypeID, NumImages, ImagesFolderSize, Success, ErrorMessage) VALUES (GETDATE(), " & backupTypeID & ", " & numImages & ", " & folderSize & ", " & success & ", " & errorMessage & "); SELECT scope_identity() AS ImageCopyID;"
objRecordSet.Open sqlquery,objConnection
objRecordSet.MoveFirst
WriteDatabase = objRecordSet("ImageCopyID")
objRecordSet.Close
objConnection.Close
If err.number <> 0 Then
WriteLog "Error writing to the EHN database - " & err.number & " " & err.description, strLogFileName
End If
End Function
It successfully inserts the row, but I get the error message '3704 Operation is not allowed when the object is closed.' when it tries to return the identity number in the record set. When I execute this sql query directly on the server, it works. Anyone able to help?
I do the same thing (very similar anyway). I believe that there are two sets of results coming back, one for the INSERT and then another for the SELECT. Try calling objRecordSet.NextRecordset().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With