Access has a TopValue function you can use.
If there are 3 measures you'll need to create 3 Functions that you'll add in a Module, 3 TOPVALUE queries of your table with each one concentrating on a different field (gross profit, total sales, etc.), and 1 Macro to run it all.
To simplify this, I created a table called tblStores, with 3 fields: Store, GrossProfit, GrossProfit Rank.
Here are the table values I used:
GrossProfit Store GrossProfit Rank
125000 Chicago
10000 Boston
5000 New York
1000 Dallas
250 Denver
100 San Francisco
I created a TOP VALUE query on this table. Here's the SQL to make it easy to see:
SELECT TOP 5 tblStores.Store, tblStores.GrossProfit, tblStores.[GrossProfit Rank]
FROM tblStores
ORDER BY tblStores.GrossProfit DESC;
Notice the TOP 5. This is basically a SELECT query with TOP 5 added after the SELECT statement. You can make this any number you want.
Then, I created a function called TopProfit() in a module. Here's that code:
Function TopProfit()
Dim MyDB As Database
Dim MySet As Recordset
Dim I As Integer
Dim R As Integer
Set MyDB = DBEngine.Workspaces(0).Databases(0)
Set MySet = MyDB.OpenRecordset("select * from [qryGrossProfit] order by [GrossProfit] DESC", DB_OPEN_DYNASET)
MySet.MoveLast
I = MySet.RecordCount
R = 12
If MySet.RecordCount > 0 Then
MySet.MoveFirst
For I = 1 To MySet.RecordCount
MySet.Edit
MySet![GrossProfit Rank] = (R - 2)
MySet.Update
MySet.MoveNext
R = R - 2
Next
End If
End Function
Lastly, I created a Macro with 1 Action set to RUNCODE and TOPPROFIT() in the Function Name line.
If you take the time to set up this sample table, query, module and macro, and you run the macro, you'll see how it works.
The result will add ranks to all but San Francisco the way you want it.
Then, if you want to track TOTAL SALES, there should be a field in THE SAME TABLE called TOTAL SALES, and you can add a field called TOTAL SALES RANK.
Create a new query called qryTotalSales and a new function in the module called TotalSales(), changing the necessary fields to match the new query. Add this Function to THE SAME Macro as the 2nd Action line and when you run this it will take care of both ranks.
Let me know if you have questions. Good luck.
Jim "Get it right the first time, that's the main thing..."
![[wavey] [wavey] [wavey]](/data/assets/smilies/wavey.gif)