Hello All, kindly please take a look at the following code for calculating the Median. Code for Min and Max work just fine but need some assistance here.
Am basically trying to pull the Median from a structure such as this:
Part Number Length Width Height
00010236 4.00 3.50 1.50
00010237 5.00 4.50 2.50
00010238 6.00 5.50 3.50
My expression is - Med: MedianOfRst("Master_HMA","Length")
When I run this I get the Median based on every single record listed for Length. How do I structure the code and expression so I can pull the median from Length, Width and Height for each Part Number?
Am basically trying to pull the Median from a structure such as this:
Part Number Length Width Height
00010236 4.00 3.50 1.50
00010237 5.00 4.50 2.50
00010238 6.00 5.50 3.50
My expression is - Med: MedianOfRst("Master_HMA","Length")
When I run this I get the Median based on every single record listed for Length. How do I structure the code and expression so I can pull the median from Length, Width and Height for each Part Number?
Code:
Public Function MedianOfRst(RstName As String, fldName As String, Optional strWhere As String) As Double
Dim MedianTemp As Double
Dim RstOrig As Recordset
Dim strSQL As String
strSQL = "SELECT [" & fldName & "] FROM [" & RstName & "]"
If strWhere <> vbNullString Then
strSQL = strSQL & " WHERE " & strWhere
End If
Set RstOrig = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
RstOrig.Sort = fldName
Dim RstSorted As Recordset
Set RstSorted = RstOrig.OpenRecordset()
If RstSorted.RecordCount Mod 2 = 0 Then
RstSorted.AbsolutePosition = (RstSorted.RecordCount / 2) - 1
MedianTemp = RstSorted.Fields(fldName).Value
RstSorted.MoveNext
MedianTemp = MedianTemp + RstSorted.Fields(fldName).Value
MedianTemp = MedianTemp / 2
Else
RstSorted.AbsolutePosition = (RstSorted.RecordCount - 1) / 2
MedianTemp = RstSorted.Fields(fldName).Value
End If
MedianOfRst = MedianTemp
End Function