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?
5 2627 MMcCarthy 14,534
Recognized Expert Moderator MVP -
Private Sub ButtonName_Click()
-
Dim count As Integer
-
Dim TotalValue
-
-
count = 0
-
TotalValue = 0
-
If nz(Me![Field1],0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me![Field1]
-
ElseIf nz(Me![Field2],0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me![Field2]
-
ElseIf nz(Me![Field3],0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me![Field3]
-
ElseIf nz(Me![Field4],0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me![Field4]
-
End If
-
-
Me!AvgValue = TotalValue / count
-
-
End Sub
-
I'm sure theres a more efficient way of doing this but can't think of one at the moment.
Mary
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. -
Private Sub AVERAGELE_Click()
-
Dim count As Integer
-
Dim TotalValue
-
count = 0
-
TotalValue = 0
-
If Nz(Me.Ctl21st, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.Ctl21st
-
ElseIf Nz(Me.AVS, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.AVS
-
ElseIf Nz(Me.Fasano, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.Fasano
-
ElseIf Nz(Me.EMSI, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.EMSI
-
ElseIf Nz(Me.ISC, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.ISC
-
End If
-
Me.AVERAGELE = TotalValue / count
-
End Sub
-
-
Private Sub ButtonName_Click()
-
Dim count As Integer
-
Dim TotalValue
-
-
count = 0
-
TotalValue = 0
-
If nz(Me![Field1],0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me![Field1]
-
ElseIf nz(Me![Field2],0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me![Field2]
-
ElseIf nz(Me![Field3],0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me![Field3]
-
ElseIf nz(Me![Field4],0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me![Field4]
-
End If
-
-
Me!AvgValue = TotalValue / count
-
-
End Sub
-
I'm sure theres a more efficient way of doing this but can't think of one at the moment.
Mary
MMcCarthy 14,534
Recognized Expert Moderator MVP
Sorry I shouldn't do things when I'm tired. Try this ... -
Private Sub AVERAGELE_Click()
-
Dim count As Integer
-
Dim TotalValue
-
count = 0
-
TotalValue = 0
-
-
If Nz(Me.Ctl21st, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.Ctl21st
-
End If
-
-
If Nz(Me.AVS, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.AVS
-
End If
-
-
If Nz(Me.Fasano, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.Fasano
-
End If
-
-
If Nz(Me.EMSI, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.EMSI
-
End If
-
-
If Nz(Me.ISC, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.ISC
-
End If
-
-
Me.AVERAGELE = TotalValue / count
-
End Sub
-
Mary
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 ... -
Private Sub AVERAGELE_Click()
-
Dim count As Integer
-
Dim TotalValue
-
count = 0
-
TotalValue = 0
-
-
If Nz(Me.Ctl21st, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.Ctl21st
-
End If
-
-
If Nz(Me.AVS, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.AVS
-
End If
-
-
If Nz(Me.Fasano, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.Fasano
-
End If
-
-
If Nz(Me.EMSI, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.EMSI
-
End If
-
-
If Nz(Me.ISC, 0) <> 0 Then
-
count = count + 1
-
TotalValue = TotalValue + Me.ISC
-
End If
-
-
Me.AVERAGELE = TotalValue / count
-
End Sub
-
Mary
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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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:
|
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
|
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!
|
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...
|
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.
| |
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.
|
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...
|
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
|
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
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |