0

I need to get the Autonumbered ID of the last record inserted into a table (through INSERT INTO command)

I have seen some methods in the forum that looks to work for most of the community but not in my case: Autonumber value of last inserted row - MS Access / VBA

Dim miDDBB As DAO.Database
.....
StrSQL = " INSERT INTO DG_tb_Main " ......   
Set miDDBB = CurrentDb
miDDBB.Execute (StrSQL)
newRow = miDDBB.OpenRecordset("SELECT @@IDENTITY")(0)
Set db = Nothing

When using this code I always get the 'newRow' value as '0'. I have checked the SQL expresion and works fine, insterting the new record into the table. ¿What am I doing wrong?

Regards

  • Is this your actual code? `Set db = Nothing` vs. `miDDBB` before is suspect. -- Is `DG_tb_Main` a linked table? If so, from where? – Andre Apr 22 '19 at 09:24

1 Answers1

0

Study your own reference again.

You have DAO, but use the method for ADO.

Gustav
  • 53,498
  • 7
  • 29
  • 55