How to retrieve autonumbered field via ADODB

谁说胖子不能爱 提交于 2019-12-11 09:57:13

问题


Context: MS Access and JScript

I have a table into which I insert one field's worth of data so that the autonumber will fire and give me that record's unique id, viz

oConn.Execute( "INSERT INTO tbl (DateTimeStamp) VALUES (" + newNow + ");");

in this case, newNow is a Double value which is automagically coerced into a Date.

Once that's done, I would like to retrieve the autonumbered ID field which should have been filled at the point of INSERT. Up until now I've been using ADODB.Recordset for this stuff, but have been experimenting with ADODB.Connection's Execute() in the hope that it might be faster.


回答1:


With oConn as an ADODB.Connection, in VBA the following is convenient to retrieve the last autonumber value inserted from that connection. See if that translates to JScript.

oConn.Execute("SELECT @@Identity")(0)

Rather than automagically casting the double value to Date/Time, you can ask the db engine to do it explicitly.

"INSERT INTO tbl (DateTimeStamp) VALUES (CDate(" + newNow + "));"



回答2:


You can use @@Identity with MS Access against your connection.

SELECT @@identity


来源:https://stackoverflow.com/questions/10999973/how-to-retrieve-autonumbered-field-via-adodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!