Results 1 to 4 of 4
  1. #1
    glmtd is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jul 2012
    Posts
    5

    Average of multiple fields

    Hello everyone. I am trying to calculate an average of multiple fields (Field1, Field 2, Field 3) in a query in Access 2010. Can someone help me to create a module that would calculate the average of multiple fields with 0 as valid value, but ignore the blanks. So if Field1 is blank, Field2 is 1, and Field3 is 2, their average would turn out to be 1.5, as blank would be ignored. Consequently, if Field1 is 0, Field2 is 1, and Field3 is 2, their average would be 1 ( (0+1+3)/3).

    Here is a script that I found which deals with 0, but would like for it to utilize 0 and ignore blanks.

    Thanks

    Script:
    Public Function Average_Of_Fields(ParamArray flds() As Variant) As Double
    Dim i As Byte, sumFields As Long, numFields As Byte

    For i = LBound(flds) To UBound(flds)
    If Nz(flds(i), 0) <> 0 Then


    sumFields = sumFields + flds(i)
    numFields = numFields + 1
    End If
    Next

    If numFields <> 0 Then
    Average_Of_Fields = sumFields / numFields
    End If
    End Function

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try

    If Len(flds(i) & vbNullString) > 0 Then
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    glmtd is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jul 2012
    Posts
    5
    Thank You! That did the job.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem, and welcome to the site by the way!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Calculating the Total of Average Fields
    By DDEB in forum Queries
    Replies: 1
    Last Post: 05-09-2012, 06:26 PM
  2. Count Unique Fields and then get Average
    By Kirsti in forum Queries
    Replies: 5
    Last Post: 02-19-2012, 07:25 PM
  3. Replies: 13
    Last Post: 05-28-2010, 11:57 AM
  4. Average of multiple fields on one row
    By Lockrin in forum Access
    Replies: 3
    Last Post: 05-18-2010, 11:14 AM
  5. Query to Average on Grouped Fields
    By randolphoralph in forum Queries
    Replies: 1
    Last Post: 03-16-2010, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums