I fixed this by using
Set reader2 = reader2.NextRecordset
after the command execution. The batched commands create a list of recordsets, the first of which is the recordset belonging to the insert, not the select.
Pergunta
I am attempting to send a command using ADODB.Command
in VBA. I know the connection and command work properly, as they are used in several places in the code. However, I am having trouble getting a dual insert/select to work. I keep getting a recordset with fields of count 0. I'm more familiar with using C# SqlCommand objects, which allow this sort of stacked commands. Is there a way to do this in VBA using ADODB? Code below:
NOTE: reader2 is an ADODB.Recordset
. Connecting to Microsoft SQL Server using TSQL
comm2.CommandText = "INSERT INTO tblTest (F1,F2,F3,F4,F5) VALUES(?,?,'Running',?,0); SELECT SCOPE_IDENTITY()"
comm2.Parameters.Append comm2.CreateParameter("@f1", adVarChar, adParamInput, Len(f1) + 1, f1)
comm2.Parameters.Append comm2.CreateParameter("@f2", adVarChar, adParamInput, 1, f2)
comm2.Parameters.Append comm2.CreateParameter("@f3", adDate, adParamInput, 0, Now)
Set reader2 = comm2.Execute
scopeId = reader2.Fields(0)
reader2.Close
comm2.Parameters.Delete (0)
comm2.Parameters.Delete (0)
comm2.Parameters.Delete (0)
Solução
I fixed this by using
Set reader2 = reader2.NextRecordset
after the command execution. The batched commands create a list of recordsets, the first of which is the recordset belonging to the insert, not the select.