Hi,
I am borrowing some code for a function I found on this site to calculate medians by group. The original posting appears in this link: http://www.mrexcel.com/forum/archive/index.php/t-229421.html
When I tried to use this function, though, I got the error message: "Compile Error: Method or Data Member not Found" on the line of code that says Set RstSorted = RstOrig.OpenRecordset(). Can someone tell me what I am doing wrong? Below is the code I am borrowing. Thank you for your help!
ssh
Public Function MedianOfRst(RstName As String, fldName As String, Optional strWhere As String) As Double
'** Call function in query by Median: MedianOfRst("Table Name","Field Name","[Group]= " & [Group])
'This function will calculate the median of a recordset. The field must be a number value.
Dim MedianTemp As Double
Dim RstOrig As Recordset
'** NEW CODE ADDED BY GIACOMO **
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)
'** END OF NEW CODE **
' ** OLD CODE REPLACED IN THE BLOCK ABOVE **
'Set RstOrig = CurrentDb.OpenRecordset(RstName, dbOpenDynaset)
' ** ORIGINAL CODE **
RstOrig.Sort = fldName
Dim RstSorted As Recordset
'THIS IS WHERE THE COMPILE ERROR: METHOD OR DATA MEMBER NOT FOUND OCCURS.
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
-kjo1080
I am borrowing some code for a function I found on this site to calculate medians by group. The original posting appears in this link: http://www.mrexcel.com/forum/archive/index.php/t-229421.html
When I tried to use this function, though, I got the error message: "Compile Error: Method or Data Member not Found" on the line of code that says Set RstSorted = RstOrig.OpenRecordset(). Can someone tell me what I am doing wrong? Below is the code I am borrowing. Thank you for your help!
ssh
Public Function MedianOfRst(RstName As String, fldName As String, Optional strWhere As String) As Double
'** Call function in query by Median: MedianOfRst("Table Name","Field Name","[Group]= " & [Group])
'This function will calculate the median of a recordset. The field must be a number value.
Dim MedianTemp As Double
Dim RstOrig As Recordset
'** NEW CODE ADDED BY GIACOMO **
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)
'** END OF NEW CODE **
' ** OLD CODE REPLACED IN THE BLOCK ABOVE **
'Set RstOrig = CurrentDb.OpenRecordset(RstName, dbOpenDynaset)
' ** ORIGINAL CODE **
RstOrig.Sort = fldName
Dim RstSorted As Recordset
'THIS IS WHERE THE COMPILE ERROR: METHOD OR DATA MEMBER NOT FOUND OCCURS.
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
-kjo1080