Hello,
It is probably trivial question I have no experience, yet.
I've found a function that calculates a median and I need to apply it in report. How to alter the function I found? Currently, this function takes two parameters: table name and field name. To make it work, should I drop the table name parameter and use something like current Table? How can I refer to current table? Thanks for help
PS> The code has been taken from this address:
http://support.microsoft.com/?id=210581
It is probably trivial question I have no experience, yet.
I've found a function that calculates a median and I need to apply it in report. How to alter the function I found? Currently, this function takes two parameters: table name and field name. To make it work, should I drop the table name parameter and use something like current Table? How can I refer to current table? Thanks for help
PS> The code has been taken from this address:
http://support.microsoft.com/?id=210581
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