Tutorial 9-6. Using Subreports for Hidden Calculations
You learned in Chapter 1 that Crystal Reports uses a 2 pass processing model to generate reports. This can cause problems if you want to share data between two objects that are both processed in pass 2. An alternative around this problem is to use a subreport for performing one of the calculations and pass the value to the main report using a shared variable. Although the subreport is basically repeating a calculation that already appears on the main report, by using a subreport you get around the limitation of not being able to share data within the second pass of the report processing model.
You can also use subreports to perform calculations that don’t appear on the main report at all. You can use them to process supporting data that needs to be used on the main report in other calculations, but that data doesn’t need to be shown. For example, someone once put a crosstab object in a subreport and then pulled the monthly subtotals out of the crosstab object and displayed them in the main report.
In both of these scenarios, you need the subreport to process data but you don’t want the subreport to be shown on the main report. The problem you will encounter is that if you add the subreport to the main report and then suppress it, its formulas won’t be calculated. The main report will have a zero value for the shared variables. The subreport object has to be shown on the main report to perform its calculations. Some people make the subreport really small so that it is almost hidden. This works pretty well, but it’s not the best solution.
There is a way to trick Crystal Reports into hiding the subreport but still perform its calculations. The key is that even though you can’t suppress the subreport object, you can suppress the sections within the subreport. Suppressing the sections will still call the subreport formulas and the shared variables will return a value. But, the subreport won’t have any output. Follow these steps on your own report to carry out this trick.
- Create the subreport and build the formula as you normally would. Test it to make sure it works.
- Click on every section in the subreport and choose the Suppress option. If you preview the report now, you should see a blank area where the subreport object is located, but there won’t be any data in it. This tells Crystal Reports to calculate the formulas in the subreport but not display them.
- On the main report, move the subreport in its own section. If you have to create a new section just for the subreport then that is fine. You want the subreport to be the only object in that section.
- The next step is to tell Crystal Reports not to show the subreport if its sections are all suppressed. On the main report, right-click on the subreport object and select Format Subreport. This opens the Format Editor dialog box.
- Click on the Subreport tab. Then click on the option Suppress Blank Subreport.
- Click the OK button to save your changes and close the Format Editor dialog box.
- On the main report right-click on the grey area to the left of the section that holds the subreport and select Section Expert.
- Select the option Suppress Blank Section and click the OK button.
When you run the report the section holding the subreport will be suppressed and the shared variables will still be calculated correctly.