Solved

Access 2007 - Return ID of last record inserted

Posted on 2011-02-25
6
310 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
Comment Utility


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

Expert Comment

by:Jeffrey Coachman
Comment Utility
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
Comment Utility
What about Transactions?  How do a perform a Transaction?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
<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
Comment Utility
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
Comment Utility
<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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now