A
Al
I am trying to calculate the median with the following
function:
********************************
Function Medianfltr(TName As String, fldName As String,
Optional fltrName 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 and [" & fltrName & "]='" & [fltrName] &
"' 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
Medianfltr = ssMedian(fldName)
Else
OffSet = (RCount / 2) - 2
For I = 0 To OffSet
ssMedian.MovePrevious
Next I
X = ssMedian(fldName)
ssMedian.MovePrevious
Y = ssMedian(fldName)
Medianfltr = (X + Y) / 2
End If
ssMedian.Close
MedianDB.Close
End Function
********************************
originally, this function did not have the <Optional
fltrName As String)>, therefore it would calculate the
median for a recordset in its entirity. What I am
interested in is to calculate the median for different sub
sets of the record set and that is why I tryed to tweak it
a little bit by having the function calculate for sub sets
if fltrName was added. in other words if a field named
states was added on the report then the function would
calculate the median for NY,AL,TX,... instead of the median
for all the states. the problem that I am having is how to
substitute for the different values in the state fields so
that I get the median for each state. can someone help?
thanks (note the function works fine without the optional
fltername as string that I added later)
Al
function:
********************************
Function Medianfltr(TName As String, fldName As String,
Optional fltrName 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 and [" & fltrName & "]='" & [fltrName] &
"' 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
Medianfltr = ssMedian(fldName)
Else
OffSet = (RCount / 2) - 2
For I = 0 To OffSet
ssMedian.MovePrevious
Next I
X = ssMedian(fldName)
ssMedian.MovePrevious
Y = ssMedian(fldName)
Medianfltr = (X + Y) / 2
End If
ssMedian.Close
MedianDB.Close
End Function
********************************
originally, this function did not have the <Optional
fltrName As String)>, therefore it would calculate the
median for a recordset in its entirity. What I am
interested in is to calculate the median for different sub
sets of the record set and that is why I tryed to tweak it
a little bit by having the function calculate for sub sets
if fltrName was added. in other words if a field named
states was added on the report then the function would
calculate the median for NY,AL,TX,... instead of the median
for all the states. the problem that I am having is how to
substitute for the different values in the state fields so
that I get the median for each state. can someone help?
thanks (note the function works fine without the optional
fltername as string that I added later)
Al