How to get the last record id of a form?
ms-access, vba
Solution
There is a function `DMax` that will grab the highest number.
Dim lastID As Integer
lastID = DMax("IDField","YourTable")
' or = DMax("IDField","YourTable","WhenField=Value")
MsgBox lastID
The other Domain functions are:
- DAvg
- DCount
- DFirst
- DLast
- DLookup
- DMin
- DStDev
- DStDevP
- DSum
- DVar
- DVarP
Check with your friendly F1 key for more info
Problem
I currently have a form in access. What I want to do is get the value of the last record added. For example, if i have 10 records, I want to get the value "10", because this is the id of the added last record. I am trying to run a query with the function last id inserted() but it is not working. This the code I am using : ``` Dim lastID As Integer Query = "select last_insert_id()" lastID = Query MsgBox (lastID) ``` What am I missing?