Last record in a table

latex88

Registered User.
Local time
Today, 17:36
Joined
Jul 10, 2003
Messages
198
This seems easy enough, but I'm not sure why when I test it in the Immediate window, I get value of 0, when I know it should return something like 386.

Basically, I want to set A public variable to reflect the highest ID (OrderID is the field name) in TABLE, tblOrderID. My code is below.


Public Function ID_lookup()

LastID = DMax("OrderID", "tblOrderID")

End Function


When I test it, it returns 0. See below. What am I doing wrong?


Immediate
___________________________________________________________
? LastID
0
 
Is OrderID a text or number in your table. check the properties in your table.
 
If OrderId is a number, try the following

Code:
Public Function ID_lookup() as Long

Id_Lookup= DMax("OrderID", "tblOrderID")

End Function


In immediate window
?Id_lookup

Caution: There is no guarantee that this will be incremental to the previous DMax value if OrderId is an autonumber. Since autonumbers are unique (but not necessarily incremental/sequential)
 
Last edited:
I goofed with my last answer. In order for LastID to have a value you would have had to run the function first, the way you had it laid out. But doing it the way jdrew did the sample is how I would have done it.
 

Users who are viewing this thread

Back
Top Bottom