Introduction

When working with Microsoft Access, a common requirement is to insert a new record into a table and immediately retrieve the ID (primary key) that Access automatically generated. This usually happens when a table uses an AutoNumber column as its primary key.

In simple words, after inserting a row, you want to know the unique ID that was created for that row. This article explains how to return the ID of a newly inserted row in Access SQL using simple and practical approaches with clear examples.

Understanding AutoNumber and Primary Key in Access

In Access, many tables use an AutoNumber field as the primary key. This means Access automatically assigns a unique number to each new record.

Example:

When you insert a new order, Access automatically generates an OrderID. The challenge is retrieving this value after the insert.

Using @@IDENTITY to Get the Last Inserted ID

One of the simplest ways to retrieve the last inserted ID in Access SQL is to use @@IDENTITY. This returns the AutoNumber value generated by the most recent INSERT operation in the current database session.

Example:

INSERT INTO Orders (CustomerName, OrderDate)
VALUES ('Amit', Date());

SELECT @@IDENTITY AS NewOrderID;

Explanation:

This approach is simple and commonly used in small applications.

Using DAO Recordset to Insert and Return ID (Recommended)

For VBA-based Access applications, using DAO (Data Access Objects) is one of the safest and most reliable methods.

Example in VBA:

Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("Orders", dbOpenDynaset)

rs.AddNew
rs!CustomerName = "Amit"
rs!OrderDate = Date
rs.Update

MsgBox rs!OrderID

rs.Close
Set rs = Nothing
Set db = Nothing

Explanation:

Using ADO to Return the Inserted ID

If your application uses ADO instead of DAO, you can still retrieve the inserted ID, but it requires a slightly different approach.

Example using ADO:

Dim cn As Object
Dim rs As Object

Set cn = CurrentProject.Connection
Set rs = CreateObject("ADODB.Recordset")

rs.Open "SELECT * FROM Orders", cn, 1, 3

rs.AddNew
rs.Fields("CustomerName").Value = "Amit"
rs.Fields("OrderDate").Value = Date
rs.Update

MsgBox rs.Fields("OrderID").Value

Explanation:

Using INSERT with a Follow-Up SELECT Query

Another practical approach is to run an INSERT query and then immediately fetch the last ID.

Example:

INSERT INTO Orders (CustomerName, OrderDate)
VALUES ('Amit', Date());

SELECT MAX(OrderID) AS LastID FROM Orders;

Important note:

Common Mistakes to Avoid

Many beginners face issues when trying to return the inserted ID. Common mistakes include:

Understanding these mistakes helps avoid incorrect results.

Comparison Table: DAO vs ADO vs @@IDENTITY

The table below helps you quickly decide which approach is best for your use case.

MethodBest Used WhenReliabilityMulti-User SafeComplexity
DAO RecordsetVBA-based Access applicationsVery HighYesMedium
ADO RecordsetExternal connections or mixed environmentsHighYesMedium
@@IDENTITYSimple SQL insertsMediumNo (connection dependent)Low

Real-World Example: Access Form → Table Insert

A very common real-world scenario is inserting data from an Access form into a table and then using the generated ID.

Example:

VBA code behind the form button:

Private Sub btnSave_Click()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset

    Set db = CurrentDb
    Set rs = db.OpenRecordset("Orders", dbOpenDynaset)

    rs.AddNew
    rs!CustomerName = Me.txtCustomerName
    rs!OrderDate = Me.txtOrderDate
    rs.Update

    MsgBox "Order saved with ID: " & rs!OrderID

    rs.Close
    Set rs = Nothing
    Set db = Nothing
End Sub

Explanation:

This approach is widely used in real-world Access applications.

Troubleshooting Common Runtime Errors

While working with Access inserts, developers may face common issues.

Error: ID Returns Null or 0

Cause:

Solution:

Error: @@IDENTITY Returns Wrong Value

Cause:

Solution:

Error: Recordset Is Not Editable

Cause:

Solution:

Error: Field Not Found

Cause:

Solution:

Which Method Should You Use?

The best method depends on how your Access application is built:

Choosing the right approach ensures accurate and reliable results.

Summary

Returning the ID of a newly-inserted row in Access SQL is a common and important task. You can use @@IDENTITY for simple cases, DAO Recordsets for reliable VBA solutions, or ADO when working with external connections. Understanding how AutoNumber fields work and choosing the correct method ensures you always retrieve the correct ID after inserting a record.