Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

12.01 Linking Options

Subreports are used to create multiple views of data on a single report. A limitation of the standard report is that only a single view of the data can be displayed. Rather than create one or more reports to present additional views of the data, subreports are used to present data that is independent of the main report or present multiple parent-child relationships.

From a functional and design standpoint, a subreport is virtually identical to a standard report. It has the same layout as a standard report and it uses the same report objects. The subreport differs from a standard report in that it is an object on a report. Thus, it is part of another report.

Adding subreports to a main report requires knowledge of three things: the options for linking subreports to the main report, how to add and edit a subreport object and whether it should be bound to the main report.

Linking Options

The most important aspect of subreports is the various options for linking them to the main report. Subreports are either linked or unlinked to the main report. A linked subreport relies upon the main report to tell it what to print. An unlinked subreport doesn’t use any data from the main report to determine what to print.

If the subreport is linked to the main report, the subreport’s data is dependent upon the data in the main report. For example, let’s say that a subreport is linked to the main report via the Customer Id. The subreport’s data will only display records related to the current Customer Id on the main report. When the Customer Id on the main report changes, so will the detail data on that is displayed in the subreport. When the subreport is linked to the main report, the main report effectively acts as a filter for the subreport.

When the subreport is unlinked, it is independent from the main report. For example, assume that the main report is an employee sales report. It is grouped by sales person and it is broken apart to be distributed to each sales person. Within the group header is a chart showing the company’s overall sales for the month. This chart will be added to the main report as an unlinked subreport. The chart prints the same data no matter which sales person is currently being printed.

For subreports that are linked to the main report, the main report needs a method of telling the subreport what data to print. One way of doing this is using a parameter field that is populated with information from the main report. Another way is to use shared variables. Shared variables aren’t used for filtering data, but they are used in formulas in the report. Table 12-1 is a summary of the different linking options between a main report and its subreports. A more thorough description of these options and related examples are listed after the table.

Table 12-1. Subreport linking options

Linking Option Description
Linked with a data field A field from the main report is passed to the subreport and this is used for filtering records. If the field is from a PC database (e.g. MS Access) then it must be indexed.
Linked with a formula field A formula’s value from the main report is passed to the subreport and this is used to filter records. Used to link to non-indexed fields in PC databases (e.g. MS Access, Excel).
Unlinked The subreport is not connected to the main report. There is no data passed between the main report and the subreport. The subreport uses a data source that is independent of the parent report. This is used for combining unrelated reports into a single report.
Unlinked and using a formula field A formula’s value from the main report is passed to the subreport, but it doesn’t effect record selection. It can be used for displaying non-critical data on the subreport.
Unlinked and using global variables Multiple variables are used to pass data back and forth between the parent report and the subreport. This lets the parent report keep track of what the subreport is printing.