Could someone help me with this module for Microsoft Access. At the moment, the module calculates the median of my entire population of patients. However, in my report I have patients grouped by carepath, hospital, month. Could I incorporate these groupings so that my report gives me the carepath median, hospital median, and carepath by month median.
Public Function median(qryCarepaths As String, LOS As String) As Single
Dim MedianDB As DAO.Database
Dim ssMedian As DAO.Recordset
Dim RCount As Integer, i As Integer, x As Integer, y As Integer, _
OffSet As Integer
Set MedianDB = CurrentDb()
Set ssMedian = MedianDB.OpenRecordset("SELECT [" & LOS & _
"] FROM [" & qryCarepaths & "] WHERE [" & LOS & _
"] IS NOT NULL ORDER BY [" & LOS & "];"
'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(LOS)
Else
OffSet = (RCount / 2) - 2
For i = 0 To OffSet
ssMedian.MovePrevious
Next i
x = ssMedian(LOS)
ssMedian.MovePrevious
y = ssMedian(LOS)
median = (x + y) / 2
End If
ssMedian.Close
MedianDB.Close
End Function
Public Function median(qryCarepaths As String, LOS As String) As Single
Dim MedianDB As DAO.Database
Dim ssMedian As DAO.Recordset
Dim RCount As Integer, i As Integer, x As Integer, y As Integer, _
OffSet As Integer
Set MedianDB = CurrentDb()
Set ssMedian = MedianDB.OpenRecordset("SELECT [" & LOS & _
"] FROM [" & qryCarepaths & "] WHERE [" & LOS & _
"] IS NOT NULL ORDER BY [" & LOS & "];"
'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(LOS)
Else
OffSet = (RCount / 2) - 2
For i = 0 To OffSet
ssMedian.MovePrevious
Next i
x = ssMedian(LOS)
ssMedian.MovePrevious
y = ssMedian(LOS)
median = (x + y) / 2
End If
ssMedian.Close
MedianDB.Close
End Function