Rank order fields in record (Access)

Posted on 2009-04-10
Last Modified: 2013-11-27
Hello, everyone.  Yet another Access/DAO/data question for the masses....

I need to determine rank-order data in a number of fields within a specific record (Access 2003 database).  Actually, I need to find the top 3 highest values within these fields.  Would be easy if these values were in columns (could pull "TOP 3"), but as they fall within fields within the

I'm actually pulling this data out of Access into an Excel spreadsheet using DAO.  I've been toying around with some methods, but they end up long and drawn out.  I have current written a Sub in Access to "transpose" these fields into rows in a "work" table to pull what I need.  Example of the table I have:
VAR   MR1   MR2   MR3  MR4 .... MR12  (column headings)
TOT    25       15      30     10          3
BUS    10                 15     30         40
LOC    5         17      33                  12
(this is only a small section of the table, there are other fields around these, but these are the ones I need to work with - their will be many more lines)

How, if possible, in "memory" (a recordset object, array, or some such) can I determine the highest three scores and, if possible, the columns/fields they are in "MR#" ?  The results will be used to color-shade the corresponding cells in the Excel spreadsheet.

The Sub I've written spits out a "work" table that looks like:
LineNum   TOT    BUS   LOC    (col headings/field names)
      1          25       10      5
      2          15                17
      3          30        15     33
      4          10        30
      12         3         40     12 this way, I"m assuming, I can pull the "TOP 3" using SQL in a DAO recordset definition to get what I need (any way to also pull the LineNumber along with the TOP 3 scores?).

Would be great if this can all be done with the recordset object or an array w/o having to resort to another table...

Thank everyone, for any ideas....
Question by:CThomp2005
  • 5
  • 3
LVL 119

Expert Comment

by:Rey Obrero
ID: 24115511
? TOP 3 of what?
from your original table, which are the TOP 3?


Author Comment

ID: 24115602
Sorry, capricorn1, I guess I didn't explain that clearly...

For each original row/record, the highest three values of all the MR fields (1-12)...  So, for the original TOT line, I would get back "30" "25" and "15".  Preferably, I could also get the fields they're in ("3" "1" "2" or even "MR3" "MR1" "MR2").  Ordering of the returned top 3 is not necessary, just need to find the highest 3 values.

I have a sense this is "simple", but just can't get my head wrapped around it.  I'm overthinking it, I think..
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 100 total points
ID: 24115688
using your "work" table (since you already have it)

select top 3 lineNum,tot
from worktable
order by tot desc

Author Comment

ID: 24116233
Thanks much for the response, capricorn1....

A couple of questions...  How could I go about doing this without using the work table?  Concern (which I've run into before) is that the data gets updated periodically, and if I forget to run the Sub to recreate the work table, I deal with old data.  Any way to do this directly in a rs or array?

Next... I see the sql you threw out - along my same lines of thinking, except that I'm not horribly familiar with the "top 3" clause (know about it - never actually used it).  By doing "top 3" against both LineNum and Tot, will that (in some way) pull a "top 3" for LineNum, also?  Just curious here, what would (small example, if poss) would the resultant recordset/array look like after this (before it hits the "order by")?

Thanks very much.  I'll be keeping tabs on this thread pretty closely as I'm at this point in my responses should be quick...
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

LVL 119

Expert Comment

by:Rey Obrero
ID: 24116345
you have to do this using VBA codes in a user define function,
create a function, something like this

function getMax(paramarray args()) as double
dim vMax as double, j
for j=1 to ubound(args)
  if vMax < args(j) then
  end if

end function

to use in a query

select VAR,getMax([MR1],[MR2],....[MRn])

that will get you the max value for each row

just tweak it to get three values


Author Comment

ID: 24116550
OK...I understand where you're going with this...  I get the "select...".  What I'm not sure about is how to "tweak it to get three values"... How would I tweak to get the next two values (2nd and 3rd) ?

Sorry to be dense about this...  Kinda thought there was a way to just read into an array and sort the array descending, and pull out the first three elements...or same with a rs...


Author Comment

ID: 24117046
Moving on...

After playing around on my end, I've decided to stick with the "use work table and pull top 3 via SQL" trick in your first response...

A slightly off-topic question, considering the solution I'm going with...  From Excel VBA, how can I "run" the Sub that creates the work table?  Can it be done without having the db open?  Theory being, if I can run the code to create the table prior to pulling the top3 lines out, I know I'll be working with the most current data (in the work table).

Thanks again.

Accepted Solution

CThomp2005 earned 0 total points
ID: 24143189
As a final followup....  as an alternative, I've also tracked down some code to run a sort on an array created by stepping through these fields...  Sooo...

for x = 1 to 12
  ary(x) = .Fields("MR" & x)
next x create the array, then pass it to the "bubble sort" code (set up as a function) to sort it descending.  A subsequent routine steps through the "top" three elements (ary(1) through ary(3)) to pull out the values I need.

Not sure which I'll end up the end...

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Creating and Managing Databases with phpMyAdmin in cPanel.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now