I need to have 'median' number of required data arrays in an access database. And I found on the internet the following function which i can compile into the module and be used.
The problem is that the function work well with tables and queries without criteria. However, the function can not work with queries with criteria. For example, if the query is named 'production query', I want to know the median value of its field 'rate', and the query has a criteria like 'between [Forms]![Date Buffer]![BeginDate] And [Forms]![Date Buffer]![EndDate]', then if we use the function as Median ("production query","rate"), access will pop up an error message 'Too few parameters, expected 2'.And i'm sure the 'begindate' and 'enddata' in the criteria is filled.
Thank you very much for any suggestion!!
The function code is as follows.
-----------------------------------------------------------------------
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
-----------------------------------------------------------------------
The problem is that the function work well with tables and queries without criteria. However, the function can not work with queries with criteria. For example, if the query is named 'production query', I want to know the median value of its field 'rate', and the query has a criteria like 'between [Forms]![Date Buffer]![BeginDate] And [Forms]![Date Buffer]![EndDate]', then if we use the function as Median ("production query","rate"), access will pop up an error message 'Too few parameters, expected 2'.And i'm sure the 'begindate' and 'enddata' in the criteria is filled.
Thank you very much for any suggestion!!
The function code is as follows.
-----------------------------------------------------------------------
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
-----------------------------------------------------------------------