An SQL statement is the definition of a query, so it can
only be used as a query. Furthermore, the Normalization
rules of Relational databases (not just Access) state that
calculated values such as this should **not** be saved to a
table. If they were and you ever edited a mileage reading
to correct a typo or whatever, the saved calculation would
be incorrect. Therefore, you should (re)do the calculation
whenever you need the value.
Try studying the Access Help chapter (Contents) - Creating
and Working with Databases - About designing a database.
One way I like to summarize those rules is -
If you ever need to change a value anywhere
in any record in any table, you need to be able
to do it by only changing one column, in one row,
in one table.
If you think about that, you'll see that a calculated value
would have to be changed, whenever any of the fields used in
the calulation were changed, so saving calculated values are
a no-no.
Don't forget that all data stored in tables can **only** be
retrieved using a query. This means that queries are an
essential part of every database project. On the other
hand, a query can be used in many different contexts such as
displaying data in a query window (primarily for debugging
purposes), as the Record Source for a form or report, as the
Row Source for the combo box and list box controls, in a VBA
procedure to open a Recordset as well as other situations
that retrieve data. Even when you display a table in sheet
view, access uses an internally created query to do the
work.
--
Marsh
MVP [MS Access]
Spider said:
Thank you for your help. I'm somewhat new with SQL but I'll give it a go
and
see what happens. I'll try to be more informative in the future.
One other thing...can this code be placed in a table, or do I use it with
a
query?
Yes, that's the core of the requirements. I'm sorry if I
seem to be nagging on you, but, for future reference, it
really helps us answer questions if we have a real rather
than oversimplified abstraction of the problem. Another
important bit of information is a list of the pertinate
table fields and their data type. If there are multiple
tables involved, the relationship primary and foreign key
fields are also important.
Lecture's over, here's an air code example of that type of
query:
SELECT T.vehicleID, T.readdate, T.mileage,
T.mileage - (SELECT TOP 1 X.mileage
FROM thetable As X
WHERE X.vehicleID = T.vehicleID
AND X.readdate < T.readdate
ORDER BY X.readdate DESC
) As Difference
FROM thetable As T
ORDER BY T.vehicleID, T.mileage
--
Marsh
MVP [MS Access]
You are correct in assuming the Date field determines the order of the
records. I apologize for the confusion. There could easily be more than
one
vehicle to track. Currently there is only one. Each vehicle is numbered
and
that number is entered into each record.
Does this give you enough to work with?
Each record of the database has a field called "Mileage" that stores
a
numeric value. What I would like to do is take the mileage from the
previous
record and subtract it from the current record for each record
entered.
How
can I do this?
Marshall Barton wrote:
Use a subquery, or, if the query must be updatable, the DMax
function.
Without knowing the table, its fields and what "previous"
means (in terms of fields in each record), I don't think I
can be more specific.
Private email from Spider:
Assume that the database has numerous records with only one field -
Mileage.
Each day the vehicle mileage is entered into a new record. What I
would
like
to be able to do is display on a form the difference between the
current
mileage & the previous days mileage for every entry.
Record 1 - Mileage = 500
Record 2 - Mileage = 750 The difference between record 2 & record 1
is
250.
This difference would be displayed on a form when the record is
selected.
Record 3 - Mileage = 100 The difference between record 2 & record 1
is
250.
This difference would be displayed on a form when the record is
selected.
What I can't figure out is how do I get the value from a previous
record
so
I can use it in the current record? I would like the mileage
difference
to
be calculated automatically each time the user enters the Mileage into
the
new days (current) record.
"Marshall Barton" wrote
Please keep the correspondence in the newsgroups. The delay
in this response is because I don't check that account all
that often and is another good reason to stick to the
newsgroups.
You still haven't defined (in terms of fields in the table)
what "previous" means. Relational databases have no
inherent order to the records, including the order they were
entered. You may have some kind of special situation such
as the mileage number always increases, but your example
does not demonstrate it, probably because of a typo, but I
can't tell for sure. The most common way of determining
"previous" is by having a date field so the order of the
records can be easily determined.
You also failed to mention whether there is a single vehicle
or multiple vehicles. This information is critical to
working out the details of a query to get the value you
want.
Without those details, all I can do is provide an example
based on the extraordinarily simple assumptions you posted:
SELECT T.mileage,
T.mileage - (SELECT Max(X.mileage)
FROM thetable As X
WHERE X.mileage < T.mileage
) As Difference
FROM thetable As T
ORDER BY T.mileage