G
Guest
I have a large database in which I'd like to find the median, not average,
value of numeric data in several fields.
value of numeric data in several fields.
navman9 said:I have a large database in which I'd like to find the median, not average,
value of numeric data in several fields.
StCyrM said:Good afternoon
The following function will calculate the Median for you. Simply copy and
paste this into a new module.
Function Median (tName$, fldName$) As Single
Dim MedianDB As Database
Dim ssMedian As Recordset
Dim RCount%, i%, x%, y%, OffSet%
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
ssMedian.Close
MedianDB.Close
End Function
Best Regards
Maurice St-Cyr
Micro Systems Consultants, Inc.
I have a large database in which I'd like to find the median, not average,
value of numeric data in several fields.
StCyrM said:Good afternoon
The following function will calculate the Median for you. Simply copy and
paste this into a new module.
Function Median (tName$, fldName$) As Single
Dim MedianDB As Database
Dim ssMedian As Recordset
Dim RCount%, i%, x%, y%, OffSet%
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
ssMedian.Close
MedianDB.Close
End Function
Best Regards
Maurice St-Cyr
Micro Systems Consultants, Inc.
I have a large database in which I'd like to find the median, not average,
value of numeric data in several fields.
MaryW said:Hi-
I am new to VBA/modules and I must be missing a step in creating the median
function. Please spell out a few things for me.
I created a module with the code listed below. Then I went to use the median
function in a query. I got the message "Undefined function 'Median' in
expression". What am I missing?
Any help is appreciated!
StCyrM said:Good afternoon
The following function will calculate the Median for you. Simply copy and
paste this into a new module.
Function Median (tName$, fldName$) As Single
Dim MedianDB As Database
Dim ssMedian As Recordset
Dim RCount%, i%, x%, y%, OffSet%
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
ssMedian.Close
MedianDB.Close
End Function
Best Regards
Maurice St-Cyr
Micro Systems Consultants, Inc.
I have a large database in which I'd like to find the median, not average,
value of numeric data in several fields.
Good afternoon
The following function will calculate the Median for you. Simply copy and
paste this into a new module.
Function Median (tName$, fldName$) As Single
Dim MedianDB As Database
Dim ssMedian As Recordset
Dim RCount%, i%, x%, y%, OffSet%
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
ssMedian.Close
MedianDB.Close
End Function
Best Regards
Maurice St-Cyr
Micro Systems Consultants, Inc.
I have a large database in which I'd like to find the median, not average,
value of numeric data in several fields.
John Spencer (MVP) said:I think you have a small problem in this function as posted. You have a line
continuation inside the quotes.
Set ssMedian = MedianDB.Openrecordset("SELECT [" & fldName$ & _
"] FROM [" & tName$ & "] WHERE [" & fldName$ & "] IS _
NOT NULL ORDER BY [" & fldName$ & "];")
I would rewrite it for clarity as
Set ssMedian = MedianDB.Openrecordset ( _
"SELECT [" & fldName$ & "] " & _
"FROM [" & tName$ & "] " & _
"WHERE [" & fldName$ & "] IS NOT NULL " & _
"ORDER BY [" & fldName$ & "]")
Good afternoon
The following function will calculate the Median for you. Simply copy and
paste this into a new module.
Function Median (tName$, fldName$) As Single
Dim MedianDB As Database
Dim ssMedian As Recordset
Dim RCount%, i%, x%, y%, OffSet%
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
ssMedian.Close
MedianDB.Close
End Function
Best Regards
Maurice St-Cyr
Micro Systems Consultants, Inc.
I have a large database in which I'd like to find the median, not average,
value of numeric data in several fields.
I hope I'm not being dense here, but I'm still having trouble using the
median in a query. Now I get the message "Wrong number of arguments used with
function in query expression"
Please help...
John Spencer (MVP) said:I think you have a small problem in this function as posted. You have a line
continuation inside the quotes.
Set ssMedian = MedianDB.Openrecordset("SELECT [" & fldName$ & _
"] FROM [" & tName$ & "] WHERE [" & fldName$ & "] IS _
NOT NULL ORDER BY [" & fldName$ & "];")
I would rewrite it for clarity as
Set ssMedian = MedianDB.Openrecordset ( _
"SELECT [" & fldName$ & "] " & _
"FROM [" & tName$ & "] " & _
"WHERE [" & fldName$ & "] IS NOT NULL " & _
"ORDER BY [" & fldName$ & "]")
Good afternoon
The following function will calculate the Median for you. Simply copy and
paste this into a new module.
Function Median (tName$, fldName$) As Single
Dim MedianDB As Database
Dim ssMedian As Recordset
Dim RCount%, i%, x%, y%, OffSet%
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
ssMedian.Close
MedianDB.Close
End Function
Best Regards
Maurice St-Cyr
Micro Systems Consultants, Inc.
I have a large database in which I'd like to find the median, not average,
value of numeric data in several fields.
John Spencer (MVP) said:Well, assuming that the function actually works (which I won't vouch for since
it is not my code), did you pass it two strings? One the name of the table,
and secondly the name of the field you want to generate the median on.
You should have a column something like:
Field: GetMedian: Median("MyTableName","MyFieldName")
Now the problem I see with this entire function is that it basically gets the
median of the field for ALL the records in the table or a query. In other
words, it does not get the median of a group or the median of a query that is
returns a limited set of records using a where clause.
You could still use it by creating a query that returns the desired record and
then use that query as the basis of what you need to generate the median values.
I hope I'm not being dense here, but I'm still having trouble using the
median in a query. Now I get the message "Wrong number of arguments used with
function in query expression"
Please help...
John Spencer (MVP) said:I think you have a small problem in this function as posted. You have a line
continuation inside the quotes.
Set ssMedian = MedianDB.Openrecordset("SELECT [" & fldName$ & _
"] FROM [" & tName$ & "] WHERE [" & fldName$ & "] IS _
NOT NULL ORDER BY [" & fldName$ & "];")
I would rewrite it for clarity as
Set ssMedian = MedianDB.Openrecordset ( _
"SELECT [" & fldName$ & "] " & _
"FROM [" & tName$ & "] " & _
"WHERE [" & fldName$ & "] IS NOT NULL " & _
"ORDER BY [" & fldName$ & "]")
StCyrM wrote:
Good afternoon
The following function will calculate the Median for you. Simply copy and
paste this into a new module.
Function Median (tName$, fldName$) As Single
Dim MedianDB As Database
Dim ssMedian As Recordset
Dim RCount%, i%, x%, y%, OffSet%
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
ssMedian.Close
MedianDB.Close
End Function
Best Regards
Maurice St-Cyr
Micro Systems Consultants, Inc.
I have a large database in which I'd like to find the median, not average,
value of numeric data in several fields.
MaryW said:Hi-
Thanks for helping through my first attempt at VBA. I corrected the query
and defined the two arguements as the table and the field names. The NEW
error message that I get is "Compile Error: User-define type is not defined".
In the module, the second line of code is highlighted
Dim MedianDB As Database
Any more suggestions?
John Spencer (MVP) said:Well, assuming that the function actually works (which I won't vouch for since
it is not my code), did you pass it two strings? One the name of the table,
and secondly the name of the field you want to generate the median on.
You should have a column something like:
Field: GetMedian: Median("MyTableName","MyFieldName")
Now the problem I see with this entire function is that it basically gets the
median of the field for ALL the records in the table or a query. In other
words, it does not get the median of a group or the median of a query that is
returns a limited set of records using a where clause.
You could still use it by creating a query that returns the desired record and
then use that query as the basis of what you need to generate the median values.
I hope I'm not being dense here, but I'm still having trouble using the
median in a query. Now I get the message "Wrong number of arguments used with
function in query expression"
Please help...
:
I think you have a small problem in this function as posted. You have a line
continuation inside the quotes.
Set ssMedian = MedianDB.Openrecordset("SELECT [" & fldName$ & _
"] FROM [" & tName$ & "] WHERE [" & fldName$ & "] IS _
NOT NULL ORDER BY [" & fldName$ & "];")
I would rewrite it for clarity as
Set ssMedian = MedianDB.Openrecordset ( _
"SELECT [" & fldName$ & "] " & _
"FROM [" & tName$ & "] " & _
"WHERE [" & fldName$ & "] IS NOT NULL " & _
"ORDER BY [" & fldName$ & "]")
StCyrM wrote:
Good afternoon
The following function will calculate the Median for you. Simply copy and
paste this into a new module.
Function Median (tName$, fldName$) As Single
Dim MedianDB As Database
Dim ssMedian As Recordset
Dim RCount%, i%, x%, y%, OffSet%
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
ssMedian.Close
MedianDB.Close
End Function
Best Regards
Maurice St-Cyr
Micro Systems Consultants, Inc.
I have a large database in which I'd like to find the median, not average,
value of numeric data in several fields.
John Spencer (MVP) said:Change code to read
DIM MedianDB as DAO.Database
Dim ssMedian As DAO.Recordset
You will also need a reference to the DAO library.
Select Tools: References from the menu
Scroll down and find rhw Microsoft DAO 3.6 Object Library (or higher version
depending on your version of Access).
If your database is not a .mdb database but is an .adp database this will still fail.