473,503 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help finding the Median using MS Access

72 New Member
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

Expand|Select|Wrap|Line Numbers
  1. Function Median (tName As String, fldName As String) As Single
  2.   Dim MedianDB As DAO.Database
  3.   Dim ssMedian As DAO.Recordset
  4.   Dim RCount As Integer, i As Integer, x As Double, y As Double, _
  5.       OffSet As Integer
  6.   Set MedianDB = CurrentDB()
  7.   Set ssMedian = MedianDB.Openrecordset("SELECT [" & fldName & _
  8.             "] FROM [" & tName & "] WHERE [" & fldName & _ 
  9.             "] IS NOT NULL ORDER BY [" & fldName  & "];")
  10.   'NOTE: To include nulls when calculating the median value, omit
  11.   'WHERE [" & fldName & "] IS NOT NULL from the example.
  12.   ssMedian.MoveLast
  13.   RCount% = ssMedian.RecordCount
  14.   x = RCount Mod 2
  15.   If x <> 0 Then
  16.      OffSet = ((RCount + 1) / 2) - 2
  17.      For i% = 0 To OffSet
  18.         ssMedian.MovePrevious
  19.      Next i
  20.      Median = ssMedian(fldName)
  21.   Else
  22.      OffSet = (RCount / 2) - 2
  23.      For i = 0 To OffSet
  24.         ssMedian.MovePrevious
  25.      Next i
  26.      x = ssMedian(fldName)
  27.      ssMedian.MovePrevious
  28.      y = ssMedian(fldName)
  29.      Median = (x + y) / 2
  30.   End If
  31.   If Not ssMedian Is Nothing Then
  32.      ssMedian.Close
  33.      Set ssMedian = Nothing
  34.   End If
  35.   Set MedianDB = Nothing
  36. 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:

Expand|Select|Wrap|Line Numbers
  1.  Function CompareMedians()
  2.       Dim MyDB as Database
  3.       .
  4.       .
  5.       .
  6.       X = Median("<TableName>", "<FieldName>")
  7.       Y = Median("<TableName>", "<FieldName>")
  8.       If X > Y Then Debug.Print "The median for X is greatest."
  9.    End Function
Attached Files
File Type: zip SampleDatabase.zip (124.4 KB, 85 views)
Aug 25 '11 #1
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?
Aug 25 '11 #2
Dave Smith
72 New Member
Thanks NeoPa, could you help my understand what I need to do after I paste the VBA. Thank you soon much
Aug 26 '11 #3
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.
Aug 26 '11 #4
Dave Smith
72 New Member
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

Expand|Select|Wrap|Line Numbers
  1. Median("MyTestTable","w_Ovg_Points","LoanName = ",LoanName)
Aug 26 '11 #5
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 :
  1. 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.
  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.
    Expand|Select|Wrap|Line Numbers
    1. Dim strSQL As String
    2.  
    3. strSQL = "(SELECT [w_Ovg_Points]" & _
    4.          " FROM   [MyTestTable]" & _
    5.          " WHERE  ([LoanName]='" & Me.LoanName & "')"
    6. BlahBlah = Median(strSQL, "w_Ovg_Points")
Aug 26 '11 #6
Dave Smith
72 New Member
Please forgive my ignorance. Would this code go in conjunction with all the code above?
Aug 26 '11 #7
NeoPa
32,557 Recognized Expert Moderator MVP
Yes it would. It calls the function Median(), included in your first post.
Aug 26 '11 #8
Dave Smith
72 New Member
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
Aug 29 '11 #9
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.
Aug 29 '11 #10
ADezii
8,834 Recognized Expert Expert
Expand|Select|Wrap|Line Numbers
  1. Function Median(tName As String, fldName As String, strLoanName As String) As Single
  2. Dim MedianDB As DAO.Database
  3. Dim ssMedian As DAO.Recordset
  4. Dim RCount As Integer, i As Integer, x As Double, y As Double, _
  5.     OffSet As Integer
  6. Dim strSQL As String
  7.  
  8. 'NOTE: To include nulls when calculating the median value, omit
  9. 'WHERE [" & fldName & "] IS NOT NULL from the example.
  10. strSQL = "SELECT [" & fldName & "] FROM [" & tName & "] WHERE [" & fldName & _
  11.          "] IS NOT NULL AND [LoanName] = '" & strLoanName & _
  12.          "' ORDER BY [" & fldName & "];"
  13.  
  14. Set MedianDB = CurrentDb()
  15. Set ssMedian = MedianDB.OpenRecordset(strSQL)
  16.  
  17. ssMedian.MoveLast
  18. RCount% = ssMedian.RecordCount
  19. x = RCount Mod 2
  20.  
  21. If x <> 0 Then
  22.   OffSet = ((RCount + 1) / 2) - 2
  23.     For i% = 0 To OffSet
  24.       ssMedian.MovePrevious
  25.     Next i
  26.     Median = ssMedian(fldName)
  27. Else
  28.   OffSet = (RCount / 2) - 2
  29.     For i = 0 To OffSet
  30.       ssMedian.MovePrevious
  31.     Next i
  32.     x = ssMedian(fldName)
  33.     ssMedian.MovePrevious
  34.     y = ssMedian(fldName)
  35.       Median = (x + y) / 2
  36. End If
  37.  
  38. If Not ssMedian Is Nothing Then
  39.   ssMedian.Close
  40.     Set ssMedian = Nothing
  41. End If
  42.   Set MedianDB = Nothing
  43. End Function
Aug 29 '11 #11
Dave Smith
72 New Member
How would I call this function Median([Feildname])?
Sep 7 '11 #12
ADezii
8,834 Recognized Expert Expert
You cannot supply a Field Name only without modifying the Code. The Syntax would be:
Expand|Select|Wrap|Line Numbers
  1. Dim sngMedian as Single
  2. sngmedian = Median("<Table Name>","<Field Name>","<Loan Name>") 
  3.  
  4. 'Supply the appropriate Values between the <...>
Sep 8 '11 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1925
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...
0
1411
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...
3
4934
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...
2
2516
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...
6
1983
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...
9
12558
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 ...
4
2574
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...
4
1936
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...
1
1842
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...
0
1427
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...
0
7199
marktang
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,...
0
7273
Oralloy
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,...
0
5572
agi2029
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,...
1
5000
isladogs
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...
0
3161
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...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
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 ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.