Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Access Create a new record then read it

Hi

What is the best way to create a new record in a table then get the ID number created from this?
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Try using a recordset:

Dim rs as DAO.Recordset
Set rs = CurrentDB.OpenRecordset("YourTable")

rs.AddNew
rs!SomeField = "Some value"
rs.Update

MsgBox "New record ID = " & rs!ID

rs.close
Set rs = nothing

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

Thanks
You're welcome!

Just as a note, one advantage to this method as opposed to other methods (Such as DMax or DLast) is that in a multi user database, it ensures that the ID returned is that of the record that was created by the current user and not someone else.
Hi

Thanks very much. I am creating this for a multi user environment and have been looking for a method like this for around a year, so you have done me a great service.
Thanks for that!