How to get id of newly inserted record using Excel VBA?

前端 未结 6 1593
傲寒
傲寒 2020-12-16 20:49

Seems a common enough problem this, but most solutions refer to concatenating multiple SQL commands, something which I believe can\'t be done with ADO/VBA (I\'ll be glad to

6条回答
  •  -上瘾入骨i
    2020-12-16 21:04

    Re: "I have tried to get @@IDENTITY working, but this always returns 0 using the code below."

    Your code sends SQL and SQL2 through different connection objects. I don't think @@identity will return anything other than zero unless you ask from the same connection where you executed your INSERT statement.

    Try changing this:

    myRecordset.Open SQL2, dbConnectionString, adOpenStatic, adLockReadOnly
    

    to:

    myRecordset.Open SQL2, databaseConnection, adOpenStatic, adLockReadOnly
    

提交回复
热议问题