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.
I have also attached my sample database so you can see what I’m working with.
I have found code, but not know VBA I don’t know how it would work
Any help would be greatly appreciated - 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: - 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
12 2464 NeoPa 32,557
Recognized Expert Moderator MVP
From what I can see of your post you have everything you need. The Median function appears to do a pretty decent (It doesn't handle even numbered record counts very well but that's just being picky) and you include code that illustrates you know how to call it.
What is your question?
Thanks NeoPa, could you help my understand what I need to do after I paste the VBA. Thank you soon much
NeoPa 32,557
Recognized Expert Moderator MVP
Pretend I don't already know what you're thinking, and explain again what you want.
Third time lucky.
NeoPa, you must be a teacher or one in your past life.:0)
I want it to return the median of all records in the field, but the median for all records for a given LoanName
I would guess the formula looks something like this - Median("MyTestTable","w_Ovg_Points","LoanName = ",LoanName)
NeoPa 32,557
Recognized Expert Moderator MVP Dave Smith:
NeoPa, you must be a teacher or one in your past life.:0)
There's certainly a deal of similarity answering questions on a forum. If only more people had the experience of reading the questions they write down, they'd see how often they make barely discernable sense. Not a problem. One gets used to it, but one still can't help much until the question makes sense, so we sometimes have to prompt for clarification. All in a good cause at the end of the day of course.
On to the question.
What you require is not actually (directly) supported by the procedure you've posted, which leaves us two options : - Update the procedure to accept another parameter to include in the WHERE clause (Essentially specify extra filtering). This is possible, but more work than option #2.
- Be a bit clever and pass a SQL SELECT string as the tName parameter enclosed in parentheses () and pre-filtered to match your [LoanName] so that the function uses that instead of a table.
- Dim strSQL As String
-
-
strSQL = "(SELECT [w_Ovg_Points]" & _
-
" FROM [MyTestTable]" & _
-
" WHERE ([LoanName]='" & Me.LoanName & "')"
-
BlahBlah = Median(strSQL, "w_Ovg_Points")
Please forgive my ignorance. Would this code go in conjunction with all the code above?
NeoPa 32,557
Recognized Expert Moderator MVP
Yes it would. It calls the function Median(), included in your first post.
Hi NeoPa, I know you;ve been a huge help already, but now I'm getting the error below?
There was an error compiling this function.
The Visual Basic module contains a syntax error
Check the code, and then recompile it
NeoPa 32,557
Recognized Expert Moderator MVP
What did it tell you when you compiled it? It should give an error message and a line number. This information is quite important if I'm to have a clue what's going on.
ADezii 8,834
Recognized Expert Expert - Function Median(tName As String, fldName As String, strLoanName 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
-
Dim strSQL As String
-
-
'NOTE: To include nulls when calculating the median value, omit
-
'WHERE [" & fldName & "] IS NOT NULL from the example.
-
strSQL = "SELECT [" & fldName & "] FROM [" & tName & "] WHERE [" & fldName & _
-
"] IS NOT NULL AND [LoanName] = '" & strLoanName & _
-
"' ORDER BY [" & fldName & "];"
-
-
Set MedianDB = CurrentDb()
-
Set ssMedian = MedianDB.OpenRecordset(strSQL)
-
-
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
How would I call this function Median([Feildname])?
ADezii 8,834
Recognized Expert Expert
You cannot supply a Field Name only without modifying the Code. The Syntax would be: - Dim sngMedian as Single
-
sngmedian = Median("<Table Name>","<Field Name>","<Loan Name>")
-
-
'Supply the appropriate Values between the <...>
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Bill |
last post by:
For a variety of user-driven reasons, we're using an
Access DB on a Desktop as the repository for data we
transfer to/from a Pocket PC 2002 Application that uses
SQL-CE. Have not yet located a...
|
by: Paradigm |
last post by:
I am using Access 2K as a front end to MySQL running on a Linux server.
I am having trouble connectiong to the server. MySQL control centre connects
and I can connect using a DSN data source. But...
|
by: bill mahoney |
last post by:
Hello,
I am using access 2000 and I am trying to email a csv file. I have
only used the docmd.sendobject command but you can only email objects
that are within the access database ( as far as I...
|
by: Bob |
last post by:
I have been looking at the code for MedianFind(pDte As String) from the
following thread from UtterAccess.com: "Finding Median average grouped
by field"
I have been able to get it to run using...
|
by: Serious_Practitioner |
last post by:
Good day all, and thank you in advance for your help. No - MANY thanks in
advance for your help - I know nothing about using databases on Web servers.
I am about to discuss a project with a...
| |
by: dave m |
last post by:
I need to be able to retrieve a unique ID from a users PC. I needs to be
something a user could not easily change, like the computer name. Could
someone point me in the right direction to find ...
|
by: Bugs |
last post by:
Hi everyone.
I am trying to open a database which works fine using Access 2003, but when
trying to open it on another PC that has Access 2002 I get the following
error
"This database is...
|
by: Rose2007 |
last post by:
Hi,
I have a question regarding how to read a XML code from ACCESS.
In my company there is a webpage that ask to enter the employee ID. After entering the ID automatically it gives all the...
|
by: konradson |
last post by:
Hello,
I've built a DB I would like to use in an Intranet through web, this
way a small group of people can gain access by typing the address in
their browser. It is a small DB, with a few...
|
by: jesiecraig |
last post by:
Hello,
I am new to VBA programming - thank you for your help. I am using a form in Access 2007 to update my database. I use a submit button on the form to send all the values into Access. I...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |