I’ve spend the last day researching online how to make access calculate the Median.
I really hoping someone can help me find what I need to do to find the median Number of Loans w/Ovg Pts for each name.
my query is at the bottom.
I have found code, but not know VBA I don’t know how it would work
Any help would be greatly appreciated
Back to the top
How to Use the Median() Function
Create a form and add a text box control where you want to display the median values of a data set. Set the ControlSource property of the text box control to the following:
=Median("<TableName>", "<FieldName>")
The value of this control is the median of the data set. Another way to use this function is to call it from within another function that compares the median from different data sets. For example:
I really hoping someone can help me find what I need to do to find the median Number of Loans w/Ovg Pts for each name.
my query is at the bottom.
I have found code, but not know VBA I don’t know how it would work
Any help would be greatly appreciated
Code:
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
Back to the top
How to Use the Median() Function
Create a form and add a text box control where you want to display the median values of a data set. Set the ControlSource property of the text box control to the following:
=Median("<TableName>", "<FieldName>")
The value of this control is the median of the data set. Another way to use this function is to call it from within another function that compares the median from different data sets. For example:
Code:
Function CompareMedians()
Dim MyDB as Database
.
.
.
X = Median("<TableName>", "<FieldName>")
Y = Median("<TableName>", "<FieldName>")
If X > Y Then Debug.Print "The median for X is greatest."
End Function
Code:
SELECT Table1.Type, Table1.BRANCH, Table1.Name, Table1.[Closed Loans], Table1.[Number of Loans w/Ovg Pts], Table1.[Number of Loans w/Undg Pts], Table1.[Percentage w/Ovg Pts], Table1.[Percentage w/Undg Pts], Table1.[Mean_of_w/Ovg Pts], Table1.[Mean_of_w/Undg Pts], Table1.[Median_of_w/Ovg Pts], Table1.[Median_of_w/Undg Pts]
FROM Table1
GROUP BY Table1.Type, Table1.BRANCH, Table1.Name, Table1.[Closed Loans], Table1.[Number of Loans w/Ovg Pts], Table1.[Number of Loans w/Undg Pts], Table1.[Percentage w/Ovg Pts], Table1.[Percentage w/Undg Pts], Table1.[Mean_of_w/Ovg Pts], Table1.[Mean_of_w/Undg Pts], Table1.[Median_of_w/Ovg Pts], Table1.[Median_of_w/Undg Pts];