Find last id in a table

J

James Brown

Hi,

From an unbound form I need to run code to find the last value of the
autonumber field in a table. Can anyone help?

James
 
B

BruceM

Or:
DMax("[AutoNumberFieldName]", "TableName")

With an + sign before it, this expression would be the Control Source of a
text box:
=DMax("[AutoNumberFieldName]", "TableName")
 
B

BruceM

That was supposed to be an = sign as shown in the expression, not a + sign
as shown in the description.

BruceM said:
Or:
DMax("[AutoNumberFieldName]", "TableName")

With an + sign before it, this expression would be the Control Source of a
text box:
=DMax("[AutoNumberFieldName]", "TableName")

James Brown said:
Hi,

From an unbound form I need to run code to find the last value of the
autonumber field in a table. Can anyone help?

James
 
J

John Spencer

You can find the MAXIMUM value, but that is not necessarily the LAST value.
Autonumbers can become random under some circumstances and if you add enough
records Autonumbers can become negative.

You have the answers in other responses if you want the MAXIMUM value. The
only way to get the last value would be if you had a datetime field in the
table that was defaulting to NOW(). Then you could look up the record with
the latest (Maximum) datetime value and stand a very good chance of getting
the last autonumber.

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top