On 30 Jul 2004 19:56:25 -0700, John young wrote:
I have a table which has fields "year", "odometer", and "annual"....
what i currently do is enter manually data for all fields,... what i
want to have happen is the annual figure to be added automatically by
subtracting the previous year odo reading from current year reading
and haveing that value placed in "annual" field
Can any one assist with this ???? I am an "intermediate" user
Access Version is XP 2000
You have a couple of things you need to 'fix' first.
1) You have a field named "Year".
Year is a reserved word in Access/vba and should not be used as a
field name.
See the appropriate Microsoft Knowledgebase article:
109312 'Reserved Words in Microsoft Access'
209187 'Acc2000: 'Reserved Words in Microsoft Access'
286335 'ACC2002: Reserved Words in Microsoft Access'
Change the name of the Year field to something else.
In my example below I named it "ForYear".
2) All you need to save in your table for this is the Year and the
Odometer reading. You do not need the "Annual" field. The annual
mileage is a calculated field and need not be saved in any table.
Whenever you need the mileage for any given year, simply run the
calculation.
3) To calculate the mileage for the years in the table, here is a
query SQL which will do so.
SELECT YourTable.ForYear, YourTable.Odometer,
Abs(DMax("[Odometer]","YourTable","[ForYear]<" &
[ForYear])-[Odometer]) AS AnnualMileage
FROM YourTable;
The above assumes ForYear and [Odometer] are number datatype with Year
being an Integer and Odometer a Long Integer Size.
You can also use Text datatypes for those 2 fields, but then the SQL
needs a small change.
Here is a sample of the data returned
ForYear Odometer AnnuaMileage
1999 15236
2000 28237 13001
2001 42856 14619
2002 62123 19267
2003 78123 16000
If you would rather have the latest year on top, Order By Descending.
--
Fred
Please only reply to this newsgroup.
I do not reply to personal email.