Home  |  About  |   Search   

What's New
Table Of Contents
Credits
Netiquette
10 Commandments 
Bugs
Tables
Queries
Forms
Reports
Modules
APIs
Strings
Date/Time
General
Downloads
Resources
Search
Feedback
mvps.org

RunCommand Constants

Terms of Use


 

Modules: Filter a recordset

Author(s)
Dev Ashish

(Q)    How can I filter a recordset based on a criteria?

(A)    Use the Filter property of a recordset to limit it's record and then open another new recordset on it. 

        For example, in the sub sFilterRS,  I'm limiting rs2 to a single record by setting a filter on rs1.

'***************** Code Start *************
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Sub sFilterRS()
Dim db As Database, rs1 As Recordset
Dim rs2 As Recordset
    Set db = CurrentDb
    Set rs1 = db.OpenRecordset("Select * from tblRuns")
    rs1.Filter = "RunID=4"
    Set rs2 = rs1.OpenRecordset
    'rs2 should contain only one record
    MsgBox rs2.RecordCount
    Set rs2 = Nothing:  Set rs1 = Nothing
    Set db = Nothing
End Sub
'***************** Code End **************

1998-2009, Dev Ashish & Arvin Meyer, All rights reserved. Optimized for Microsoft Internet Explorer