Using On-Demand Subreports
By default, subreports are run at the same time as the main report. When you view or print the main report, the subreport information is printed as well. This may result in a performance decrease because the subreport could require just as much time, if not more, to process as the main report. If performance becomes a problem, a solution is to declare the subreport so that it isn’t bound to the main report. This is called an on-demand subreport.
An on-demand subreport doesn’t print at the same time as the main report. Instead, a hyperlink that describes the subreport is shown where the subreport should appear. When the user clicks on the hyperlink the subreport is processed and shown to the user. The on-demand subreport is shown on a separate tab in the viewer.
To define a subreport as being an on-demand subreport, use the Subreport tab of the Insert Subreport dialog box. At the bottom of the dialog box is an On-Demand Subreport checkbox. It is unchecked by default. Click on it to make the subreport an on-demand subreport.
By default, an on-demand subreport is shown as a hyperlink on the main report. The text is the name of the subreport.
Figure 9-9. On-demand subreports shown as a hyperlink.
When you click on the hyperlink the subreport is processed and displayed on a new tab in the viewer. Each on-demand subreport is displayed on its own tab.
Figure 9-10. On-demand subreports shown on a separate tab.
If you feel that displaying a hyperlink isn’t professional enough, you can display a customized placeholder instead. When you are in design mode, right-click on the subreport object and choose Format Subreport. Then go to the Subreport tab that is shown in Figure 9-11.
Figure 9-11. Formatting the subreport placeholder.
There is a formula button for modifying the caption that is displayed on the main report. You can enter a simple string in this formula or customize the string by concatenating data fields into it. The rest of the tabs on this dialog box can be used to format how the placeholder looks. For example, you can change the font, the background color and the border. Figure 9-12 shows the placeholder formatted with a border and a larger font size.
Figure 9-12. The subreport placeholder.
Questions are always being posted to the forum about subreports. Here are a few more that I frequently see and I thought would be beneficial for you.
Question: My main report uses a portrait page orientation and my subreport is formatted for landscape. But when I print the report, the subreport comes out in portrait mode as well. How can I get the subreport to print in landscape mode?
Answer: You can’t. Crystal Reports uses the orientation of the main report for printing the subreport. You can’t use different page orientations for subreports.
Question: I have a few separate reports and I want to export them all to a single PDF file. How can I do this?
Answer: There are third party utilities that let you merge PDF files into a single file. I haven’t used them before, but you can use Google to search for them. Another alternative is to create an empty master report to hold the reports. Put each subreport in its own section on the master report so that they print one after the other. It can be exported to a single PDF file. The only limitation is that these subreports can’t have subreports of their own. Crystal Reports doesn’t allow nesting subreports more than one level deep.
Question: The data in my report is printed on a separate detail line for each record. But, I want to print a list of all the names on a single line going across the page. How do I do this?
Answer: Create a subreport that concatenates the fields together in one string as they are printed. The string should be a shared variable and the formula would look like this (assuming that X is the string variable):
X := X & {table.field} & " ";
Use a formula on the main report to print this shared variable after the subreport. If you want to hide the subreport, refer Tutorial 9-6.