ACC: Adding Dynamic Counter to Query to Count Records

ID: Q94397


The information in this article applies to:


SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article describes how to use a dynamic counter value in a query field for the following purposes:

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x, or the "Building Applications" manual in version 2.0.


MORE INFORMATION

Sample code to implement a dynamic counter is described later in this article. There are several limitations to this method, including the following:

The Dynamic Counter Function

The following Access Basic functions, Qcntr() and SetToZero(), can be used to implement a dynamic counter in a query. To use the Access Basic functions do the following:
  1. Create a new module.


  2. Type the following declarations in the Global Declarations section (omit the Option Explicit line if it already exists):


  3. 
    'Global Declarations Section.
    Option Explicit
    Global Cntr 
  4. Type the following functions in the module:


  5. 
    '*************************************************************
    ' Function:  Qcntr()
    '
    ' Purpose: This function will increment and return a dynamic
    ' counter. This function should be called from a query.
    '*************************************************************
    
    Function QCntr(x) As Long
       Cntr = Cntr + 1
       QCntr = Cntr
    End Function
    
    '**************************************************************
    ' Function:  SetToZero()
    '
    ' Purpose: This function will reset the global Cntr to 0. This
    ' function should be called each time before running a query
    ' containing the Qcntr() function.
    '**************************************************************
    
    Function SetToZero()
       Cntr = 0
    End Function 

Returning the Top X Records

To return the 10 most expensive orders to ship, follow these steps:
  1. Open the sample Northwind database (In Access 1.x and 2.0, called NWIND.MDB and in all other versions, Northwind.mdb).


  2. Create a new query based on the Orders table. Include the following fields:


  3. 
       Field: Order ID
       Field: Customer ID
       Field: Order Date
       Field: Freight
          Sort: Descending
       Field: Expr1: Qcntr([order id]) 
  4. From the Query menu, choose Make Table. When you are prompted for a table name, type Sorted Orders.


  5. Save the query as Make Sorted Orders.


  6. Create a new query that will be based on the Sorted Orders table after it is created. For now, close the Add Table dialog box without adding a table to the query. From the view menu, choose SQL, and then enter the following SQL statement:


  7. Select * from [Sorted Orders] where [Expr1] <= 10
  8. Save this query as Top 10 Orders, and then close it.


  9. Create the following macro:


  10. 
       Actions
       -----------
       RunCode
       SetWarnings
       OpenQuery
       OpenQuery
    
       Macro Actions
       -----------------------------------
       RunCode
    
          Function Name: SetToZero()
       SetWarnings
          Warnings On: No
       OpenQuery
          Query Name: [Make Sorted Orders]
       OpenQuery
          Query Name: [Top 10 Orders] 
  11. Run the macro. The table Sorted Orders will be created, and then the Top 10 Orders query will be displayed.


NOTE: Because the Top 10 Orders query is based on a table, you can scroll through the records and use criteria on the counter expression without causing the function to be called multiple times per record.

Creating a Top 10 Report That Includes Totals

To create a Top 10 Report that includes totals, follow these steps:
  1. Repeat steps 1-5 in the above section.


  2. Create a new report called Top 10 Orders based on the Top 10 Orders query.


  3. Create the following new macro:


  4. 
       Actions
       -----------
       RunCode
       SetWarnings
       OpenQuery
       OpenReport
    
       Macro Actions
       -----------------------------------
       RunCode
    
          Function Name: SetToZero()
       SetWarnings
          Warnings On: No
       OpenQuery
          Query Name: [Make Sorted Orders]
       OpenReport
          Report Name: [Top 10 Orders] 
  5. Run the macro.


NOTE: The counter expression will not renumber if you change the sort order or add grouping in the report because it is calculated in the make-table query. The counter expression is being used here to limit the recordset to the top 10 orders.


REFERENCES

For information about when functions are executed in queries, please see the following article in the Microsoft Knowledge Base:

Q98788 ACC: Number of Times a Custom Function Executes in a Query

Additional query words:


Keywords          : kbusage QryOthr 
Version           : WINDOWS:1.0,1.1,2.0,7.0,97
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: July 15, 1999