
Signature
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
> Allen et al,
>
[quoted text clipped - 54 lines]
>> I apologize for this lengthy request. Thanks, in advance for any
>> suggestions for this problem.
Okay, you are on the right track by having LastBattery as a calculated
field, i.e. it is not stored in the table, but calculated as needed, so that
if another record is inserted it, will update automaticallly.
The piece of the puzzle that looks to be missing is a date/time field that
stores the date and time of the battery change. This lets you add another
record later (which probably gets a higher ID value, but still specify that
the record fits between 2 others based on the date/time.
If that field is named "ChangeDateTime", you could create a query and type
something like this into the Field row to get the calculated field named
"LastBattery":
LastBattery: ELookup("Battery", "MyTable", "([Vehicle] = """ & [Vehicle] &
") AND ([ChangeDateTime] < " & Format([ChangeDateTime], "\#mm\/dd\/yyyy\#")
& ")", "[ChangeDateTime] DESC")
Note that the requirement to order records differently than the ID field
means DLookup() is not up to the task, and you need the ELookup() from this
link:
http://allenbrowne.com/ser-42.html
Replace "MyTable" with the name of your table.
Another approach would be to use a subquery. This will be way faster, but
will give read-only results. The expression to type into your quey will be
something like this:
LastBattery: (SELECT TOP 1 Battery FROM MyTable AS Dupe WHERE (Dupe.Vehicle
= MyTable.Vehicle) AND (Dupe.ChangeDateTime < MyTable.ChangeDateTime) ORDER
BY Dupe.ChangeDateTime, Dupe.ID)

Signature
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
> Allen et al,
>
[quoted text clipped - 54 lines]
>> I apologize for this lengthy request. Thanks, in advance for any
>> suggestions for this problem.
Dan Johnson - 22 May 2005 19:47 GMT
Allen,
I already have another query that orders the list of vehicles down to the
current record's vehicle. such that the query results are as follows based
on the Table data in the original post:
ID Vehicle Battery Last Battery
101 002 028 006
104 002 004
Having said that, I need the Last Battery field populated with Battery 028
from the battery that should be in the vehicle currently. Am I trying to
make this too difficult?

Signature
Dan Johnson
> Okay, you are on the right track by having LastBattery as a calculated
> field, i.e. it is not stored in the table, but calculated as needed, so
[quoted text clipped - 84 lines]
>>> I apologize for this lengthy request. Thanks, in advance for any
>>> suggestions for this problem.
Allen Browne - 23 May 2005 02:47 GMT
Yes: you are making it too difficult by trying to store the value instead of
asking Access to get it when you need it.
If you try to store it, you must respond to every insert, edit, or delete,
to ensure that every other entry is still correct. By using the calculated
field as indicated, the results can never be wrong. (One of the basic rules
of data design is not to store dependent data. For more info, search on
"normalization.")

Signature
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
> Allen,
>
[quoted text clipped - 99 lines]
>>>> I apologize for this lengthy request. Thanks, in advance for any
>>>> suggestions for this problem.