Using a VBA script in Excel, I\'m trying to insert a new row into a table and then get back the identity value of that row. If I run:
When you run a batch of commands using ADODB, I believe it runs each one seperately. To force the next command to run, you have to use the following:
Set rs = rs.NextRecordset()
Changing the end of your routine to the following should do the trick:
Set rs = New ADODB.Recordset
rs.Open SQLStr, cn, adOpenKeyset, adLockOptimistic
Set rs = rs.NextRecordset
MsgBox (rs.Fields(0).Value)
In your rs.Open Try this
rs.Open SQLStr, cn, adCmdText
You are executing two statements so you will get two results back. the recordset object can only hold one result at a time - to get the other result you need to use the NextRecordset method.
Set rs = rs.NextRecordset
See what happens when you remove the adOpenKeySet and adLockOptimistic values leave them at their defaults.