Log in

View Full Version : Solved: Printing report from Form Recordset



Kicker
10-18-2005, 07:39 PM
I have a form that displays records based on various options such as date, status, etc. Everytime one of the options changes, the form uses a docmd to apply a filter. Works great.

I also have a report that prints perfectly based on the basic query used for the form before any docmd filters are applied.

What I need to do is print the report based on the modified recordset while the form is open. For example, I want to print separate reports for different dates and/or status codes. :dunno :banghead:

chocobochick
10-19-2005, 06:17 AM
Just apply the form's filter to the report. If the form might not always be open, you can cycle through the Forms collection to see if it's there, like so:

Option Explicit
Private Sub Report_Activate()
Dim f As Form
For Each f In Forms
If f.Name = "NameOfMyForm" Then
Me.Filter = f.Filter
Exit Sub
End If
Next
End Sub

xCav8r
10-19-2005, 07:30 AM
chocobochicken's approach is the right one. If you're opening a report from a filtered form and you want to apply the same filter to the report, then you should use the OpenReport method of the DoCmd object with a FilterName argument: DoCmd.OpenReport "ReportName",,Me.Filter Alternatively, if the filter has been saved as a query, substitute its name for Me.Filter in the FilterName argument: DoCmd.OpenReport "ReportName",,"FilterName"

For your understanding, here's what's happening behind the scenes...


If you have a form based on a table with four fields (ID, StartDate, EndDate, StatusCode), that's essentially like using either one of these queries:
SELECT ID, StartDate, EndDate, StatusCode FROM tblSample; or
SELECT * FROM tblSample; Both queries return all records in the table called tblSample. When you apply a filter, it has the effect of adding a WHERE clause to the query. For example, say you only want to display the records in the table where the StatusCode equals "Incomplete":
SELECT * FROM tblSample WHERE StatusCode='Incomplete'; Another approach to filtering your report (to the ways I suggested at the beginning of this post) would be to use the WhereCondition argument of the OpenReport method: DoCmd.OpenReport "ReportName",,,"StatusCode='Incomplete'" Or, say you only want to display the records in the table where the StartDate is between two dates the user specifies:
SELECT * FROM tblSample WHERE StartDate BETWEEN #1/1/05# AND #10/15/05#; Yet another approach would be to pass this information to the report in the OpenArgs argument. You could then use the report's load event to change the recordsource of the report to this SQL statement (Event procedure not shown for changing the recordsource of the report.)DoCmd.OpenReport "ReportName",,,,,"SELECT * FROM tblSample WHERE StartDate BETWEEN #1/1/05# AND #10/15/05#;"Or, say you only want to display the reocrds in the table where the StartDate is after some date but the EndDate is before some other date:
SELECT * FROM tblSample WHERE StartDate > #1/1/05# AND EndDate < #10/15/05#; You could modify a query made specifically for the report that looked to the form to get these values, but I don't think this approach is as good as the others, so I'll just leave it at that. :)

Kicker
10-19-2005, 03:43 PM
I was on the right track. Just hadn't been able to get there until now. Thanks a lot. I have printed this set of responses for keeping in my snipit folder.