I need to calculate the median value for a bunch of groups of data but I have absolutely no knowledge of access or coding or anything like that. I've found a code for calculating the median to put in a module and for using the query to pull up the results but its not working.
My table is "1,1,1-trichloroethane_chem2981" and I need the median of the field "Value" grouped by the field "PWSID"
Here is the query language:
SELECT PWSID, Median("tbl1,1,1-Trichloroethane_Chem2981", "Value",
"[PWSID] = '" & PWSID &"'") AS MedianValue
FROM [1,1,1-Trichloroethane_Chem2981]
GROUP BY PWSID
When I run the query, it comes back with the PWSID values with #Error in the Median Value column. Any suggestions or help would be greatly appreciated!!!
Here is my code:
Function Median(tName As String, fldName As String, nType as LONG) 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 AND [Type] = " & nType & " 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
ssMedian.Close
MedianDB.Close
End Function
My table is "1,1,1-trichloroethane_chem2981" and I need the median of the field "Value" grouped by the field "PWSID"
Here is the query language:
SELECT PWSID, Median("tbl1,1,1-Trichloroethane_Chem2981", "Value",
"[PWSID] = '" & PWSID &"'") AS MedianValue
FROM [1,1,1-Trichloroethane_Chem2981]
GROUP BY PWSID
When I run the query, it comes back with the PWSID values with #Error in the Median Value column. Any suggestions or help would be greatly appreciated!!!
Here is my code:
Function Median(tName As String, fldName As String, nType as LONG) 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 AND [Type] = " & nType & " 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
ssMedian.Close
MedianDB.Close
End Function