Look up most recent value from the most recent record (1 Viewer)

ddewald

New member
Local time
Today, 18:45
Joined
Jan 31, 2020
Messages
19
Hello all,

I have a database that tracks mileage and fuel for our fleet of trucks. We have a paper trip report that gets entered into an access database.

On the form "frm_TripReports" there is an "OdometerStart" and an "OdometerEnd". Each trip report is specific to each Vehicle. Vehicle is defined by "LicensePlate".

Is there a way to start each entry with the "OdometerStart" auto filled with the most recent record's "OdometerEnd"? "LicensePlate" specific.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:45
Joined
Oct 29, 2018
Messages
21,558
Hi. When discussing computer processes, "most recent" must have a logical sequence. Does your table store the date and time when records are entered? If so, would you consider these values in proper sequence. Otherwise, would you simply consider "most recent" as the one with the "highest" value?
 

ddewald

New member
Local time
Today, 18:45
Joined
Jan 31, 2020
Messages
19
Hi. When discussing computer processes, "most recent" must have a logical sequence. Does your table store the date and time when records are entered? If so, would you consider these values in proper sequence. Otherwise, would you simply consider "most recent" as the one with the "highest" value?

I would say the most recent would be the one with the highest value. Odometer never goes down so that's probably a safe bet.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:45
Joined
Oct 29, 2018
Messages
21,558
I would say the most recent would be the one with the highest value. Odometer never goes down so that's probably a safe bet.
In that case, you could try using DMax().
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:45
Joined
May 21, 2018
Messages
8,613
Something like.
Code:
Public Function GetMaxOdometer(LicenseID as string) as long
   GetMaxOdometer = dmax("OdometerEnd","SomeTable","License = '" & LicenseID & "'")
end function

this you can test in the immediate window to make sure it works
?GetMaxOdometer("xyz-123")
See if you get the answer.

Then you can use this on the forms on current event to set the default value of the OdometerStart or set actual value.
me.OdometerStart.defaultvalue = getMaxOdometer(Me.license)
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:45
Joined
Oct 29, 2018
Messages
21,558
So would I do a DLookup to find records by license plate and then a DMax to narrown down to highest Odometer value?
Hi. Sorry for the delay. See the function MajP provided above. The DMax() function allows for a criteria to narrow down the scope of what you're searching in the data. Cheers!
 

ddewald

New member
Local time
Today, 18:45
Joined
Jan 31, 2020
Messages
19
Something like.
Code:
Public Function GetMaxOdometer(LicenseID as string) as long
   GetMaxOdometer = dmax("OdometerEnd","SomeTable","License = '" & LicenseID & "'")
end function

this you can test in the immediate window to make sure it works
?GetMaxOdometer("xyz-123")
See if you get the answer.

Then you can use this on the forms on current event to set the default value of the OdometerStart or set actual value.
me.OdometerStart.defaultvalue = getMaxOdometer(Me.license)
Please excuse my ignorance. Where would I put that function in at?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:45
Joined
May 21, 2018
Messages
8,613
I would put the function in a standard module so you can call it from anywhere.
How is your form set up. I am guessing it might be a main form with "License" on the main form and a subform with in and out trips?

If that is the case then you could do something like this in the subform on current event
me.OdometerIn.defaultvalue = getMaxOdometer(me.parent.license)

this assumes you have a field on the main form called license
 

Users who are viewing this thread

Top Bottom