M
Mendoza05
Hi,
I am trying to get a Median calculation per group of numbers in a query. I
am a novice at VBA as you will tell from my scripting below but i just get
the feeling its something simple I'm overlooking. Thanks for all your help
in advance!
For Instance:
Table: "Sample_Aging"
R_Number TOTAL
A 5
A 4
A 2
A -3
A 5
B 6
B 5
B 8
B -2
B 4
B 6
B 2
B 6
Wanted Results In Query:
R_Number Median
A Median Result
B Median Result
I am using the following Script:
Option Explicit
Public Function DMedian(FieldName As String, _
TableName As String, _
Optional Criteria As Variant) As Variant
On Error GoTo Err_DMedian
Dim conn As Connection
Dim rs As New ADODB.Recordset
Dim strSQL As String
Dim RowCount As Long
Dim LowMedian As Double, HighMedian As Double
'Open a recordset on the table.
Set conn = CurrentProject.Connection
strSQL = "SELECT " & FieldName & " FROM " & TableName
If Not IsMissing(Criteria) Then
strSQL = strSQL & " WHERE " & Criteria & " ORDER BY " & FieldName
Else
strSQL = strSQL & " ORDER BY " & FieldName
End If
rs.Open strSQL, conn, adOpenKeyset, adLockOptimistic
'Find the number of rows in the table.
rs.MoveLast
RowCount = rs.RecordCount
rs.MoveFirst
'Determine Even or Odd
If RowCount Mod 2 = 0 Then
'There is an even number of records. Determine the low and high
'values in the middle and average them.
rs.Move Int(RowCount / 2) - 1
LowMedian = rs(FieldName)
rs.Move 1
HighMedian = rs(FieldName)
'Return Median
DMedian = (LowMedian + HighMedian) / 2
Else
'There is an odd number of records. Return the value exactly in
'the middle.
rs.Move Int(RowCount / 2)
'Return Median
DMedian = rs(FieldName)
End If
Exit_DMedian:
'close recordset
rs.Close
Exit Function
Err_DMedian:
If Err.Number = 3075 Then
DMedian = 0
Resume Exit_DMedian
ElseIf Err.Number = 3021 Then
'EOF or BOF ie no recordset created
DMedian = -999
Resume Exit_DMedian
Else
MsgBox Err.Description
Resume Exit_DMedian
End If
End Function
In my Query, I am using the following expression:
Median: DMedian("TOTAL","Sample_Aging","[Remit_Number] = "" & [Remit_Number]
& """"")
And I am coming up with the following errors: This module provided above
does compile successfully.
1.) Syntax error in string in query expression: '[Remit_Number] = "&
[Remit_Number] & "ORDER BY TOTAL'
2.) Operation not allowed when object is closed.
I've been trying to fix it for days. I appreciate everyone looking into this!
I am trying to get a Median calculation per group of numbers in a query. I
am a novice at VBA as you will tell from my scripting below but i just get
the feeling its something simple I'm overlooking. Thanks for all your help
in advance!
For Instance:
Table: "Sample_Aging"
R_Number TOTAL
A 5
A 4
A 2
A -3
A 5
B 6
B 5
B 8
B -2
B 4
B 6
B 2
B 6
Wanted Results In Query:
R_Number Median
A Median Result
B Median Result
I am using the following Script:
Option Explicit
Public Function DMedian(FieldName As String, _
TableName As String, _
Optional Criteria As Variant) As Variant
On Error GoTo Err_DMedian
Dim conn As Connection
Dim rs As New ADODB.Recordset
Dim strSQL As String
Dim RowCount As Long
Dim LowMedian As Double, HighMedian As Double
'Open a recordset on the table.
Set conn = CurrentProject.Connection
strSQL = "SELECT " & FieldName & " FROM " & TableName
If Not IsMissing(Criteria) Then
strSQL = strSQL & " WHERE " & Criteria & " ORDER BY " & FieldName
Else
strSQL = strSQL & " ORDER BY " & FieldName
End If
rs.Open strSQL, conn, adOpenKeyset, adLockOptimistic
'Find the number of rows in the table.
rs.MoveLast
RowCount = rs.RecordCount
rs.MoveFirst
'Determine Even or Odd
If RowCount Mod 2 = 0 Then
'There is an even number of records. Determine the low and high
'values in the middle and average them.
rs.Move Int(RowCount / 2) - 1
LowMedian = rs(FieldName)
rs.Move 1
HighMedian = rs(FieldName)
'Return Median
DMedian = (LowMedian + HighMedian) / 2
Else
'There is an odd number of records. Return the value exactly in
'the middle.
rs.Move Int(RowCount / 2)
'Return Median
DMedian = rs(FieldName)
End If
Exit_DMedian:
'close recordset
rs.Close
Exit Function
Err_DMedian:
If Err.Number = 3075 Then
DMedian = 0
Resume Exit_DMedian
ElseIf Err.Number = 3021 Then
'EOF or BOF ie no recordset created
DMedian = -999
Resume Exit_DMedian
Else
MsgBox Err.Description
Resume Exit_DMedian
End If
End Function
In my Query, I am using the following expression:
Median: DMedian("TOTAL","Sample_Aging","[Remit_Number] = "" & [Remit_Number]
& """"")
And I am coming up with the following errors: This module provided above
does compile successfully.
1.) Syntax error in string in query expression: '[Remit_Number] = "&
[Remit_Number] & "ORDER BY TOTAL'
2.) Operation not allowed when object is closed.
I've been trying to fix it for days. I appreciate everyone looking into this!