Subreports

In traditional banded-style report writers, reports that can be printed from a single source of data are quite easy to create. But if the content of the report consists of information from several different sources of data, the choices become quite limited. One option is to use SQL to join the data together into one ‘virtual table’, and then build the report based on this table. If many tables are involved, the performance of this approach can be prohibitive.

Delphi provides an alternative to this approach by allowing linkages to be established between data access objects. Within ReportBuilder we can use free-form subreports to take advantage of the many configurations these data access objects make possible.

Single Data Source

A single data source can be connected directly to the report via the DataPipeline property. When printed, the report will generate one detail band for each record provided by the data source.

Independent Data Sources

Here we have three sources of data with no linkages between them. In this report we want to print all of the customers, then all of the products, and then all of the vendors. The report needs to fit together like a book, with each data source providing a ‘chapter’. Here we use the main report to launch a subreports for each of the data sources. The subreports would be set with a PrintBehavior of section, which means that each subreport would start a new page, generate a set of pages as necessary to traverse all of the data of the data source and then return control to the main report. The main report is not connected to any data source, so we would manually stop the report generation process by coding an event handler in the detail band AfterPrint event. This event handler would call the Report.DataTraversalCompleted procedure, which would stop the report generation process after all of the subreports have completed printing.

Master Data Source with Single Detail Data Source

In this scenario, the master data is connected to the detail data via a field or set of fields. It is assumed that this connection results in multiple detail records being selected for each individual master record. The master data source is assigned to the report, and the detail data source is assigned to the detail band of the report. The report steps through each of the master records and generates one detail band for each record in the detail data source. If there are no corresponding detail records for a given master record, the report generates a blank detail band.

Master Data Source with Nested Detail Data Sources

Here we have the master data source containing a list of customers. Each customer has multiple orders; each order has multiple products, and each product has multiple potential vendors. This configuration is called ‘nested’ because each set of records is selected based on the linkage established with the previous data source.

The first two data sources can be handled as in the standard Master/Detail arrangement. The customer data source is assigned to the report, and the order data source is assigned to the detail band. The next two data sources can be handled by a subreport. A subreport has all of the capabilities of a regular report – the only difference is that it can be placed within the band of another report. In this case, we place the subreport in the detail band of the main report. It will then print once for each order record. Within the subreport, we assign the report to the part data source and the detail band to the vendor data source.

By configuring the report in this manner, the following information will be printed: all customers, all orders for each customer, all parts for each order, and all vendors for each part.

Master Data Source with Multiple Independent Data Sources

Here we have the order table. Each order has many order items. For each order there is also a group of vendors that can supply the products for that order. Both of these data sources are linked to the master data source, as opposed to being nested within one another; therefore, the data sources are ‘independent’. This type of data can be handled by placing two subreports in the detail band. The first subreport can be linked to the order item data source, and the second subreport can be linked to the vendor data source. In order to print the vendor data after the order item data, we would link the vendor subreport to the order item subreport via the ShiftRelativeTo property.