473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

calculated fields in tables/ queries

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
Nov 13 '05 #1
10 2422
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.
Nov 13 '05 #2

Fredg
thanks for that ....I have made changes to table/fieldsand modified your
sql example to fit ....but i get a completly wrong result.. one thinh i
did not mention is the original table has a Vehicle.ID field as there
are a number of different vehicles that data is being held for ( a
vintage car club)
here is my sql with additional selection material to select a car ..id
328

SELECT YourTable.VehicleID, YourTable.period, YourTable.Odometer,
Abs(DMax("[Odometer]","YourTable","[Period]<" & [Period])-[Odometer])
AS Annual
FROM YourTable
WHERE (((YourTable.VehicleID)=328))
ORDER BY YourTable.period DESC;

Here are the results as per above

VehicleID period Odometer Annual
328 2004 482 571330
328 2003 334 571478
328 2002 99882 471930
328 2001 97111 474701
328 2000 96411 3145
328 1999 95647

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #3

Fredg
thanks for that ....I have made changes to table/fieldsand modified your
sql example to fit ....but i get a completly wrong result.. one thinh i
did not mention is the original table has a Vehicle.ID field as there
are a number of different vehicles that data is being held for ( a
vintage car club)
here is my sql with additional selection material to select a car ..id
328

SELECT YourTable.VehicleID, YourTable.period, YourTable.Odometer,
Abs(DMax("[Odometer]","YourTable","[Period]<" & [Period])-[Odometer])
AS Annual
FROM YourTable
WHERE (((YourTable.VehicleID)=328))
ORDER BY YourTable.period DESC;

Here are the results as per above

VehicleID period Odometer Annual
328 2004 482 571330
328 2003 334 571478
328 2002 99882 471930
328 2001 97111 474701
328 2000 96411 3145
328 1999 95647
As you can see ...not quiet rite

Where Have I Gone Wrong

P.S Ignore previous reply as appears i hit wrong button and sent only
part message

Cheers
John


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #4
fredg
should have mentioned that your original reply works well if only
one vehicle is involved but we have lots of vehicles ID's
cheers
John

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #5
Just add the VehicleID to the DMax() function's criteria. E.g.:

Abs(DMax("[Odometer]","YourTable","[Period]<" & [Period] & " AND
VehicleID=" & [VehicleID]))

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
John Young wrote:
Fredg
thanks for that ....I have made changes to table/fieldsand modified your
sql example to fit ....but i get a completly wrong result.. one thinh i
did not mention is the original table has a Vehicle.ID field as there
are a number of different vehicles that data is being held for ( a
vintage car club)
here is my sql with additional selection material to select a car ..id
328

SELECT YourTable.VehicleID, YourTable.period, YourTable.Odometer,
Abs(DMax("[Odometer]","YourTable","[Period]<" & [Period])-[Odometer])
AS Annual
FROM YourTable
WHERE (((YourTable.VehicleID)=328))
ORDER BY YourTable.period DESC;

Here are the results as per above

VehicleID period Odometer Annual
328 2004 482 571330
328 2003 334 571478
328 2002 99882 471930
328 2001 97111 474701
328 2000 96411 3145
328 1999 95647


Nov 13 '05 #6

MGFoster
.... thanks for your reply but am unable to get that ammendment to work
either..... it gived me a zero ( 0 ) in the calculated field.... have
looked at the Dmax() Method and can not get it to work as suggested
.....

Cheers
John
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #7
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Well I should have read the original query w/ more attention. Instead
of DMAX() it should be using the DSUM(), and, the Period comparison
should be less than or equal to (<=), and, the Abs() function isn't
needed [presumably, the odometer reading will always be a positive
number]. Try this:

SELECT VehicleID, [Period], Odometer, DSum("[Odometer]", "YourTable",
"[Period] <= " & [Period] & " AND VehicleID=" & [VehicleID]) AS Annual
FROM YourTable
ORDER BY VehicleID, [Period] DESC

This assumes Period and VehicleID are both numeric data types.

This query will sum the Odometer for the current period & all previous
periods for each VehicleID in the table. This is what's know as a
running-sum query. Here is a MS KB article that explains:

http://support.microsoft.com/support.../q138/9/11.asp

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQQ6Hq4echKqOuFEgEQJocACguD4+dOEqy7o5x/Ix1ejNzCebeF4An30p
j9MlhvEn61V0NTl35VAmrQxX
=oYjk
-----END PGP SIGNATURE-----

John Young wrote:
MGFoster
... thanks for your reply but am unable to get that ammendment to work
either..... it gived me a zero ( 0 ) in the calculated field.... have
looked at the Dmax() Method and can not get it to work as suggested
....


Nov 13 '05 #8
MGFoster ...

your amendment works for giving running totals as you describe. but
what i am look ing for is annual total ie difference between this
years and last years figure
i.e. 2003 year was 40000 LAST year was 35000 giving a "annual total" of
5000....
...as you assumed all data is numeric..

I have tried various formula / criteria and have so far got no
satisfactory result ....

NOTE... need for this figure is for annual transportation department
returns for each vehicle
cheers ..and thanks for your assistance
John


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #9
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ah... Maybe one of these (these qrys assume there is only one odometer
reading per period):

SELECT VehicleID, [Period], Odometer, [Odometer] - DLookup("[Odometer]",
"YourTable", "[Period] = " & [Period]-1 & " AND VehicleID=" &
[VehicleID]) AS Annual
FROM YourTable
ORDER BY VehicleID, [Period] DESC

Same version using a subquerey (might be faster 'cuz can use indexes):

SELECT VehicleID, [Period], Odometer,
Odometer - (SELECT Odometer FROM YourTable WHERE Period = T.Period-1 AND
VehicleID = T.VehicleID) AS Annual
FROM YourTable As T
ORDER BY VehicleID, [Period] DESC

If the periods are not sequential, try this:

SELECT VehicleID, [Period], Odometer,
Odometer - (SELECT Odometer FROM YourTable WHERE
Period = (SELECT MAX(Period) FROM YourTable WHERE Period < T.Period)
AND VehicleID = T.VehicleID) AS Annual
FROM YourTable As T
ORDER BY VehicleID, [Period] DESC

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQQ/W0oechKqOuFEgEQLLwwCgmN1Q9nNb0q3ampd7G+NsRlHMEqMAo P0W
yrK4hG0fiRpFvrfhcsSx2hYl
=Y5fn
-----END PGP SIGNATURE-----
John Young wrote:
MGFoster ...

your amendment works for giving running totals as you describe. but
what i am look ing for is annual total ie difference between this
years and last years figure
i.e. 2003 year was 40000 LAST year was 35000 giving a "annual total" of
5000....
...as you assumed all data is numeric..

I have tried various formula / criteria and have so far got no
satisfactory result ....

NOTE... need for this figure is for annual transportation department
returns for each vehicle


Nov 13 '05 #10
MGFoster ..

Success at last ...for a moment there i thought it mite be in the
two hard basket ..but your solution is just what i was looking for
.....I dont do a lot of work in access so when i need to i am not
"expert" enough to know all the finer 'hidden' solutions.... It is good
to know there is help close to hand....

Mant thanks for your assistance.....

Cheers

John Y

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SJH | last post by:
I have been given an older database and asked to make upgrades and what not. One interesting thing I have come across with the database is that it was at one time set up so one of the tables would...
5
by: John Bahran | last post by:
I am trying to use calculated fields in my query but all the results are zero ven when they're not. Please help. Thanks.
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
1
by: svdh | last post by:
I have posed a question last saturday and have advanced alot in the meantime. But I am still not there Problem is that I try to merging various fields from various tables in one document in Word...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
9
by: Mohd Al Junaibi | last post by:
Hello all, my first post here...hope it goes well. I'm currently working on stored procedure where I translated some reporting language into T-SQL The logic: I have a group of tables...
2
by: jcf378 | last post by:
hi all. I have a form which contains a calculated control ("days") that outputs the # of days between two dates (DateDiff command between the fields and ). However, when I click "Filter by...
3
NatronA111
by: NatronA111 | last post by:
I am having a problem with calculated fields (...the most important one in the database!) in an MS Access query that pulls info from one table and a few other calculated fields stored in other...
3
by: jonceramic | last post by:
Hi All, I need to know the best way to set up a datawarehouse/materialized view for doing statistics/graphs in Access. My crosstabs and unions are getting too complicated to crunch in real...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.