Filtering Recordset (1 Viewer)

ria4life

Registered User.
Local time
Today, 00:45
Joined
Feb 24, 2016
Messages
40
Id like to filter thisr ecordset with a value from a form...however...the filter does not seem to do anythong...please advise.



PHP:
Function getCalendarData() As Boolean
Dim rs As DAO.Recordset





    Set rs = CurrentDb.OpenRecordset("qry_employeeAttendance", dbOpenDynaset)
              rs.Filter = "EmployeeID = [Forms]![FRM_Main]![FRM_Welcome]![user]"
 

Ranman256

Well-known member
Local time
Today, 03:45
Joined
Apr 9, 2015
Messages
4,339
You don't filter recordsets.
Filter a form with a query.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:45
Joined
Feb 28, 2001
Messages
27,140
The problem may be WHEN you apply the filter.

In this article:

https://docs.microsoft.com/en-us/of...abase-reference/recordset-filter-property-dao

You can use the Filter property to restrict the records returned from an existing object when a new Recordset object is opened based on an existing Recordset object.

(Underscoring in that quote was something I added.)

If you open the recordset first, I think it is then too late to apply the filter.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:45
Joined
Aug 30, 2003
Messages
36,124
I'd do this:

Code:
Set rs = CurrentDb.OpenRecordset("SELECT * FROM qry_employeeAttendance WHERE EmployeeID = " & [Forms]![FRM_Main]![FRM_Welcome]![user], dbOpenDynaset)
 

theDBguy

I’m here to help
Staff member
Local time
Today, 00:45
Joined
Oct 29, 2018
Messages
21,454
Hi,

Just to clarify your intention. Must you open an unfiltered recordset and then subsequently apply a filter to it? Or would opening a filtered recordset to begin with, as has already been suggested, be sufficient?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 08:45
Joined
Feb 19, 2013
Messages
16,607
Most developers would use Paul's suggest but you can assign a filtered recordset to a new recordset (or itself). Something like

Code:
Dim rst As DAO.Recordset
Dim db As DAO.Database
 
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT * FROM someTable")
rst.Filter="ID<=200"
set rst=rst.openrecordset
However there is no going back, once filtered, the records filtered out are lost - unless you assign to a difference recordset

Code:
Dim rst As DAO.Recordset
Dim db As DAO.Database
Dim Frst as DAO.Recordset
 
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT * FROM someTable")
rst.Filter="ID<=200"
set Frst=rst.openrecordset

then you can still go back to your unfiltered rst recordset. However any changes you made in Frst will not be reflected in rst.

I guess it can have it's uses but good practice is to bring the minimum number of records through in the first place.
 

ria4life

Registered User.
Local time
Today, 00:45
Joined
Feb 24, 2016
Messages
40
pbaldy:

i tried the line you recommended..however, now i get an error:
Run-Time Error 3061
Too few paremeters. Expected 1.

I verified all field names and all is correct.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:45
Joined
Aug 30, 2003
Messages
36,124
Does the query have a form parameter? If so, simplest solution is probably to wrap it in the Eval() function in the query.
 

Users who are viewing this thread

Top Bottom