Solved

Access 2007 - Return ID of last record inserted

Posted on 2011-02-25
6
315 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 500 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

809 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