473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to write code for Median

29
I have a textbox that shows Max, Min, Avg, and Median. I have the code for all expect Median. Can somebody please help.
Example: For Max, I just went into the properties of the textbox, then in the control source I just put in =Max([Engine Price Est]). What would it be for Median?
Feb 27 '07 #1
12 5090
MMcCarthy
14,534 Expert Mod 8TB
This cannot be done with a standard function as there is no way to decide the half way point in a set of numbers you will need a new function like the following:

Expand|Select|Wrap|Line Numbers
  1. Public Function findMedianPrice() As Double
  2. Dim db As DAO.Database
  3. Dim rs As DAO.Recordset
  4. Dim recCount As Integer
  5. Dim tmpMed as Double
  6.  
  7.    Set db = CurrentDb
  8.    Set rs = db.OpenRecordset("SELECT [Engine Price Est] FROM TableName " & _
  9.           "ORDER BY [Engine Price Est];"
  10.  
  11.    rs.MoveLast
  12.    rs.MoveFirst
  13.    recCount = rs.RecordCount
  14.  
  15.    If recCount mod 2 = 0 Then ' If even number of records
  16.       rs.Move recCount/2
  17.       tmpMed = rs![Engine Price Est]
  18.       rs.MoveNext
  19.       tmpMed = tmpMed + rs![Engine Price Est]
  20.       tmpMed = tmpMed/2
  21.    Else
  22.       rs.Move (recCount -1)/2
  23.       rs.MoveNext
  24.       tmpMed = rs![Engine Price Est]
  25.    End If
  26.  
  27.    findMedianPrice = tmpMed
  28.  
  29. End Sub
  30.  
Feb 27 '07 #2
Scldb
29
This cannot be done with a standard function as there is no way to decide the half way point in a set of numbers you will need a new function like the following:

Expand|Select|Wrap|Line Numbers
  1. Public Function findMedianPrice() As Double
  2. Dim db As DAO.Database
  3. Dim rs As DAO.Recordset
  4. Dim recCount As Integer
  5. Dim tmpMed as Double
  6.  
  7.    Set db = CurrentDb
  8.    Set rs = db.OpenRecordset("SELECT [Engine Price Est] FROM TableName " & _
  9.           "ORDER BY [Engine Price Est];"
  10.  
  11.    rs.MoveLast
  12.    rs.MoveFirst
  13.    recCount = rs.RecordCount
  14.  
  15.    If recCount mod 2 = 0 Then ' If even number of records
  16.       rs.Move recCount/2
  17.       tmpMed = rs![Engine Price Est]
  18.       rs.MoveNext
  19.       tmpMed = tmpMed + rs![Engine Price Est]
  20.       tmpMed = tmpMed/2
  21.    Else
  22.       rs.Move (recCount -1)/2
  23.       rs.MoveNext
  24.       tmpMed = rs![Engine Price Est]
  25.    End If
  26.  
  27.    findMedianPrice = tmpMed
  28.  
  29. End Sub
  30.  
Your code:
Dim db As DAO.Database
Dim rs As DAO.Recordset

Does DAO only work with the newer versions of Access?
What if I was doing this in Access 97? Would it still work? I put in the code you gave me and I got a line of code that it didn't like. The line of code that was shown in red is:
Set rs = db.OpenRecordset("SELECT [Engine Price Est] FROM All Areas with additions " &
"ORDER BY [Engine Price Est];"

I don't know what the problem is? Can you help?
Feb 27 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
Your code:
Dim db As DAO.Database
Dim rs As DAO.Recordset

Does DAO only work with the newer versions of Access?
What if I was doing this in Access 97? Would it still work? I put in the code you gave me and I got a line of code that it didn't like. The line of code that was shown in red is:
Set rs = db.OpenRecordset("SELECT [Engine Price Est] FROM All Areas with additions " &
"ORDER BY [Engine Price Est];"

I don't know what the problem is? Can you help?
Go to Tools - References in the VBA Editor window and make sure that the Microsoft DAO library is ticked.

Change code to ...

Set rs = db.OpenRecordset("SELECT [Engine Price Est] FROM [All Areas with additions] " & _
"ORDER BY [Engine Price Est];")
Feb 27 '07 #4
Scldb
29
Go to Tools - References in the VBA Editor window and make sure that the Microsoft DAO library is ticked.

Change code to ...

Set rs = db.OpenRecordset("SELECT [Engine Price Est] FROM [All Areas with additions] " & _
"ORDER BY [Engine Price Est];")

The code you gave me is for a report that is linked to a form (a form that lets people build queries) then prints out a report of the query.
So with this being said, I want the median of only the query (over group) not over the whole table. What would the VBA code be for this? It wouldn't be the same would it?
Feb 27 '07 #5
MMcCarthy
14,534 Expert Mod 8TB
The code you gave me is for a report that is linked to a form (a form that lets people build queries) then prints out a report of the query.
So with this being said, I want the median of only the query (over group) not over the whole table. What would the VBA code be for this? It wouldn't be the same would it?
Dynamic reporting is a b***. Sorry just had to get that out of the way.

The problem is how to put the subset into the recordset:

This ...
Expand|Select|Wrap|Line Numbers
  1.  
  2. Set rs = db.OpenRecordset("SELECT [Engine Price Est] FROM [All Areas with additions] " & _
  3.           "ORDER BY [Engine Price Est];")
Would have to be changed to only reflect the subset of data you are working from. Without knowing a lot more I can't really help. I don't think it will be easy though.

Mary
Feb 27 '07 #6
Scldb
29
Dynamic reporting is a b***. Sorry just had to get that out of the way.

The problem is how to put the subset into the recordset:

This ...
Expand|Select|Wrap|Line Numbers
  1.  
  2. Set rs = db.OpenRecordset("SELECT [Engine Price Est] FROM [All Areas with additions] " & _
  3.           "ORDER BY [Engine Price Est];")
Would have to be changed to only reflect the subset of data you are working from. Without knowing a lot more I can't really help. I don't think it will be easy though.

Mary
Thanks, for all your help. I'll see what I can do.
Feb 27 '07 #7
NeoPa
32,556 Expert Mod 16PB
Would the following not work?
Expand|Select|Wrap|Line Numbers
  1. =(Max([Engine Price Est])+Min([Engine Price Est]))/2
Or is my Maths too rusty?
Mar 2 '07 #8
MMcCarthy
14,534 Expert Mod 8TB
Would the following not work?
Expand|Select|Wrap|Line Numbers
  1. =(Max([Engine Price Est])+Min([Engine Price Est]))/2
Or is my Maths too rusty?
The median of a set of numbers is nothing to do with half the value of the numbers. It is the one (odd set) or half the value of the two (even set) number(s) that occur half way through the ordered set.

Mary
Mar 2 '07 #9
NeoPa
32,556 Expert Mod 16PB
Would the following not work?
Expand|Select|Wrap|Line Numbers
  1. =(Max([Engine Price Est])+Min([Engine Price Est]))/2
Or is my Maths too rusty?
The median of a set of numbers is nothing to do with half the value of the numbers. It is the one (odd set) or half the value of the two (even set) number(s) that occur half way through the ordered set.

Mary
The latter then :(
Mar 2 '07 #10
MMcCarthy
14,534 Expert Mod 8TB
The latter then :(
I sound so knowledgible don't I. Actually I couldn't be sure of my memory on the issue so I looked it up.

Mary
Mar 2 '07 #11
NeoPa
32,556 Expert Mod 16PB
The code you gave me is for a report that is linked to a form (a form that lets people build queries) then prints out a report of the query.
So with this being said, I want the median of only the query (over group) not over the whole table. What would the VBA code be for this? It wouldn't be the same would it?
This is a challenge!
I will write a Public Function that can be called from within a query.
When passed one of the values of the grouped field (I will assume the field name [Group] unless told otherwise) it will return the Median value for that particular group.
As it will build up the values in an internal array during the first call, the efficiency will be optimum for calling within a query as the hard work only needs to be executed once.
Mar 3 '07 #12
NeoPa
32,556 Expert Mod 16PB
Here's the code.
It can be added to an existing module (Not Class Module) or one can be created for it to go in. If adding to an existing module the two top lines should already be there so can be dropped.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Function MedianPrice(strGroup As String) As Double
  5.   Static astrGrps() As String
  6.   Static adblMedians() As Double
  7.   Dim strGrp As String, strSQL As String
  8.   Dim intGrp As Integer, intGrps As Integer
  9.   Dim intStart As Integer, intRec As Integer
  10.  
  11.   'If UBound() fails then intGrps stays as 0 ==> first call
  12.   On Error Resume Next
  13.   intGrps = UBound(astrGrps)
  14.   On Error GoTo 0
  15.   If intGrps = 0 Then
  16.     'First time through only
  17.     strSQL = "SELECT DISTINCT [Group] " & _
  18.              "FROM [All Areas with additions]"
  19.     intGrps = DCount("[Group]", strSQL)
  20.     ReDim astrGrps(1 To intGrps)
  21.     ReDim adblMedians(1 To intGrps)
  22.     strSQL = "SELECT [Group],[Engine Price Est] " & _
  23.              "FROM [All Areas with additions] " & _
  24.              "ORDER BY [Group],[Engine Price Est]"
  25.     With CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
  26.       Call .MoveFirst
  27.       strGrp = ![Group]
  28.       intStart = 1
  29.       intRec = 1
  30.       For intGrp = 1 To intGrps
  31.         While ((Not .EOF) And (![Group] = strGrp))
  32.           intRec = intRec + 1
  33.           Call .MoveNext
  34.         Wend
  35.         astrGrps(intGrp) = strGrp
  36.         strGrp = ![Group]
  37.         'Go back to middle record or first of two middle records
  38.         Call .Move(Rows:=Fix((intStart - intRec) / 2) - 1)
  39.         adblMedians(intGrp) = ![Engine Price Est]
  40.         'If two middle records process the average
  41.         If ((intRec - intStart) Mod 2) = 0 Then
  42.           Call .MoveNext
  43.           adblMedians(intGrp) = (adblMedians(intGrp) + ![Engine Price Est]) / 2
  44.         End If
  45.         'Return to record intRec
  46.         Call .Move(Rows:=Fix((intRec + 1 - intStart) / 2))
  47.       Next intGrp
  48.     End With
  49.   End If
  50.  
  51.   'Find the index of strGroup & return matching value
  52.   For intGrp = 1 To UBound(astrGrps)
  53.     If strGroup = astrGrps(intGrp) Then Exit For
  54.   Next intGrp
  55.   MedianPrice = adblMedians(intGrp)
  56. End Function
Mar 3 '07 #13

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

Similar topics

4
by: Ross Contino | last post by:
Hello to all: I have been searching the web for examples on how to determine a median value in a mySQL table. I have reviewed the article at...
2
by: William Jiang | last post by:
Recently, I will write a database application to get the median value according to the grouped condition. maybe, just like the following. SELECT Max(a1) MaxValue, Median(a1) MedianValue FROM...
2
by: Hugo L. | last post by:
I really don't know how to calculate the median. Can anybody help me?
2
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...
8
by: nick.vitone | last post by:
Hi, I'm somewhat of a novice at Access, and I have no experience programming whatsoever. I'm attempting to calculate the statistical median in a query. I need to "Group by" one column and find...
4
by: uspensky | last post by:
I have a table (cars) with 3 fields: VIN, Class, sell_price 101, sports, 10000 102, sports, 11000 103, luxury, 9000 104, sports, 11000 105, sports, 11000 106, luxury, 5000 107, sports, 11000
5
by: jonm4102 | last post by:
I'm trying to calculate the median of some numerical data. The data can only be found in a query (henceforth query 1) field I previously made, and I would prefer to calculate the median in a new...
4
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last...
7
by: Bhadan | last post by:
Hello, I have several jagged arrays which have been sorted. I'm trying to find the median of each array. Any tips appreciated. TIA. Bhads.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...

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.