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?
Following on from the last comments, here's a piece of code I used recently to turn the last ID value of a record set into variable for use in VBA. It's not great, however, because I still can't work out how to turn the record's ID field value directly into a variable. Instead I used the inelegant solution of copying the record set into an excel workbook, and then setting the variable value to the value of the cell I just copied into.
EDIT: Worked out how to turn the ID into a simple variable: new code at end
This is all run from a single client workbook:
Then I used this value as a variable to make a new SQL query:
EDIT: Code for getting field value directly into variable
End Sub
What you would do is set up and save a query that gets the value for you first. Call it MaxID
e.g
Then, in your VBA code, set your variable to that
eg.
There is a function
DMax
that will grab the highest number.The other Domain functions are:
Check with your friendly F1 key for more info