I was hoping someone could help out with a median module we are trying to get to work.
It fails at the rs.MoveLast step if the particular grouped values are all 0 values. As long as the result field has a value the code works perfectly. Anyone have a good way to get around this? In other words I want the median formula to ignore the calculation or put something like a 0 and move on to the next group. - Thanks
It fails at the rs.MoveLast step if the particular grouped values are all 0 values. As long as the result field has a value the code works perfectly. Anyone have a good way to get around this? In other words I want the median formula to ignore the calculation or put something like a 0 and move on to the next group. - Thanks
Code:
Function MedianF(pTable As String, pfield As String, Optional pgroup As String) As Single
Dim rs As recordSet
Dim strSQL As String
Dim n As Integer
Dim sglHold As Single
If Len(pgroup) > 0 Then
strSQL = "SELECT " & pfield & " from " & pTable & " WHERE analyte= '" & pgroup & "' and " & pfield & ">0 Order by " & pfield & ";"
Else
strSQL = "SELECT " & pfield & " from " & pTable & " WHERE " & pfield & ">0 Order by " & pfield & ";"
End If
Set rs = CurrentDb.OpenRecordset(strSQL)
rs.MoveLast
n = rs.RecordCount
rs.Move -Int(n / 2)
If n Mod 2 = 1 Then 'odd number of elements
MedianF = rs(pfield)
Else 'even number of elements
sglHold = rs(pfield)
rs.MoveNext
sglHold = sglHold + rs(pfield)
MedianF = sglHold / 2
End If
rs.Close
End Function