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?
12 5090
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: -
Public Function findMedianPrice() As Double
-
Dim db As DAO.Database
-
Dim rs As DAO.Recordset
-
Dim recCount As Integer
-
Dim tmpMed as Double
-
-
Set db = CurrentDb
-
Set rs = db.OpenRecordset("SELECT [Engine Price Est] FROM TableName " & _
-
"ORDER BY [Engine Price Est];"
-
-
rs.MoveLast
-
rs.MoveFirst
-
recCount = rs.RecordCount
-
-
If recCount mod 2 = 0 Then ' If even number of records
-
rs.Move recCount/2
-
tmpMed = rs![Engine Price Est]
-
rs.MoveNext
-
tmpMed = tmpMed + rs![Engine Price Est]
-
tmpMed = tmpMed/2
-
Else
-
rs.Move (recCount -1)/2
-
rs.MoveNext
-
tmpMed = rs![Engine Price Est]
-
End If
-
-
findMedianPrice = tmpMed
-
-
End Sub
-
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: -
Public Function findMedianPrice() As Double
-
Dim db As DAO.Database
-
Dim rs As DAO.Recordset
-
Dim recCount As Integer
-
Dim tmpMed as Double
-
-
Set db = CurrentDb
-
Set rs = db.OpenRecordset("SELECT [Engine Price Est] FROM TableName " & _
-
"ORDER BY [Engine Price Est];"
-
-
rs.MoveLast
-
rs.MoveFirst
-
recCount = rs.RecordCount
-
-
If recCount mod 2 = 0 Then ' If even number of records
-
rs.Move recCount/2
-
tmpMed = rs![Engine Price Est]
-
rs.MoveNext
-
tmpMed = tmpMed + rs![Engine Price Est]
-
tmpMed = tmpMed/2
-
Else
-
rs.Move (recCount -1)/2
-
rs.MoveNext
-
tmpMed = rs![Engine Price Est]
-
End If
-
-
findMedianPrice = tmpMed
-
-
End Sub
-
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?
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];")
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?
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 ... -
-
Set rs = db.OpenRecordset("SELECT [Engine Price Est] FROM [All Areas with additions] " & _
-
"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
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 ... -
-
Set rs = db.OpenRecordset("SELECT [Engine Price Est] FROM [All Areas with additions] " & _
-
"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.
NeoPa 32,556
Expert Mod 16PB
Would the following not work? - =(Max([Engine Price Est])+Min([Engine Price Est]))/2
Or is my Maths too rusty?
Would the following not work? - =(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
NeoPa 32,556
Expert Mod 16PB
Would the following not work? - =(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 :(
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
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.
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. - Option Compare Database
-
Option Explicit
-
-
Public Function MedianPrice(strGroup As String) As Double
-
Static astrGrps() As String
-
Static adblMedians() As Double
-
Dim strGrp As String, strSQL As String
-
Dim intGrp As Integer, intGrps As Integer
-
Dim intStart As Integer, intRec As Integer
-
-
'If UBound() fails then intGrps stays as 0 ==> first call
-
On Error Resume Next
-
intGrps = UBound(astrGrps)
-
On Error GoTo 0
-
If intGrps = 0 Then
-
'First time through only
-
strSQL = "SELECT DISTINCT [Group] " & _
-
"FROM [All Areas with additions]"
-
intGrps = DCount("[Group]", strSQL)
-
ReDim astrGrps(1 To intGrps)
-
ReDim adblMedians(1 To intGrps)
-
strSQL = "SELECT [Group],[Engine Price Est] " & _
-
"FROM [All Areas with additions] " & _
-
"ORDER BY [Group],[Engine Price Est]"
-
With CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
-
Call .MoveFirst
-
strGrp = ![Group]
-
intStart = 1
-
intRec = 1
-
For intGrp = 1 To intGrps
-
While ((Not .EOF) And (![Group] = strGrp))
-
intRec = intRec + 1
-
Call .MoveNext
-
Wend
-
astrGrps(intGrp) = strGrp
-
strGrp = ![Group]
-
'Go back to middle record or first of two middle records
-
Call .Move(Rows:=Fix((intStart - intRec) / 2) - 1)
-
adblMedians(intGrp) = ![Engine Price Est]
-
'If two middle records process the average
-
If ((intRec - intStart) Mod 2) = 0 Then
-
Call .MoveNext
-
adblMedians(intGrp) = (adblMedians(intGrp) + ![Engine Price Est]) / 2
-
End If
-
'Return to record intRec
-
Call .Move(Rows:=Fix((intRec + 1 - intStart) / 2))
-
Next intGrp
-
End With
-
End If
-
-
'Find the index of strGroup & return matching value
-
For intGrp = 1 To UBound(astrGrps)
-
If strGroup = astrGrps(intGrp) Then Exit For
-
Next intGrp
-
MedianPrice = adblMedians(intGrp)
-
End Function
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: Hugo L. |
last post by:
I really don't know how to calculate the median. Can anybody help me?
|
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: 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...
|
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
|
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...
|
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...
|
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.
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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,...
| |