I am trying to find the median of a single set of values produced in a query in access, the code I am using is as follows:
Function FindMedian(rst As Recordset) As Double
Dim rst As Recordset
Dim intCount As Integer
Dim dblValue1 As Double
Dim dblValue2 As Double
Set rst = CurrentDb.OpenRecordset
rst.MoveLast
intCount As rst.RecordCount
If intCount Mod 2 = 0 Then
rst.AbsolutePosition = intCount / 2 - 1
dbvalue1 = rst!ValueColumn
rst.MoveNext
dbvalue2 = rst!ValueColumn
FindMedian = (dbvalue1 + dbvalue2) / 2
Else
rst.AbsolutePosition = (intCount + 1) / 2
FindMedian = rst!ValueColumn
End If
rst.Close
Set rst = Nothing
End Function
In the query I'm using Expr1: FindMedian([Query1]![data1])where data1 is the column of data whose median I am trying to find, but all I'm getting are errors when I run the query!
Please help - sorry, newbie!
Kev
Function FindMedian(rst As Recordset) As Double
Dim rst As Recordset
Dim intCount As Integer
Dim dblValue1 As Double
Dim dblValue2 As Double
Set rst = CurrentDb.OpenRecordset
rst.MoveLast
intCount As rst.RecordCount
If intCount Mod 2 = 0 Then
rst.AbsolutePosition = intCount / 2 - 1
dbvalue1 = rst!ValueColumn
rst.MoveNext
dbvalue2 = rst!ValueColumn
FindMedian = (dbvalue1 + dbvalue2) / 2
Else
rst.AbsolutePosition = (intCount + 1) / 2
FindMedian = rst!ValueColumn
End If
rst.Close
Set rst = Nothing
End Function
In the query I'm using Expr1: FindMedian([Query1]![data1])where data1 is the column of data whose median I am trying to find, but all I'm getting are errors when I run the query!
Please help - sorry, newbie!
Kev