Re: How do I retrieve the median value of a field?
From: MaryW (MaryW_at_discussions.microsoft.com)
Date: 11/01/04
- Next message: rgrantz: "Re: No of months and days between 2 dates"
- Previous message: John Vinson: "Re: automatic reordering for report"
- In reply to: John Spencer (MVP): "Re: How do I retrieve the median value of a field?"
- Next in thread: Douglas J. Steele: "Re: How do I retrieve the median value of a field?"
- Reply: Douglas J. Steele: "Re: How do I retrieve the median value of a field?"
- Reply: John Spencer (MVP): "Re: How do I retrieve the median value of a field?"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 1 Nov 2004 11:45:01 -0800
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)" wrote:
> 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.
>
> MaryW wrote:
> >
> > 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)" wrote:
> >
> > > 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.
> > > > >
> > >
>
- Next message: rgrantz: "Re: No of months and days between 2 dates"
- Previous message: John Vinson: "Re: automatic reordering for report"
- In reply to: John Spencer (MVP): "Re: How do I retrieve the median value of a field?"
- Next in thread: Douglas J. Steele: "Re: How do I retrieve the median value of a field?"
- Reply: Douglas J. Steele: "Re: How do I retrieve the median value of a field?"
- Reply: John Spencer (MVP): "Re: How do I retrieve the median value of a field?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|