Posted 7 March 2019, 8:15 am ESTI have a report with two subreports. I want to run or not run each subreport based on a condition. Is this possible?
Run a subreport based on a condition in the parent
Posted by: george.viveiros on 7 March 2019, 8:15 am EST
Replied 7 March 2019, 9:37 pm ESTHi,
Yes, it is possible to choose the SubReports from the Parent Report based on some condition. For instance, you can use the Visible property of SubReport control in the Detail's Format Event to control the visibility of the subreports based on a counter variable incremented each time Detail.OnFormat event is fired. The following steps should be performed for this case:
1. Declare a counter variable in the ReportStart event.
2. Increment the counter variable in Detail Format event. This works in this case because the subreports are added to the Detail section and their visibility is controlled based on the counter value.
3. Provide the desired condition in Detail's Format event:
counter = counter + 1;
subReport.Report = subreport1;
subReport2.Report = subreport2;
subReport.Visible = true;
subReport2.Visible = false;
subReport2.Visible = true;
subReport.Visible = false;
A sample has been attached here for your reference where the reports are bound to NWIND.mdb shipped with the product.
Similarly, you can use your own conditions to view subreports conditionally.
Replied 12 March 2019, 2:47 am ESTThanks.
Replied 12 March 2019, 3:34 am ESTYour example is confusing. Your subreport names and almost identical on both side of the equal signs. Can you change the names on one side to be different?
Marked as Answer
Replied 12 March 2019, 3:22 pm ESTHello,
I have attached the modified application with changed field names. Here, "subReportFld_1" and "subReportFld_2" are the names of SubReport controls added to the report. Similarly, "firstSubRpt" and "secondSubRpt" are the instances of the code-based subreport files.