Print subreport from form (Access 2016)

786 Views Asked by At

I have a Parent Form with a Tab Control, each Tab holding one Report. I am trying to put a command button on my main form that will print the Report on the Active Tab. I am strongly trying to avoid putting a "Print" button on EVERY report. So far I have done the following:

1.I am using this line of code to open selected report in print preview:

DoCmd.OpenReport Right(Me!Rpt0.SourceObject, Len(Me!Rpt0.SourceObject) - 7), acViewPreview

It looks so complicated because when you have a report as a subform the source object property is Report.MyReport so I had to eliminate the "Report." part.

So far so good.

2.I have also named all the controls holding the reports according to the index of the tab that holds them. So, Report on page 1 is Rpt0, on page 2 is Rpt1 and so on.

  1. I am trying to replace the "Rpt0" part on that line of code with an expression looking like this:

    myexp= "Rpt" & Me!TabCtl1.TabIndex
    

    So the above code becomes:

    DoCmd.OpenReport Right(Me!myexp.SourceObject, Len(Me!myexp.SourceObject) - 7), acViewPreview    
    

    and the active report is chosen each time.

My problem is with the Dim As and Set.. I am not familiar with them and it seems that whatever combination I try I get an "Object required" error, or or run-time error '2465': can't find field "myexp".

Thank you in advance

1

There are 1 best solutions below

2
On BEST ANSWER

It could be:

DoCmd.OpenReport Right(Me(myexp).SourceObject, Len(Me(myexp).SourceObject) - 7), acViewPreview