Getting new record's identity (ID)

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

What is the correct syntax of an access insert query that also return the ID
(via @@Identity) of the new record?

Thanks

Regards
 
This should work:

Function GetID() As Variant
Dim db As DAO.Database
Dim rst As DAO.Recordset

Set db = CurrentDb
db.Execute "INSERT INTO MyTable ( MyField ) SELECT 'Whatever' AS Expr1;"

Set rst = db.OpenRecordset("SELECT @@IDENTITY AS LatestID;")
GetID = rst!LatestID

rst.Close
Set rst = Nothing
Set db = Nothing
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top