ACC: Using a Query to Return Every Nth Record from a Table (95/97)ID: Q180662
|
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article shows you two methods that you can use to return every Nth
record from a table. The first method is the simplest and uses an
AutoNumber field. But this method is not always reliable. The second method
is a little more complex, but it is reliable. It uses a dynamic counter in
a query.
Option Explicit
Global Cntr As Integer
'*************************************************************
' Function: NthRec(Z As String, Nth As Integer) As String
'
' Purpose: This function will return an "X" to the query for
' every Nth record as specified by the parameter in the query.
'*************************************************************
Function NthRec(Z As String, Nth As Integer) As String
Cntr = Cntr + 1
If Cntr Mod Nth = 0 Then
NthRec = "X"
End If
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
'**************************************************************
' Function: RunNthQuery()
'
' Purpose: This function runs the SetToZero function and then
' opens the qryEveryNthRecord query in Datasheet view.
'**************************************************************
Function RunNthQuery()
SetToZero
DoCmd.OpenQuery "qryEveryNthRecord"
End Function
Query: qryEveryNthRecord
-----------------------------
Type: Select Query
Field: OrderID
Table: Orders
Field: CustomerID
Table: Orders
Field: OrderDate
Table: Orders
Field: Freight
Table: Orders
Field: NthRec([OrderID],[What Nth Would You Like Today?])
Show: No
Criteria: X
?RunNthQuery()
Keywords : QryGnrl QryHowto
Version : WINDOWS:7.0,97
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: April 28, 1999