In my first reply, I thanked for providing the code which exactly what I
asked for. Unfortunately, the penultimate line which sets the filter is
causing an "object variable or With blocks variable not set" error.
Here is the code up to the error point. Note that "snno" and "fnno" are
picked up from a form from which the "click" is activated. It falls over
even if I remove reference to stno and fnno, and use your suggested code
exactly:
Private Sub cmdReport_Click()
Dim stDocName As String
Dim stno As Long, fnno As Long
Dim EMail As String
Dim WkNo As Integer
Dim FirstOccur As String
Dim X As Integer
Dim db As DAO.Database
Dim rstUnfiltered As DAO.Recordset
Dim rstFiltered As DAO.Recordset
Dim names As String
stno = varLong(Me.txtStartWeek.Value)
fnno = varLong(Me.txtEndWeek.Value)
Set db = CurrentDb
Set rstUnfiltered = db.OpenRecordset("qryStaffRequiredTimesheetsLMEMail")
rstFiltered.Filter = "[WeekNo] >= stno AND [WeekNo] <= fnno"
Set rstFiltered = rstUnfiltered.OpenRecordset
Dirk Goldgar said:
JMCS said:
I have a very complex query which I call in vba using rst =
db.OpenRecordset("xxx").
I am then trying to filter it to a range of values in one field
"[WeekNo]", but am clearly failing.
What should the next line be to apply the filter to the retrieved
recordset? Note , it will be variable,with values used from stno to
fnno.
You have to open a new recordset from the original one, after having set
the original's Filter property. For example,
Set rstUnfiltered = CurrentDb.OpenRecordset("YourQuery")
rstFiltered.Filter = "[WeekNo] > 10 AND [WeekNo] <21"
Set rstFiltered = rstUnfiltered.OpenRecordset
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)