473,803 Members | 3,943 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Average of Multiple Fields

83 New Member
I have four fields in my table that I want to average providing the fields have a value, not the null, for each case(id). I also have an average field in my table. I have a form for the table and I want the average value to be populated on click with the average of the four fields. How can I do this?
Mar 28 '07 #1
5 2627
MMcCarthy
14,534 Recognized Expert Moderator MVP
Expand|Select|Wrap|Line Numbers
  1. Private Sub ButtonName_Click()
  2. Dim count As Integer
  3. Dim TotalValue
  4.  
  5.    count = 0
  6.    TotalValue = 0
  7.    If nz(Me![Field1],0) <> 0 Then
  8.       count = count + 1
  9.       TotalValue = TotalValue + Me![Field1]
  10.    ElseIf nz(Me![Field2],0) <> 0 Then
  11.       count = count + 1
  12.       TotalValue = TotalValue + Me![Field2]
  13.    ElseIf nz(Me![Field3],0) <> 0 Then
  14.        count = count + 1
  15.        TotalValue = TotalValue + Me![Field3]
  16.    ElseIf nz(Me![Field4],0) <> 0 Then
  17.        count = count + 1
  18.        TotalValue = TotalValue + Me![Field4]
  19.    End If
  20.  
  21.    Me!AvgValue = TotalValue / count
  22.  
  23. End Sub
  24.  
I'm sure theres a more efficient way of doing this but can't think of one at the moment.

Mary
Mar 29 '07 #2
jl2886
83 New Member
Thanks Mary, but this doesn't work. It only reports the first Field value. I think it is because the code reads If Field 1 not null then count=1 and Total value=Field1 and ends there. Thus, if field 1 is empty, then it yields the field 2 value. Maybe I didn't explain myself right. I want it to average all the values, but only if the value is not null.
Here is the code with my variables, with mmcarthy's code.
Expand|Select|Wrap|Line Numbers
  1. Private Sub AVERAGELE_Click()
  2. Dim count As Integer
  3. Dim TotalValue
  4.    count = 0
  5.    TotalValue = 0
  6.    If Nz(Me.Ctl21st, 0) <> 0 Then
  7.       count = count + 1
  8.       TotalValue = TotalValue + Me.Ctl21st
  9.    ElseIf Nz(Me.AVS, 0) <> 0 Then
  10.       count = count + 1
  11.       TotalValue = TotalValue + Me.AVS
  12.    ElseIf Nz(Me.Fasano, 0) <> 0 Then
  13.       count = count + 1
  14.       TotalValue = TotalValue + Me.Fasano
  15.    ElseIf Nz(Me.EMSI, 0) <> 0 Then
  16.       count = count + 1
  17.       TotalValue = TotalValue + Me.EMSI
  18.     ElseIf Nz(Me.ISC, 0) <> 0 Then
  19.       count = count + 1
  20.       TotalValue = TotalValue + Me.ISC
  21.    End If
  22.    Me.AVERAGELE = TotalValue / count
  23. End Sub
  24.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub ButtonName_Click()
  2. Dim count As Integer
  3. Dim TotalValue
  4.  
  5.    count = 0
  6.    TotalValue = 0
  7.    If nz(Me![Field1],0) <> 0 Then
  8.       count = count + 1
  9.       TotalValue = TotalValue + Me![Field1]
  10.    ElseIf nz(Me![Field2],0) <> 0 Then
  11.       count = count + 1
  12.       TotalValue = TotalValue + Me![Field2]
  13.    ElseIf nz(Me![Field3],0) <> 0 Then
  14.        count = count + 1
  15.        TotalValue = TotalValue + Me![Field3]
  16.    ElseIf nz(Me![Field4],0) <> 0 Then
  17.        count = count + 1
  18.        TotalValue = TotalValue + Me![Field4]
  19.    End If
  20.  
  21.    Me!AvgValue = TotalValue / count
  22.  
  23. End Sub
  24.  
I'm sure theres a more efficient way of doing this but can't think of one at the moment.

Mary
Mar 29 '07 #3
MMcCarthy
14,534 Recognized Expert Moderator MVP
Sorry I shouldn't do things when I'm tired. Try this ...
Expand|Select|Wrap|Line Numbers
  1. Private Sub AVERAGELE_Click()
  2. Dim count As Integer
  3. Dim TotalValue
  4.    count = 0
  5.    TotalValue = 0
  6.  
  7.    If Nz(Me.Ctl21st, 0) <> 0 Then
  8.       count = count + 1
  9.       TotalValue = TotalValue + Me.Ctl21st
  10.    End If
  11.  
  12.    If Nz(Me.AVS, 0) <> 0 Then
  13.       count = count + 1
  14.       TotalValue = TotalValue + Me.AVS
  15.    End If
  16.  
  17.    If Nz(Me.Fasano, 0) <> 0 Then
  18.       count = count + 1
  19.       TotalValue = TotalValue + Me.Fasano
  20.    End If
  21.  
  22.    If Nz(Me.EMSI, 0) <> 0 Then
  23.       count = count + 1
  24.       TotalValue = TotalValue + Me.EMSI
  25.    End If
  26.  
  27.     If Nz(Me.ISC, 0) <> 0 Then
  28.       count = count + 1
  29.       TotalValue = TotalValue + Me.ISC
  30.    End If
  31.  
  32.    Me.AVERAGELE = TotalValue / count
  33. End Sub
  34.  
Mary
Mar 29 '07 #4
jl2886
83 New Member
Hhahaha, I ended up figuring it out and doing the same thing as you.

Sorry I shouldn't do things when I'm tired. Try this ...
Expand|Select|Wrap|Line Numbers
  1. Private Sub AVERAGELE_Click()
  2. Dim count As Integer
  3. Dim TotalValue
  4.    count = 0
  5.    TotalValue = 0
  6.  
  7.    If Nz(Me.Ctl21st, 0) <> 0 Then
  8.       count = count + 1
  9.       TotalValue = TotalValue + Me.Ctl21st
  10.    End If
  11.  
  12.    If Nz(Me.AVS, 0) <> 0 Then
  13.       count = count + 1
  14.       TotalValue = TotalValue + Me.AVS
  15.    End If
  16.  
  17.    If Nz(Me.Fasano, 0) <> 0 Then
  18.       count = count + 1
  19.       TotalValue = TotalValue + Me.Fasano
  20.    End If
  21.  
  22.    If Nz(Me.EMSI, 0) <> 0 Then
  23.       count = count + 1
  24.       TotalValue = TotalValue + Me.EMSI
  25.    End If
  26.  
  27.     If Nz(Me.ISC, 0) <> 0 Then
  28.       count = count + 1
  29.       TotalValue = TotalValue + Me.ISC
  30.    End If
  31.  
  32.    Me.AVERAGELE = TotalValue / count
  33. End Sub
  34.  
Mary
Mar 29 '07 #5
MMcCarthy
14,534 Recognized Expert Moderator MVP
Hhahaha, I ended up figuring it out and doing the same thing as you.
It feels good to figure it out though :)
Mar 29 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3260
by: CSDunn | last post by:
Hello, I have 14 fields on a report that hold integer values. The field names use the following naming convention: T1Number, T2Number ....T14Number. I need to get a 'sub total' of all fields as follows: =Sum() ... =Sum() Then I need to get an average of all fields as follows:
3
1796
by: Tony Lennard | last post by:
I have several queries, which generate about 10 fields each a text field of length 2 (which contain effort and attainment grades), eg A2, B4, A3, A3. I am trying to calculate an 11th fields, which is an average of the other fields (in this case A3). Does anyone have any ideas? Tony Lennard
1
1859
by: edhead | last post by:
Access newbie here, I am trying to figure out how to set up a query that will return an average from multiple records all created on the same date. Table has the following fields date, weight1, weight2. There are 30-40 records per day and I would like to be able to have the query find like dates (without having to enter a date) and give me an average for all of the weight1 and weight2 records. Is this possible? Thanks!
2
8418
by: David Ziger | last post by:
With mysql 4.1.11, is there a way to compile the averages and standard deviations across multiple fields? Field1 Field2 Field3 Want to be able to create a new column: SELECT t.Field1, t.Field2, t.Field3, mean(Field1, Field2, Field3) as CalcMean FROM mytable as t; There are GREATEST and LEAST functions for taking the maximum and minimum, but no analog for average and standard deviation that I can find. It appears I would need to...
1
2581
by: amit | last post by:
Hi guys I am using access database with Crystal Reports in VS2003 I am working on "Hospital Database" The fields are - Name - Date - ConsultingCharge - TotalFees i.e. Total Fees include fees for medicine issued by hospital In the report I want to display the Average of ConsultingCharge and TotalFees for every record.
2
13563
by: chrisale | last post by:
Hi All, I've been racking my brain trying to figure out some sort of Sub-Select mySQL statement that will create a result with multiple rows of averaged values over a years time. What I have is weather data. There is a new record every 5 minutes, every day. So. What I want to do with one SQL statement is figure out the Average of those 5 minute records over each day, for every day of the year.
5
16271
abouddan
by: abouddan | last post by:
Hi all I am working on an accounting project using MS Access 2000, that demands to calculate many fields in a spesific record. The problem: The query I am using returns many records and for each record 3 fields, let's say Data1, Data2, Data3. In the form related to this query I have 5 text boxes.The three text boxes are bound to the query. The two other text boxes are for displaying calculations, the first one displays the sum of the three...
1
3535
by: wetsprockets | last post by:
Hello, I have two tables, WindVector (wind data) and DOH_FC88to07 (water quality samples). The WindVector table has the following fields: Date (short date) Time (short time) WindSpeed WindDir WindDirRads V
2
6377
by: ReneHernandez | last post by:
Good afternoon, I am trying to calculate an average over multiple fields. I've tried using the following =Avg(IIF( = "NA",Null, Val())) + Avg(IIF( = "NA",Null, Val())) + Avg(IIF( = "NA",Null, Val())) The above, , , etc. are text fields pertaining to an evaluation rating scale: NA 1 2 3 4 5
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10548
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.