Solved

Can I pass a recordset to a report?

Posted on 2016-08-27
5
52 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 19

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 19
ID: 41779953
you're welcome ~ happy to help
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

920 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

12 Experts available now in Live!

Get 1:1 Help Now