Using “SELECT SCOPE_IDENTITY()” in ADODB Recordset

后端 未结 4 1959
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-20 19:42

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:



        
4条回答
  •  -上瘾入骨i
    2020-12-20 20:10

    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)
    

提交回复
热议问题