Accessing controls on a subreport

  • Thread starter Thread starter Roger Withnell
  • Start date Start date
R

Roger Withnell

How do I access controls on a subreport?

I'm using: Reports("Analysis report").Report("Complaints Analysis
subreport")!lblComplaint.Caption = "Bunions"

to set a caption, which doesn't work, and I also need to set the
RecordSource and ControlSource(s).
 
Roger said:
How do I access controls on a subreport?

I'm using: Reports("Analysis report").Report("Complaints Analysis
subreport")!lblComplaint.Caption = "Bunions"

to set a caption, which doesn't work, and I also need to set the
RecordSource and ControlSource(s).

Where is your code running? The things you're describing can only be
changed in the Open event of the report itself (unless you open the report
in design view).
 
The code is running is a sub triggered from a form command button.

First I open the report with: DoCmd.OpenReport "Analysis report",
acViewPreview

and then try to set the caption with: I'm using: Reports("Analysis
report").Report("Complaints Analysis subreport")!lblComplaint.Caption =
"Bunions"

which fails, though it doesn't give any errors. Is the code correct?

And, what is the correct order of statements?

If I do
DoCmd.OpenReport "Analysis report", acViewPreview
Reports("Analysis report").Report("Complaints Analysis
subreport")!txtComplaint.ControlSource = txtAnalysisComplaint
Reports("Analysis report").Report("Complaints Analysis
subreport")!lblComplaint.Caption = txtAnalysisComplaint
the error is "can't set a control source in print preview"

If I do
Reports("Analysis report").Report("Complaints Analysis
subreport")!txtComplaint.ControlSource = txtAnalysisComplaint
Reports("Analysis report").Report("Complaints Analysis
subreport")!lblComplaint.Caption = txtAnalysisComplaint
DoCmd.OpenReport "Analysis report", acViewPreview
the error is "Analysis report" misspelled or is not open.

Very much appreciate your help.
 
Roger said:
The code is running is a sub triggered from a form command button.

First I open the report with: DoCmd.OpenReport "Analysis report",
acViewPreview

and then try to set the caption with: I'm using: Reports("Analysis
report").Report("Complaints Analysis subreport")!lblComplaint.Caption
= "Bunions"

which fails, though it doesn't give any errors. Is the code correct?

And, what is the correct order of statements?

It's not a question of order. Unless you open the report in design view
(NOT what I would recommend) the only place that the code can run to change
those properties is in the report's code module in the Open event. Think
of it as a "Pull" instead of a "Push".

When the Report opens its Open event runs and that code has to decide how to
set those properties based on data it can pull from other objects. Those
other objects can be public variables, controls on open forms or data stored
in tables.

If you need to do this for subreports then the code needs to be in the Open
event of the subreport (which actually fires before the Open event of the
parent report).
 
Thanks for this, but I still cannot get it to work.
In summary, I am opening a report with a sub in a form and trying to put
information from the form into the report.

I have taken your suggestion and built a public function in a module of the
report and call that function from the form sub. The simple test sub in the
form is:

DoCmd.OpenReport "Analysis report", acViewPreview
Reports![Analysis Report].SetAnalysisReport

The simple test function is:

Public Function SetAnalysisReport() As Variant
MsgBox (Me!Label5.Caption) 'Caption = Hello
Me!Label5.Caption = "Goodbye"
MsgBox (Me!Label5.Caption) 'Caption = Goodbye
End Function

The caption is changed but 'Hello' is still displayed on the form.

I'm clearly doing something fundamentally wrong here.

Many thanks in anticipation for your advice.
 
Roger said:
Thanks for this, but I still cannot get it to work.
In summary, I am opening a report with a sub in a form and trying to
put information from the form into the report.

I have taken your suggestion and built a public function in a module
of the report and call that function from the form sub. The simple
test sub in the form is:

DoCmd.OpenReport "Analysis report", acViewPreview
Reports![Analysis Report].SetAnalysisReport

No. Once more...the code HAS to be executed IN the Open event of the report.
Any later than that and it is too late. In the above two lines of code every
event in the report will have run before the second line is executed. That is
too late.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top