View Single Post
  #3  
Old March 16th, 2010, 07:49 PM posted to microsoft.public.access.gettingstarted
David W. Fenton
external usenet poster
 
Posts: 3,373
Default Access, average several fields in one row

John Spencer wrote in
:

One way if you can't change your data is to use a VBA function.
I've posted one below. You would call it in a calculated field in
a query. Assuming your field names are the abbreviated month
names the expression might look like the following.

Field:
fRowAverage(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oc t,Nov,Dec)

====== Copy and paste the following into a VBA module and save.
The module
must have a name other than fRowAverage


[code snipped]

Good function. I've added it to my collection of "immediate
functions", including iMax() and iMin(). I've renamed it iAve().

It also occured to me that if you didn't want to worry about passing
non-numeric values, you could do it without walking the array. The
code for that is after my sig.

Your version is more bulletproof, and for the size of array that is
the limit in a SQL statement, shouldn't be a performance issue. But
I thought it was fun to see what methods were available to total an
array of numbers. I do so love me my Split() and Join() functions!

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/

Public Function iAve(ParamArray Values()) As Double
Dim strSum As String
Dim dblSum As Double
Dim lngItemCount As Long

strSum = Join(Values(), "+")
dblSum = Eval(strSum)
lngItemCount = UBound(Values()) + 1
iAve = dblSum / lngItemCount
End Function