Solved

Can I pass a recordset to a report?

Posted on 2016-08-27
5
82 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 21

Accepted Solution

by:
crystal (strive4peace) - Microsoft MVP, Access earned 500 total points
ID: 41772920
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
ID: 41773403
Thank you Crystal.  Worked wel.l
0
 

Author Comment

by:CRB1609
ID: 41773406
I don't seem to be able to award points and close this question.
0
 
LVL 21
ID: 41779953
you're welcome ~ happy to help
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

705 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