## Lesson 8: Creating a report that contains a subreport

Here we need to create a report that contains a subreport to show the order detail information, the related customers and employees information.

This lesson shows a report combined with another report. When adding report A into report B, the report A is referred to as a subreport, while report B is considered as a primary report. We will first create a report set that contains two reports - one is about orders detail information, and the other is about a tabular report with two charts of different types.

In this lesson, we take the first report in the report set as the primary report, and the second one as the subreport. So that we can insert the second report to the first one to show the sales total by orders in different order ID groups.

- Task 1: Create a report set that contains two reports
- Task 2: Insert a subreport to the report

### Task 1: Create a report set that contains two reports

Before taking this task, make sure you have enabled the Insert field name label with field option in the Options dialog as noted at the end of Lesson 3. Otherwise, the name labels will not be inserted together with the fields when you add fields to the report.

1. Click File > New > Report Set on the menu bar.
2. In the New Report Set dialog, clear the text in the Report Title text box, select Standard Banded Report from the layout box, then click OK.

   Be sure that the JinfonetGourmetJava.cat is specified as the current catalog because this is the catalog we used in the previous lessons. By default it should appear in this dialog. For information about specifying this catalog, see Task 1, Step 3 of Lesson 1.

3. In the Data screen of the Standard Banded Report Wizard, check the New radio button, select OrdersReport from the Imported SQLs node of Data Source 1, then click Finish.

   A report with a blank standard banded object is created as follows:

For users who wish to write their own SQL statement, JReport enables them to put the SQL statement into a file and then load them from this file. SQL files can work like queries in JReport.

In this lesson, we need to group data by Order IDs, so we will add a group to the banded object.

1. Right-click the banded object and then select Banded Object Wizard from the shortcut menu.
2. In the Group screen of the Banded Object Wizard, add the DBField Orders_Order ID as the group by field, then click Finish.

3. Resize the GroupHeader panel of the banded object, drag the fields Customer_ Name, Orders_OrderDate, Contact_LastName and Orders_PaymentReceived from the Resource View panel and drop them into it.
4. Edit the Customer_ Name, Orders_OrderDate, Contact_LastName and Orders_PaymentReceived name labels to Customer Name, Order Date, Employee Last Name and Payment Received one by one.
5. Align the newly-added objects in the GroupHeader panel as follows:

6. Resize the group by field in the GroupHeader panel, add a label ahead of it and edit its text to "Order ID:".

7. Select the Order Date DBFields and set its Format property to MM/dd/yy.
8. Select the BandedPageHeader panel, click Insert > Label to add a label in it and edit its text to Order Details. The label will then be shown in every page as the report title.
9. Hide the BandedHeader, Detail, GroupFooter, BandedPageFooter and BandedFooter panels one by one by right-clicking the panel and selecting Hide from the shortcut menu.
10. Format labels and DBFields in the report to improve their appearance. Here, we will not describe the formatting process in detail, you can refer to Task 3 of Lesson 1 for assistance.

    Then the first report in the report set is created to show the order detail information as follows in design mode:

    And the report looks as follows in view mode:

11. On the report tab bar, right-click the tab of the report and select Rename from the shortcut menu to rename the report as OrderDetailInformation.

Next, we will create the second report in the report set.

1. Click File > New > Report.
2. In the New Report dialog, clear the text in the Report Title text box, select Tabular Report from the layout box, then click OK.
3. In the Tabular Wizard, keep the default settings and click Finish. A report with a blank 2*2 tabular is created.
4. Merge the tabular cells into two rows and adjust them in order to place the bar chart and pie chart vertically in the report.

Next, we will insert the two charts into the tabular cells and have them share the same dataset as the OrdersDetailInformation report.

1. Select the first tabular cell and click Insert > Chart on the menu bar.
2. In Data screen of the Create Chart wizard, check the New radio button, select OrdersReport from the Imported SQLs node of Data Source 1, then click Next.
3. In the Type screen, select the Clustered Bar 2-D chart type and click Next.
4. In the Display screen, add the summary Sum_SQLProductSalesbyOrderID to the Show Values box, then Orders_Order ID will then be added automatically to the Category box. Click Finish to create the chart.
5. When a box attached to your mouse cursor, click the tabular cell to place the chart there.

6. Select the second tabular cell and insert a pie chart in it which uses the same dataset and displays the same data with the bar chart.

7. Right-click the bar chart and select Add Label from the shortcut menu. A label is then added to the chart.

8. Move the label to the top of the chart, then right-click it and choose Format Label from the shortcut menu to display the Format Label dialog.
9. In the General tab, change the label text to Current Orders for this Customer.
10. Switch to the Font tab, change Font to Arial, set Font Size to 12pt, Font Color to #808080, select Bold from the Style drop-down list and click OK.
11. Add a label to the pie chart and format it using the same way and edit its text to Current Orders for this Employee.
12. Select ChartObject and ChartObject1 in the Report Inspector and change their Border Type properties to None; change the property Border Type to None for ChartLegendObject and ChartLegendObject1 as well.
13. Further format the two charts to improve their appearance if desired. For details about formatting chart, you can refer to Task 2 of Lesson 4.

    Now the second report in the report set is created to show the sales total by orders.

14. On the report tab bar, right-click the tab of the report containing the charts and select Rename from the shortcut menu to rename the report as Sub_OrderDetails.
15. Click File > Save to save the report set as OrdersReport.cls.

### Task 2: Insert a subreport to the report

After creating the report, we can insert the subreport to the primary report by specifying relationships between them. In this lesson, we can specify different relationships between the primary report (the first one) and the two chart components in the second report by setting up different links between them. Then JReport will build a subreport for every order with the conditions, so that it is convenient to see the corresponding customer and employee information of every order.

We want this subreport to be at the end of the existing reports, thus we need to add a second group header and add the subreport to the new header.

1. Click OrderDetailInformation on the report tab bar to switch to the report.

2. Right-click in the GroupHeader panel of the banded object and choose Insert Panel After from the shortcut menu.
3. Select the newly-added panel and click Insert > Subreport.
4. When a box attached to the mouse cursor, click the newly-added panel to place the subreport into it, and the Subreport dialog appears.
5. Click the Browse button, select the OrdersReport.cls and then click Open.
6. Select Sub_OrderDetails from the Subreport drop-down list.
7. Click the Add button beside the Component in Report box in the Field tab, choose ChartObject in the Choose Component dialog, then click OK.

8. Build the relationship between the primary report and the selected chart object on the Orders_Customer ID field.

9. Click the Add button again, this time, choose ChartObject1 in the Choose Component dialog.
10. Build the relationship between the primary report and the second chart object on the Orders_Employee ID field.
11. Click OK in the Subreport dialog and the report will be inserted in the OrderDetails report as follows:

Next, we will filter the dataset used by reports in the report set so as to enhance the performance when running the report set.

1. Click Report > Dataset Management on the menu bar to display the Dataset Management dialog.

   The Dataset Management dialog lists all the dataset used in the currentreport set. In our report set, all the components share the same dataset, so here only one dataset is shown.

2. Click the Filter tab, click the Add Condition button to add a filter line and set the filter condition as Orders_Order ID <= 3100, then click OK.

3. Click File > Save to save the report set.
4. Click the View tab, the report that contains a subreport is displayed as follows: the primary report shows the orders detail information grouped by order ID, and the subreport shows the orders information by different customers and different employees respectively in the two charts.

   Note: If the report does not look correct, you can compare it to the final versions of the tutorial reports provided by JReport. To do so, you will need to save and close this catalog and open the TutorialReports.cat catalog file instead, which is located at `C:\JReport\Designer\Demo\Reports\TutorialReports`.

### Lesson 8 summary

In this lesson, we created a report that contains a subreport. First we created a report set with two reports and the second report with two charts of different types. Then we inserted the second report as a subreport to the first report by setting up different links between them, so that we can see clearly how many orders the customer and the employee have respectively made.

As you learned in this lesson, it is very convenient to build up a library of reports that act as components that you can add to various primary reports. Properly designed and used, subreports save a lot of time and effort in rapidly adapting your applications to changing user requirements and needs.