### 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 page report that contains two report tabs - 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 tab in the page report as the primary report, and the second one as the subreport. So that we can insert the second report tab to the first one to show the sales total by orders in different order ID groups.

- Task 1: Create a page report that contains two report tabs
- Task 2: Insert a subreport to the report

#### Task 1: Create a page report that contains two report tabs

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 > Page Report on the menu bar.
2. In the New Page Report dialog, clear the text in the Report Title text box, select Banded from the layout box, then click OK.

   Be sure that JinfonetGourmetJava.cat is specified as the current catalog because this is the catalog we use in this track. For information about specifying this catalog, see Task 1, Step 2 and 3 of Lesson 1.

3. In the Data screen of the Banded Wizard, select OrdersReport from the Imported SQLs node of Data Source 1.

4. Switch to the Style screen and select the Simple style for the report, 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 Wizard from the shortcut menu.
2. In the Group screen of the Banded Wizard, add the DBField Orders_OrderID as the group by field by selecting it and clicking button, 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. Double-click and 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 and resize them in the GroupHeader panel as follows:

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

7. Select the Order Date DBField and set its Format property to M/d/yyyy in the Report Inspector.

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 tab in the report 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 report tab and select Rename from the shortcut menu, then enter OrderDetailInformation in the Input Report Tab Name dialog and click OK.

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

1. Click File > New > Page Report Tab.
2. In the New Page Report Tab dialog, clear the text in the Report Title text box, select Tabular from the layout box, then click OK.
3. In the Tabular Wizard, keep the default settings and click Finish. A report tab with a blank 2*2 tabular is created.
4. Merge the tabular cells into two rows by selecting two cells in a row when holding the Ctrl key on the keyboard, right-clicking on them and selecting Merge from the shortcut menu, then adjust them in order to place the bar chart and pie chart vertically in the report tab.

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

1. Select the first tabular cell and click Insert > Chart on the menu bar.
2. In the Data screen of the Create Chart wizard, check the Existing radio button, select OrdersReport from the DataSets node, 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 by selecting it and clicking the button , Orders_OrderID will then be added automatically to the Category box.
5. Switch to the Style screen and select Basic from the Style list. Click Finish to create the chart.
6. When a box attached to your mouse cursor, click the tabular cell to place the chart there.

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

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

9. 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.
10. In the General tab, change the label text to Current Orders for This Customer.
11. Switch to the Font tab, change Font to Arial, set Font Size to 12pt, Font Color to 808080 and click OK.
12. Add a label to the pie chart and format it using the same way and edit its text to Current Orders for This Employee.
13. Select ChartObject and ChartObject1 in the Report Inspector by holding the Ctrl key on the keyboard and change their Border Type properties to none; change the property Border Type to none for ChartLegendObject and ChartLegendObject1 as well.
14. 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 tab in the report is created to show the sales total by orders.

15. On the report tab bar, right-click the report tab containing the charts and select Rename from the shortcut menu, then enter Sub_OrderDetails in the Input Report Tab Name dialog and click OK.
16. Click File > Save to save the report 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 tab 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 tab.

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 OrdersReport.cls and then click Open.
6. Select Sub_OrderDetails from the Report Tab drop-down list.
7. Click the Add button beside the Component in Report Tab 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_CustomerID field by selecting the field and clicking .

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

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

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 current report. In our report, 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, select Orders_OrderID from the field drop-down list, <= from the operator drop-down list, and enter 3050 in the value text field to specify the filter condition as Orders_OrderID <= 3050, then click OK.

3. Click File > Save to save the report.
4. Click the View tab, the report tab 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 version of the report provided by JReport. To do so, you will need to save and close this catalog and then open the JinfonetGourmetJava.cat catalog file located at `<install_root>\Demo\Reports\TutorialReports`.

#### Lesson 8 summary

In this lesson, we created a report that contains a subreport. First we created a page report with two report tabs and the second report tab with two charts of different types. Then we inserted the second report tab as a subreport to the first report tab 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.