Hello Experts!
I am running a report on a query in Access 2003 and I need to get the median(s) for several different fields in the footer of the report. I am currently using this solution from Microsoft (http://support.microso ft.com/kb/210581):
I pasted
into a new module and then placed a text box in the footer of the report with the Control Source property set to
=Median("<Table Name>", "<FieldName >")
When I run the report, I get a Parameter Value prompt for Median and then #Error# instead of a value in the report. I've tried several different but similar codes in the module but each time I get the same result. Any suggestions for correcting this problem? What am I doing wrong? Thanks!
I am running a report on a query in Access 2003 and I need to get the median(s) for several different fields in the footer of the report. I am currently using this solution from Microsoft (http://support.microso ft.com/kb/210581):
I pasted
Code:
Function Median (tName As String, fldName As String) 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 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
If Not ssMedian Is Nothing Then
ssMedian.Close
Set ssMedian = Nothing
End If
Set MedianDB = Nothing
End Function
=Median("<Table Name>", "<FieldName >")
When I run the report, I get a Parameter Value prompt for Median and then #Error# instead of a value in the report. I've tried several different but similar codes in the module but each time I get the same result. Any suggestions for correcting this problem? What am I doing wrong? Thanks!
Comment