Solved

Can I pass a recordset to a report?

Posted on 2016-08-27
5
42 Views
Last Modified: 2016-09-01
I have a form with a subform which contains a list of transactions filtered in various ways, by a filter which I create in code and load into the Filter property of the subform, which works.

I have a report similar to the form which I want to be able to print from a command button on the form, to contain the same data.  I have tried saving the filter from the subform and loading it into the Filter property of the subreport, but Access gives a compile error.   (Property doesn't support the method).

Can I pass the recordset from the subform to the Source Object of the subreport?

Thanks in advance,

Clive
0
Comment
Question by:CRB1609
  • 2
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
crystal (strive4peace) - Microsoft MVP, Access earned 500 total points
Comment Utility
here is code you can run to set the report filter before it is opened:
'------------------------------------ SetReportFilter
Sub SetReportFilter( _
   psReportName As String _
   , pvFilter As Variant)
'strive4peace
   'Save a filter to the specified report
   'You can do this before you send a report in an email message
   'You can use this to filter subreports instead of putting criteria in the recordset

   ' USEAGE:
   ' example: in code that processes reports for viewing, printing, or email
   ' SetReportFilter "MyReportname","someID=1000"
   ' SetReportFilter "MyAppointments","City='Denver' AND dt_appt=#9/18/16#"

   ' PARAMETERS:
   ' psReportName is the name of your report
   ' pvFilter is a valid filter string or null

   On Error GoTo Proc_Err

   '---------- declare variables
   Dim rpt As Report

   '---------- open design view of report in Hidden mode (> Access 2000)
   DoCmd.OpenReport psReportName, acViewDesign ', , , acHidden

   '---------- set object variable to report
   Set rpt = Reports(psReportName)

   '---------- set report filter and turn it on
   rpt.Filter = pvFilter
   rpt.FilterOn = IIf(Len(Nz(pvFilter, "")) > 0, True, False)

   '---------- Save and Close report
   DoCmd.Close acReport, psReportName, acSaveYes

Proc_Exit:
   '----------  Release object variable
   Set rpt = Nothing
   Exit Sub

Proc_Err:
   Resume Next

   MsgBox Err.Description, , "ERROR " & Err.Number & "  SetReportFilter"
   'press F8 to step thru code and fix problem
   Resume Proc_Exit

   'if you want to single-step code to find error, CTRL-Break at MsgBox
   'then set this to be the next statement
   Resume

End Sub

Open in new window

0
 

Author Comment

by:CRB1609
Comment Utility
Thank you Crystal.  Worked wel.l
0
 

Author Comment

by:CRB1609
Comment Utility
I don't seem to be able to award points and close this question.
0
 
LVL 18
Comment Utility
you're welcome ~ happy to help
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now