When trying to use functions that calculate the median (which I found through this forum), I was getting a "Too Few Parameters. Expected 1" error.
As the query which is to use the median function itself queries a parameter query, I thought that was the problem so I tried declaring that query and seting the parameter.
I am now getting the "Object variable or With block variable not set" error.
Any ideas or suggestions? Thanks!
Here is the code:
ublic Function GetMedian(FieldName As String, _
TableName As String) As Variant
Dim db As Database
Dim rs As Recordset
Dim firstVal As Double, recCount As Long
Dim qdf As QueryDef
Set qdf = db.QueryDefs("qryRpt_Main_PtDemographics_Summary")
qdf.Parameters(0) = [Forms]![frmPatientManagementReportMenu]![txtEndDate]
Set rs = CurrentDb.OpenRecordset("Select " & FieldName & " From " & TableName & " Order By " & FieldName & ";", dbOpenSnapshot)
recCount = rs.RecordCount
If recCount < 2 Then
Select Case recCount
Case 0
GetMedian = Null
Case 1
rs.MoveFirst
GetMedian = rs!FieldName
End Select
Set rs = Nothing
Exit Function
End If
rs.MoveLast
rs.MoveFirst
If (recCount Mod 2) = 0 Then
rs.Move (recCount / 2) - 1
firstVal = rs!FieldName
rs.MoveNext
GetMedian = (firstVal + rs!FieldName) / 2
Else
rs.Move Fix(recCount / 2) - 1
GetMedian = rs!FieldName
End If
Set rs = Nothing
End Function
As the query which is to use the median function itself queries a parameter query, I thought that was the problem so I tried declaring that query and seting the parameter.
I am now getting the "Object variable or With block variable not set" error.
Any ideas or suggestions? Thanks!
Here is the code:
ublic Function GetMedian(FieldName As String, _
TableName As String) As Variant
Dim db As Database
Dim rs As Recordset
Dim firstVal As Double, recCount As Long
Dim qdf As QueryDef
Set qdf = db.QueryDefs("qryRpt_Main_PtDemographics_Summary")
qdf.Parameters(0) = [Forms]![frmPatientManagementReportMenu]![txtEndDate]
Set rs = CurrentDb.OpenRecordset("Select " & FieldName & " From " & TableName & " Order By " & FieldName & ";", dbOpenSnapshot)
recCount = rs.RecordCount
If recCount < 2 Then
Select Case recCount
Case 0
GetMedian = Null
Case 1
rs.MoveFirst
GetMedian = rs!FieldName
End Select
Set rs = Nothing
Exit Function
End If
rs.MoveLast
rs.MoveFirst
If (recCount Mod 2) = 0 Then
rs.Move (recCount / 2) - 1
firstVal = rs!FieldName
rs.MoveNext
GetMedian = (firstVal + rs!FieldName) / 2
Else
rs.Move Fix(recCount / 2) - 1
GetMedian = rs!FieldName
End If
Set rs = Nothing
End Function