I have the following code in MS Access:
Sub IdentityFail()
Dim db1 As DAO.Database, db2 As DAO.Database
Dim id1 As Long, id2 As Long
CurrentDb.Execute "CREATE TABLE LocalDummy (Col1 AUTOINCREMENT, Col2 INT)", dbFailOnError
Set db1 = CurrentDb
Set db2 = CurrentDb
db1.Execute "INSERT INTO LocalDummy(Col2) VALUES(Null)", dbFailOnError
id1 = db1.OpenRecordset("SELECT @@IDENTITY")(0)
db2.Execute "INSERT INTO LocalDummy(Col2) VALUES(Null)", dbFailOnError
id2 = db2.OpenRecordset("SELECT @@IDENTITY")(0)
Debug.Print id1, id2
Debug.Print db1.OpenRecordset("SELECT @@IDENTITY")(0), _
db2.OpenRecordset("SELECT @@IDENTITY")(0), _
CurrentDb.OpenRecordset("SELECT @@IDENTITY")(0)
End Sub
I would expect this to output the following (ie, each distinct db object would have its own "most recent identity" value):
1 2
1 2 0
Instead I get (ie, it appears to be globally scoped):
1 2
2 2 2
I thought SELECT @@IDENTITY
was the safe way to get the latest autonumber ID in Jet 4.0+. What am I doing wrong?