1. Create a module. I would name it something like basMiscUtilities
Paste this function in the module and save it.
Puiblic Function CalcAvg(sngAmt1, sngAmt2, sngAmt3) As Single
Dim lngDivisor as Long
lngDivisor = 3
If IsNull(sngAmt1) Then lngDivisor = 2
If IsNull(sngAmt2) Then lngDivisor = 1
If IsNull(sngAmt3) Then lngDivisor = 0
If lngDivisor > 0 Then
CalcAvg = (Nz(sngAmt1,0) + Nz(sngAmt2,0) + Nz(sngAmt3,0))/lngDivisor
Else
CalcAvg = 0
End If
End Function
2. Add a column to your query and paste this in the Field row:
AmtAvg: CalcAvg(Me.txtAmt1, Me.txtAmt2, Me.txtAmt3)
Where it says Me.txtAmt1, replace it with the name of the textbox on your
form, same for the other two
As to good reference material, I don't personally care much for what
Microsoft publishes. What I use is Access 2002 Desktop Developer's Handbook
by Paul Litwin, Ken Getz, and Mike Gunderloy, published by Sybex. I have
heard they are not planning a 20003 version release, which is disappointing,
but I don't know that to be a fact.
My suggestion would be to go to a bookstore that is heavy in techincal
manuals and thumb through what is available for Access and buy the one that
makes most sense to you.
Good Luck.
Darlahood said:
No I didn't create the function. I just pasted the example into the field row
of my query.
Should I create a module? If so what would the code be.
I understand some programming, but I dont have any books on programming for
access.
Are there any examples in the Northwind DataBase?
I think I may need a good book on programming/tweaking access.
Do you have any suggestions?
--
Darlahood
:
Did you create a function named CalcAvg based on the example I posted? Where
did you put it? It needs to be in a standard module and it needs to be
identified as
Public Sub
:
It says undefined function 'CalcAvg' in expression
--
Darlahood
:
No problem. Just add a column to your query. In the Field row:
AmtAvg: CalcAvg(Me.txtAmt1, Me.txtAmt2, Me.txtAmt3)
:
Oh Goodness!~
I just found out that we need the avg to be used in a query, and the
avgField needs to be joined to the table...When we do a query on the data it
isnt "bound" to the table and we dont get any data...
I tried making an event for the avg in the code editor but to no avail...
Ho boy anymore help would be greatly appreciated Thank you very much!
--
Darlahood
:
#1. See my previous post. This is because you are getting a divide by 0
when all 3 fields are empty.
#2. Yes, but you will still get the problem in #1. You can use the function
I posted earlier (here it is again) in place of the formula as your control
source. It will avoid the divide by 0 problem.
Function CalcAvg(sngAmt1, sngAmt2, sngAmt3) As Single
Dim lngDivisor as Long
lngDivisor = 3
If IsNull(sngAmt1) Then lngDivisor = 2
If IsNull(sngAmt2) Then lngDivisor = 1
If IsNull(sngAmt3) Then lngDivisor = 0
If lngDivisor > 0 Then
CalcAvg = (Nz(sngAmt1,0) + Nz(sngAmt2,0) + Nz(sngAmt3,0))/lngDivisor
End If
End Function
#3. Great, I hoped that would work.
:
You people are Gods!!
Thank you so much for your help. Nobody has been able to help me with this.
now for the questions:
#1. The avgField displays #Num! when the fields are blank. When we have 0 in
the fields it displays 0.
#2. To implement the return 0, do I modify the current code of:
=(Nz([Press#1Moisture]))/(Abs(Not IsNull([Press#1Moisture])))
To Read:
=(Nz([Press#1Moisture],0))/(Abs(Not IsNull([Press#1Moisture])))
#3. It updates when I click to another field.
Thanks So Much!!