spiderrasmon
New Member
- Joined
- Jan 27, 2004
- Messages
- 9
Hey there:
I'm using the following code featured in the Microsoft Knowledge Base Article - 210581 to calc. the stat. median for a list of values in Access. Problem is, when I apply this code to any form or query, it spits out the same median for each different value.
I want to do a Totals query by employee name, then get the median for hours worked per employee instead of the entire list. what am I doing wrong?
Here's the code:
when I apply it in a form's text box or as an expression, I have to use the following:
Any help here would be hot!
I'm using the following code featured in the Microsoft Knowledge Base Article - 210581 to calc. the stat. median for a list of values in Access. Problem is, when I apply this code to any form or query, it spits out the same median for each different value.
I want to do a Totals query by employee name, then get the median for hours worked per employee instead of the entire list. what am I doing wrong?
Here's the code:
Code:
Option Explicit
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
ssMedian.Close
MedianDB.Close
End Function
when I apply it in a form's text box or as an expression, I have to use the following:
Code:
=Median("<TableName>", "<FieldName">)
Any help here would be hot!