Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access 2007 - Return ID of last record inserted

Posted on 2011-02-25
6
Medium Priority
?
323 Views
Last Modified: 2012-05-11
I need a way to return the ID of the last record inserted into a table.  I've found a few places out there with commentary on this, but I'm not sure what the best practice is to make this happen.

Are there transactions in access?  Should I include the insert and the ID retrieval as part of the same transaction so I can be sure I'm getting the correct ID back?
0
Comment
Question by:JosephEricDavis
  • 4
  • 2
6 Comments
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 2000 total points
ID: 34981209


=Dmax("ID","YourTable")
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 34981244
Now this is a bit tricky because my above post assumes that the ID field is an autonumber field.

I'll explain....

A table does not really know what record is "Last"
The "last record" will depend on how the table is sorted.
It can also mean the record with the most recent Day/Time stamp.

So you will have to clearly define "Last Record"
0
 
LVL 7

Author Comment

by:JosephEricDavis
ID: 34981273
What about Transactions?  How do a perform a Transaction?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 34982364
<What about Transactions?  How do a perform a Transaction?>
I'm confused?
I thought the one question here was determining the Last Record?

"Transaction" as they are known in SQL Server, do not exist in MS Access.
You can Access transactions via Access SQL (TSQL variant)
http://msdn.microsoft.com/en-us/library/bb208950%28v=office.12%29.aspx
http://msdn.microsoft.com/en-us/library/sx0e0xze.aspx
...I am sure you can google mor hits depending on your exact needs.

JeffCoachman
0
 
LVL 7

Author Comment

by:JosephEricDavis
ID: 34998613
Actually Access does have transactions, as can be seen here.
http://msdn.microsoft.com/en-us/library/bb243806%28v=office.12%29.aspx

I tried them and they work as just as one might think transactions work.  So I can't give you points for your response on that part of my question.  I can just give you points for the =Dmax("ID","YourTable") comment.

Thanks
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 34999612
<Actually Access does have transactions,>
This is more of a semantics issue...
Transaction in DAO, yes, but not in Access directly.
For example there are no "Transaction" properties or setting in Access (forms) directly.

It all depends on what you are referring to specifically when you say "Access"
As the link states:
"The Microsoft Access database engine supports transactions through the DAO"

So like A 2005 Ford Taurus will "Support" Turbo/supercharging, remember that Turbo/supercharging it is not offered with the car as standard.

See the note in the link for info that Transactions in Access DAO are not the same as Transactions in a true Client/Server architecture.
So if you are looking some specific Translation functionality, it may not be the same in Access...

;-)

JeffCoachman
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question