J
Jason
Hi,
I'm trying to use the example code from Microsoft's knowledge base to
calculate the median in a report (code below). The code works fine for me in
calculating the the mean of the entire dataset of my table/query. The
problem I run into is when I I try to calculate the same median function in a
report on a grouping, instead of returning the median for that specific
grouping, it returns the median for the entire dataset repeatedly under each
grouping.
As an example, my report is grouped based on Industry, and I would like to
return the median calculation of revenues as outlined below
Industry 1
Company1 $100
Company2 $180
Company3 $300
Company4 $450
Company5 $500
Median $300
Industry 2
Company1 $180
Company2 $250
Company3 $375
Median $250
In the current form, instead of returning the median for the grouping ($300
for Industry1 and $250 for Industry2), I instead am getting the median for
the entire set of 8 records ($275 in this case) and it is displaying under
both the median for Industry1 and Industry2. Can someone suggest how I might
alter the code below or possibly provide other coding that would handle this?
Thanks.
Function Median(tName As String, fldName As String) As Single
Dim MedianDB As DAO.Database
Dim ssMedian As DAO.Recordset
Dim RCount As Integer, i As Integer, x As Double, y As Double, _
OffSet As Integer
Set MedianDB = CurrentDb()
Set ssMedian = MedianDB.OpenRecordset("SELECT [" & fldName & _
"] FROM [" & tName & "] WHERE [" & fldName & _
"] IS NOT NULL ORDER BY [" & fldName & "];")
'NOTE: To include nulls when calculating the median value, omit
'WHERE [" & fldName & "] IS NOT NULL from the example.
ssMedian.MoveLast
RCount% = ssMedian.RecordCount
x = RCount Mod 2
If x <> 0 Then
OffSet = ((RCount + 1) / 2) - 2
For i% = 0 To OffSet
ssMedian.MovePrevious
Next i
Median = ssMedian(fldName)
Else
OffSet = (RCount / 2) - 2
For i = 0 To OffSet
ssMedian.MovePrevious
Next i
x = ssMedian(fldName)
ssMedian.MovePrevious
y = ssMedian(fldName)
Median = (x + y) / 2
End If
If Not ssMedian Is Nothing Then
ssMedian.Close
Set ssMedian = Nothing
End If
Set MedianDB = Nothing
End Function
I'm trying to use the example code from Microsoft's knowledge base to
calculate the median in a report (code below). The code works fine for me in
calculating the the mean of the entire dataset of my table/query. The
problem I run into is when I I try to calculate the same median function in a
report on a grouping, instead of returning the median for that specific
grouping, it returns the median for the entire dataset repeatedly under each
grouping.
As an example, my report is grouped based on Industry, and I would like to
return the median calculation of revenues as outlined below
Industry 1
Company1 $100
Company2 $180
Company3 $300
Company4 $450
Company5 $500
Median $300
Industry 2
Company1 $180
Company2 $250
Company3 $375
Median $250
In the current form, instead of returning the median for the grouping ($300
for Industry1 and $250 for Industry2), I instead am getting the median for
the entire set of 8 records ($275 in this case) and it is displaying under
both the median for Industry1 and Industry2. Can someone suggest how I might
alter the code below or possibly provide other coding that would handle this?
Thanks.
Function Median(tName As String, fldName As String) As Single
Dim MedianDB As DAO.Database
Dim ssMedian As DAO.Recordset
Dim RCount As Integer, i As Integer, x As Double, y As Double, _
OffSet As Integer
Set MedianDB = CurrentDb()
Set ssMedian = MedianDB.OpenRecordset("SELECT [" & fldName & _
"] FROM [" & tName & "] WHERE [" & fldName & _
"] IS NOT NULL ORDER BY [" & fldName & "];")
'NOTE: To include nulls when calculating the median value, omit
'WHERE [" & fldName & "] IS NOT NULL from the example.
ssMedian.MoveLast
RCount% = ssMedian.RecordCount
x = RCount Mod 2
If x <> 0 Then
OffSet = ((RCount + 1) / 2) - 2
For i% = 0 To OffSet
ssMedian.MovePrevious
Next i
Median = ssMedian(fldName)
Else
OffSet = (RCount / 2) - 2
For i = 0 To OffSet
ssMedian.MovePrevious
Next i
x = ssMedian(fldName)
ssMedian.MovePrevious
y = ssMedian(fldName)
Median = (x + y) / 2
End If
If Not ssMedian Is Nothing Then
ssMedian.Close
Set ssMedian = Nothing
End If
Set MedianDB = Nothing
End Function