Crystal Reports in the Real WorldNesting Subreports
Often a single report needs to show data from different and often unrelated pieces of information. In the next example, the report shows both customer and supplier information for a given location. This is demonstrated using a main report for the structure of Country, Region, and City hierarchy, one Subreport to show customer information, and another Subreport for supplier information. Because there is no relationship between suppliers and customers, at least one of these pieces of information must come from a Subreport. For this sample, both elements come from a Subreport. Follow these steps to explore this capability:
- Open the report designer and select Create New Report Using the Report Wizard. Click OK and browse to the datasource Xtreme Samples Database 10 datasource, expand the list of tables, and add the Customer table to the list of report tables by clicking the > button. Click Next.
- In the Fields window, add the fields Country, Region, and City to the report and click Next. In the Grouping window, add the same fields to the Group By list.
- Click Finish to complete the report. This builds the hierarchy that is used in the report.
- Change to Design mode. Move the fields in the group header to the left and indent them slightly at each lower level. Suppress all sections of the report other than the headers and expand each of the header areas below the fields. See Figure 12.11 to view what the framework of the report should look like.
Figure 12.11. Report hierarchy to act as the framework for Subreport content.
- With the framework complete, the next step is to create the content. There are different ways to create Subreports; in this instance, the reports are generated as separate files and later imported into the main report. Create the report for supplier information. From the File menu, select New. When the Crystal Reports Gallery window opens, click OK. Find the Xtreme Sample Database 10 connection, browse the list of tables for the Supplier table, and click the > button to move it to the list of selected tables. Click Next. From the Fields window, select Supplier Name and Phone and click the > button to move them to the Fields to Display. Click Finish to close the wizard. Minimize report content by changing to Design mode, moving the fields to the left edge, suppressing sections with no fields, and minimizing whitespace. Figure 12.12 illustrates a sample Subreport.
Figure 12.12. Supplier information Subreport, extra whitespace reduced to a minimum.
- Repeat step 5 for the customer table.
- Add each Subreport to the report three times, once each for Country, Region, and City. Drill into each of the six Subreports by double-clicking and again suppress blank sections.
- From the Report menu, select Section Expert. In the list of sections, select Group Footer #1 and check New Page After. Click OK to close the window. Save the report as Chap12SubsContext_Main.rpt. The design of the report should look like Figure 12.13.
Figure 12.13. Report with hierarchy and Subreports.
Preview the report and notice on Page 13 (Canada) that there is different and unrelated information for both Suppliers and Customers listed and that the information for both is specific to the location context.