Sheesh I am just full of bad advice today. Truly sorry!!!! The function as
I had written it will not work properly. Here is a replacement that I have
actually checked for correct operation.
Public Function Ave3Fields(fld1 As Variant, fld2 _
As Variant, fld3 As Variant) As Variant
Dim varTotal As Variant, intCount As Integer
intCount = 3
If IsNull(fld1) Then intCount = intCount - 1
If IsNull(fld2) Then intCount = intCount - 1
If IsNull(fld3) Then intCount = intCount - 1
varTotal = CDbl(Nz(fld1, 0)) + CDbl(Nz(fld2, 0)) _
+ CDbl(Nz(fld3, 0))
If intCount = 0 Then
Ave3Fields = Null
Else
Ave3Fields = varTotal / intCount
End If
End Function
Just paste the above function in the code module of your form OUTSIDE of any
other function or sub. Watch out for any lines that the newsreader wraps.
After pasting there should be no red text. Or if you prefer you can go
ahead and put it in a global module. Makes no difference.
Then in the Property Sheet of the Text Box that is showing the average, set
Control Source to =Ave3Fields([text1],[text2],[text3]) Where you replace the
text1 text2 text3 with the names of the text boxes on your form. Leave the
square brackets in place!
Access can get confused when the name of the object (text box in this case)
is the same name as the underlying field it is bound to. So if your
underlying field in the database is named FirstMortgageInterest then change
the name of the text box to something like txtFirstMortgageInterest. If I
understand your form and table structure correctly, then I believe that the
Control Source for the Text box that will display the average will be
=Ave3Fields([txtFirstMortgageInterest],[txtSecondMortgageInterest],[txtThird
MortgageInterest]). However, at the rate and velocity I have been giving
you advise today I am taking nothing for granted.
Sorry for all of the confusion.
Don't hesitate to get back to me if this also crashes and burns too. Send
me the names of the textboxes, the field names they are bound to, and the
error message.
Ron W
Frank said:
Ron,
Thanks. I've incorporated everything you wrote by replacing fld# with my
own field names, plus Ave3Fields with my own average field name (see below).
However, I think I'm placing the code in the incorrect place.
When I right click on the properties window, do I add this code as an
"event"?? Because when I right click the appropriate field, the data tab
doesn't bring up the code builder, it only brings up the Expression builder.
I only get the Code builder under the event tab.
I added your code under the "Before Update" event. It now reads like this...
Private Sub Average_Interest_Rate_BeforeUpdate(Cancel As Integer)
Public Function AverageInterestRate(FirstMortgageInterest As Variant,
SecondMortgageInterest As Variant, fld3 As Variant, ThirdMortgageInterest As
Variant) As Variant
Dim varTotal As Variant, intCount As Integer
intCount = 3
If IsNull(FirstMortgageInterest) Then intCount = intCount - 1
If IsNull(SecondMortgageInterest) Then intCount = intCount - 1
If IsNull(ThirdMortgageInterest) Then intCount = intCount - 1
varTotal = Nz(FirstMortgageInterest, 0) + Nz(SecondMortgageInterest, 0)
+ Nz(ThirdMortgageInterest, 0)
If intCount = 0 Then
AverageInterestRate = Null
Else
AverageInterestRate = varTotal / intCount
End If
End Function
End Sub
What's wrong? Sorry for my ignorance. I'm just learning VB programming
this month.
Frank
:
OOOOpppsss... Gotta' learn how to read. Sorry. :-( You can use the
following Function as the RecordSource of your text box
Public Function Ave3Fields(fld1 As Variant, fld2 As Variant, fld3 As
Variant) As Variant
Dim varTotal As Variant, intCount As Integer
intCount = 3
If IsNull(fld1) Then intCount = intCount - 1
If IsNull(fld2) Then intCount = intCount - 1
If IsNull(fld3) Then intCount = intCount - 1
varTotal = Nz(fld1, 0) + Nz(fld2, 0) + Nz(fld3, 0)
If intCount = 0 Then
Ave3Fields = Null
Else
Ave3Fields = varTotal / intCount
End If
End Function
Ron W
Ron,
I don't want to grab the average of all rows of one field. I want to grab
the average of 3 fields, on the same record. I have one record with 4
different fields. The first 3 fields represent actual input of values.
The
4th field represents the average of the first three fields (no data entry
allowed). I want the 4th field to contain the average of the 3 fields,
and
to not include blank entry fields in the average calculation. (hence my
example below)
This would then be repeated for each record in the table. So in short, I
want to grab the average of 3 different fields for the same record, and
display that result in the 4th field. Thanks.
Frank
P.S. Let me know if there's anything special about percentages that may
hinder this calculation.
:
How about
Assuming you want to fet the average of all rows of one field in your
table
SELECT Avg(YourField) AS AvgOfYourField FROM YourTable
This will ignore Rows that are Null
Ron W
Although this may seem trivial, I've been struggling all day w/ it. I
have 3
percentage fileds on a form. I would like to gather the average of
the
percentage fields that contain actual data.
Field 1 = 6.0%
Field 2 = 7.0%
Field 3 = blank
Traditional math tells you (6% + 7% + 0%) / 3 = 4.33%. But that's not
the
case here. I only have two fields populated, since the third field is
left
blank, so the average should be (6% + 7%) / 2 = 6.5%
Question is: how can I get the average of 3 fields, where only one or
two
of the fields may contain actual data?
I tried summing all the fields and then using a count function baked
in,
but
I'm having no luck. As you know, I can't use traditional math to
divide
by
the total number of fields because not all fields may contain data.
Any advice/help is greatly appreciated. Happy holidays.
Frank