Solved

How calculate median

Posted on 2016-11-22
5
52 Views
Last Modified: 2016-11-22
I have a report that shows the record ID and a number field in the detail section.  I can get the average with the sum of the number field divided by the record count in the report footer.

But how can I calculate the median average?
0
Comment
Question by:SteveL13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
crystal (strive4peace) - Microsoft MVP, Access earned 500 total points
ID: 41897756
This code averages the 2 middle values if there is not an odd number of values -- you may want to change this. Send it the SQL statement you want it to use where the first field is what you want the median of.
Function GetMedian(sSQL As String) As Double
' 121103 Crystal
' strive4peace

   On Error GoTo Proc_Err
 

   Dim db As DAO.Database _
      , rs As DAO.Recordset
      
   Dim nDbl As Double _
      , nNumValues As Long
   
   Set db = CurrentDb
   Set rs = db.OpenRecordset(sSQL, dbOpenSnapshot)
   
   If Not rs.EOF Then
      With rs
         .MoveLast
         .MoveFirst
         nNumValues = .RecordCount
         Select Case True
         
         Case nNumValues = 1
            GetMedian = Nz(.Fields(0).Value, 0)
            
         Case (nNumValues / 2 - nNumValues \ 2) < 0.001 'even
            'average 2 middle values
            .Move ((nNumValues \ 2) - 1)
            nDbl = Nz(.Fields(0).Value, 0)
            .MoveNext
            GetMedian = (nDbl + Nz(.Fields(0).Value, 0)) / 2
            
         Case Else 'odd
            .Move (nNumValues \ 2)
            GetMedian = Nz(.Fields(0).Value, 0)
         
         End Select
      End With 'rs
   End If
   
Proc_Exit:
   On Error Resume Next
   'release object variables
   If Not rs Is Nothing Then
      rs.Close
      Set rs = Nothing
   End If
   Set db = Nothing
   Exit Function
  
Proc_Err:
   MsgBox Err.Description, , _
        "ERROR " & Err.Number _
        & "   GetMedian"

   Resume Proc_Exit
   Resume
      
End Function

Open in new window

0
 

Author Comment

by:SteveL13
ID: 41897774
Must be doing something wrong or not complete.  I copy/pasted your code in the bottom of the reports VBA editor.  Now how do I make the textbox populate with the median on the report?
0
 
LVL 20
ID: 41897783
=GetMedian("SELECT MyField FROM MyTable WHERE MyConditions")

Open in new window


substitute :
MyField with the fieldname
MyTable with the table or query name
MyConditions with the criteria
0
 

Author Closing Comment

by:SteveL13
ID: 41898183
Thank you again.  Much appreciated.
0
 
LVL 20
ID: 41898421
you're welcome ~ happy to help
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question