Solved

Access 2007 - Return ID of last record inserted

Posted on 2011-02-25
6
312 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

777 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