How to Get Just Added Primary Key / Autonumber Value

  • Thread starter Thread starter Mike Thomas
  • Start date Start date
M

Mike Thomas

In Access 2000, in the statement below, is there a way to retrieve the value
of the Autonumber/ Primary Key field which Access has just inserted
automatically into the table's new row?

strSQL = "INSERT INTO [SomeTable] (Field2, Field3) " & _
" VALUES ( Val1, Val2 )"

db.Execute (strSQL)


Many thanks,
Mike Thomas
 
Hi,


If you use

CurrentProject.Connection.Execute strSQL


then, you can check

var = CurrentProject.Connection.Execute("SELECT @@IDENTITY")



@@IDENTITY is maintained by connection (so, by user), so you must use
the same reference than you use to execute the SQL statement.



Hoping it may help,
Vanderghast, Access MVP
 
Back
Top