Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Software > Access Macros > Median in Re***...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 5623 of 5947
Post > Topic >>

Median in Re****t

by =?Utf-8?B?SmFzb24=?= <Jason@[EMAIL PROTECTED] > Jul 11, 2008 at 02:59 PM

Hi,

I'm trying to use the example code from Microsoft's knowledge base to 
calculate the median in a re****t (code below).  The code works fine for me
in 
calculating the the mean of the entire dataset of my table/query.  The 
problem I run into is when I I try to calculate the same median function
in a 
re****t on a grouping, instead of returning the median for that specific 
grouping, it returns the median for the entire dataset repeatedly under
each 
grouping.  

As an example, my re****t is grouped based on Industry, and I would like to

return the median calculation of revenues as outlined below

Industry 1

Company1     $100
Company2     $180
Company3     $300
Company4    $450
Company5    $500
    Median    $300

Industry 2

Company1     $180
Company2     $250
Company3     $375
    Median      $250

In the current form, instead of returning the median for the grouping
($300 
for Industry1 and $250 for Industry2), I instead am getting the median for

the entire set of 8 records ($275 in this case) and it is displaying under

both the median for Industry1 and Industry2.  Can someone suggest how I
might 
alter the code below or possibly provide other coding that would handle
this? 
 Thanks.

Function Median(tName As String, fldName As String) As Single
  Dim MedianDB As DAO.Database
  Dim ssMedian As DAO.Recordset
  Dim RCount As Integer, i As Integer, x As Double, y As Double, _
      OffSet As Integer
  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
  If Not ssMedian Is Nothing Then
     ssMedian.Close
     Set ssMedian = Nothing
  End If
  Set MedianDB = Nothing
End Function
 




 2 Posts in Topic:
Median in Report
=?Utf-8?B?SmFzb24=?= <  2008-07-11 14:59:01 
RE: Median in Report
changliw@[EMAIL PROTECTED  2008-07-14 07:09:39 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 15:15:56 CST 2008.