Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / March 2008

Tip: Looking for answers? Try searching our database.

Query calculated field question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Victoria - 07 Mar 2008 22:01 GMT
hello

I have a query with fields    ID, Level, a1, a2, a3, a4,......... a40.

The fields a1  to  a40 will either be blank or will have an integer value
from 1 to 5.  I'd like to add a calculated field, called Score,  that will
give me the sum of these 40 columns divided by (5 times the number of fields
that have values).  How should I write this calculated field.

b)  The 40 fields mentioned above have textboxes on a form.  Is it generally
better to just do the above calculation for Score on the form, or get the
calculated value of Score from the query?
Allen Browne - 08 Mar 2008 01:20 GMT
Victoria, can I suggest you take a different approach here?

What you have designed would be fine as a spreadsheet, but it is not right
for Access. In a relational database, you don't put lots of fields in the
one table to hold similar values: you put lots of *records* in a *related*
table.

Whatever ID and Level are, it seems that you could have up to 40 values to
store for them. Drop the fields a1 to a40 from this table. Instead, use
another table with these fields:
   ID               tells which ID of your existing table this record is
for
   TheValue    a Number field to hold the score for this record.
If there is actually some difference between the columns (e.g. if a1 is for
the first week or something), you may need one more field that tells which
is which (e.g. it might hold the numbers 1 to 40.)

Now you can create a query that uses the 2 tables, and see the values in a
column (instead of side by side.) That makes it dead easy to get the
calculation you want. Just use a Total query (depress the Total button on
the toolbar), and enter an expression like this into the field row:
   Sum([TheValue]) / 5 * Count([TheValue])
or perhaps:
   Average([TheValue]) / 5

This concept of a related table that holds many records is really important
in Access.

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.

> hello
>
[quoted text clipped - 10 lines]
> better to just do the above calculation for Score on the form, or get the
> calculated value of Score from the query?
John W. Vinson - 08 Mar 2008 01:27 GMT
>hello
>
[quoted text clipped - 8 lines]
>better to just do the above calculation for Score on the form, or get the
>calculated value of Score from the query?

This might be too big an expression to work, but you could try

(NZ(a1)+NZ(a2)+NZ(a3)+ <etc etc>) / 5*(40 + IsNull([a1]) + IsNull([a2]) +
IsNull([a3]) + <etc etc>)

If that pops an error you may need to write a custom VBA function to return
this value.

If these are fields in a TABLE then your table structure needs
reconsideration. These should be rows, not fields; a Crosstab query based on a
tall-thin normalized table will let you calculate this average.
Signature


            John W. Vinson [MVP]

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.