Calculating

G

Guest

How do I have my table automatically calculate an average of 3 different
fields in my database? I try typing an expression:
([field1]+[field2]+[field3])/3 inside the field like i would in excel. ( I
know this isnt excel) But how do I make that Average Field Column
Automatically calculate an average.?
 
D

Danny J. Lesandrini

You don't have the table calculate anything. Use a query for calculations.

SELECT *, ([field1]+[field2]+[field3])/3 As MyAvgCalc
FROM MyTable
 
J

John Vinson

How do I have my table automatically calculate an average of 3 different
fields in my database? I try typing an expression:
([field1]+[field2]+[field3])/3 inside the field like i would in excel. ( I
know this isnt excel) But how do I make that Average Field Column
Automatically calculate an average.?

You can't, not in a Table. That's not what tables are for.

But you can do so easily, using a Query based on your table. In a
vacant Field cell in the Query, type the expression, just as you have
it; or - if there may be NULL values in the fields - use

(NZ([Field1]) + NZ([Field2]) + NZ([Field3]) / (3 + IsNull([Field1]) +
IsNull([Field2] + IsNull([Field3]))

to average just the non-NULL values. (Yes, I know the denominator is
excessively tricky...)


John W. Vinson[MVP]
 
G

Guest

Also I have tried to creat a query where I place my formula (expression) in
to the field so it looks like this Average: =([field1]+[field2]+[field3])/3
but then when i go to run my report I get the message ENTER Parameter Value
box with Vendor.Average.
 
G

Guest

Ok now my report i want to sort my vendors by the average but not move my
average column to the far left of my Vendors name report table.

John Vinson said:
How do I have my table automatically calculate an average of 3 different
fields in my database? I try typing an expression:
([field1]+[field2]+[field3])/3 inside the field like i would in excel. ( I
know this isnt excel) But how do I make that Average Field Column
Automatically calculate an average.?

You can't, not in a Table. That's not what tables are for.

But you can do so easily, using a Query based on your table. In a
vacant Field cell in the Query, type the expression, just as you have
it; or - if there may be NULL values in the fields - use

(NZ([Field1]) + NZ([Field2]) + NZ([Field3]) / (3 + IsNull([Field1]) +
IsNull([Field2] + IsNull([Field3]))

to average just the non-NULL values. (Yes, I know the denominator is
excessively tricky...)


John W. Vinson[MVP]
 
J

John Vinson

Ok now my report i want to sort my vendors by the average but not move my
average column to the far left of my Vendors name report table.

The average column SHOULD NOT BE IN the table. Period.

Storing derived data such as this in your table accomplishes
three things: it wastes disk space; it wastes time (almost
any calculation will be MUCH faster than a disk fetch); and
most importantly, it risks data corruption. If one of the
underlying fields is subsequently edited, you will have data
in your table WHICH IS WRONG, and no automatic way to detect
that fact.

Just redo the calculation whenever you need it, either as a
calculated field in a Query or just as you're now doing it -
in the control source of a Form or a Report textbox.

You can calculate the average in the Query by putting

Average: ([field1]+[field2]+[field3])/3

in a vacant Field cell, anywhere within the query. The order of fields
in a query has NOTHING to do with the order of textboxes on a report,
so it doesn't matter whether the average is the leftmost, rightmost,
or somewhere in the middle - simply calculate or retrieve the fields
using the Query (in any order you want), and display them on your
Report in textboxes (in any order or position that you want). You
should use this calculated field in the Report's Sorting and Grouping
dialog; it's not necessary to sort the Query.

John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top