Using “SELECT SCOPE_IDENTITY()” in ADODB Recordset

后端 未结 4 1958
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  • 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)
    
    0 讨论(0)
  • 2020-12-20 20:20

    In your rs.Open Try this

    rs.Open SQLStr, cn, adCmdText

    0 讨论(0)
  • 2020-12-20 20:22

    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
    
    0 讨论(0)
  • 2020-12-20 20:34

    See what happens when you remove the adOpenKeySet and adLockOptimistic values leave them at their defaults.

    0 讨论(0)
提交回复
热议问题