We help IT Professionals succeed at work.

Report filter not changing when sub routine runs

GPSPOW
GPSPOW asked
on
Attached is the module I am running to loop through a report to run selective reports based on a query.  The report has a group equal to the Sueprvisor in the main query.  The controlling query has 10 records with just the Supervisor, a character field.

When I step through the module, the report outputs to the separate pdf files as I expected.  However, when I run the module, the data is only for the last Supervisor processed.  The file name changes as expected, but not the data.

I would appreciate any help in correcting this.

Thanks

glen
Public Sub Supervisor()
    Dim MyRs As DAO.Recordset
    Dim rpt As Report
   
    Set MyRs = CurrentDb.OpenRecordset("qry_Active_Employee_Supv")
    DoCmd.OpenReport "rpt_Active_Employees_By_Department", acPreview, , , acHidden
    Set rpt = Reports("rpt_Active_Employees_By_Department")
   
    With MyRs
        .MoveFirst
            Do While Not .EOF
                ' open report hideen and filtered
                ' save the hidden report as a PDF
                 rpt.Filter = "[Supervisor] = " & Chr(34) & !Supervisor & Chr(34)
                 rpt.FilterOn = True
                DoCmd.OutputTo acOutputReport, "rpt_Active_Employees_By_Department", acFormatPDF, "\\pmcfs\groups\HR\HR Extractions\ActiveEmployees\Active_Empl_List_" & !Supervisor & ".PDF"
    
               .MoveNext
            Loop
    End With
    rpt.Filter = " "
    rpt.FilterOn = False
End Sub

Open in new window

Comment
Watch Question

SimonPrincipal Analyst

Commented:
You could try adding these line to ensure the filter is applied each time and that the PDF generator has time to execute for each loop...
                ' save the hidden report as a PDF
                 rpt.FilterOn = False
                 rpt.Filter = "[Supervisor] = " & Chr(34) & !Supervisor & Chr(34)
                 rpt.FilterOn = True
                DoCmd.OutputTo acOutputReport, "rpt_Active_Employees_By_Department", acFormatPDF, "\\pmcfs\groups\HR\HR Extractions\ActiveEmployees\Active_Empl_List_" & !Supervisor & ".PDF"
                 DoEvents
SimonPrincipal Analyst

Commented:
I also use a KillTime function like this if I need to pause a routine to allow for other things to execute
Function KillTime(t As Single)
Dim sTime As Single, ctr As Long
sTime = Timer
Do Until Timer > sTime + t
DoEvents
ctr = ctr + 1
Loop
KillTime = ctr
End Function

Open in new window


You can use this in place of the DoEvents line if necessary
e.g. KillTime(0.5) or KillTime(2) to yield to the operating system for 1/2 or 2 seconds respectively.

Author

Commented:
SELECT qry_Active_Employees_By_Department.Supv AS Supervisor
FROM qry_Active_Employees_By_Department
GROUP BY qry_Active_Employees_By_Department.Supv;

Open in new window


SELECT qry_Active_Employees_By_Department.Supv AS Supervisor, qry_Active_Employees_By_Department.DeptID, qry_Active_Employees_By_Department.DeptName, qry_Active_Employees_By_Department.JobCodeID, qry_Active_Employees_By_Department.PositName, qry_Active_Employees_By_Department.LastName, qry_Active_Employees_By_Department.FirstName, qry_Active_Employees_By_Department.EmployeeID, qry_Active_Employees_By_Department.EmpType, qry_Active_Employees_By_Department.BaseRate, qry_Active_Employees_By_Department.DOB
FROM qry_Active_Employees_By_Department
ORDER BY qry_Active_Employees_By_Department.Supv, qry_Active_Employees_By_Department.DeptID, qry_Active_Employees_By_Department.JobCodeID, qry_Active_Employees_By_Department.LastName;

Open in new window


Public Sub Supervisor()
    Dim MyRs As DAO.Recordset
    Dim rpt As Report
   
    Set MyRs = CurrentDb.OpenRecordset("qry_Active_Employee_Supv")
    DoCmd.OpenReport "rpt_Active_Employees_By_Department", acPreview, , , acHidden
    Set rpt = Reports("rpt_Active_Employees_By_Department")
   
    With MyRs
        .MoveFirst
            Do While Not .EOF
                ' open report hideen and filtered
                ' save the hidden report as a PDF
                 rpt.FilterOn = False
                 rpt.Filter = "[Supervisor] = " & Chr(34) & !Supervisor & Chr(34)
                 rpt.FilterOn = True
                DoCmd.OutputTo acOutputReport, "rpt_Active_Employees_By_Department", acFormatPDF, "\\pmcfs\groups\HR\HR Extractions\ActiveEmployees\Active_Empl_List_" & !Supervisor & ".PDF"
                DoEvents
               .MoveNext
            Loop
    End With
    
End Sub

Open in new window


I tried using the Rpt.filter=False & DoEvents but they had no effect on the output.  The Report ran with all 24 supervisors getting the same report.

The First code snippet is the Query to create the RecordSet.
the Second code snippet is the Query used by the report.
The Third code snippet is the module used to generate the report pdf's.

Would it be better to insert a strSql string within the module to create a report output query with a Where clause to designate the Supervisor value to print as it loops through the Supervisor query?

Thanks

Glen
SimonPrincipal Analyst

Commented:
Hi Glen, I possibly misundertood your problem. I thought you meant it worked as intended when you stepped through it as opposed to letting it run at full speed.

Author

Commented:
It did.

Is my syntax correct where I use Rpt.FilterOn=False and the  DoEvents?

There are a total of 58 pages in the report when I run it without a Supervisor filter.  For each output, the name changes for the file name but the data stays the same.

Thanks

Glen
Principal Analyst
Commented:
This works. Open and close the report within the loop, using a where clause instead of a filter:
Public Sub Superevisor()
    Dim MyRs As DAO.Recordset
    Dim rpt As Report
   
    Set MyRs = CurrentDb.OpenRecordset("qry_Active_Employee_Supv")
       
    With MyRs
        .MoveFirst
            Do While Not .EOF
                'Open report with where clause to limit to single supervisor and hidden
                DoCmd.OpenReport "rpt_Active_Employees_By_Department", acPreview, , "[Supervisor] = " & Chr(34) & !Supervisor & Chr(34), acHidden
                Set rpt = Reports("rpt_Active_Employees_By_Department")
                DoCmd.OutputTo acOutputReport, rpt.Name, acFormatPDF, "\\pmcfs\groups\HR\HR Extractions\ActiveEmployees\Active_Empl_List_" & !Supervisor & ".PDF"
                DoCmd.Close acReport, rpt.Name
               .MoveNext
            Loop
    End With
End Sub

Open in new window

Author

Commented:
Perfect!!

Thank you very much.

Glen