When setting up embedded subreports in ActiveReports, the principles are the same as when setting up simple subreports but are applied to the child-grandchild reports. This walkthrough illustrates how to set up embedded subreports.
This walkthrough is split up into the following activities:
- Creating parent, child, and grandparent reports
- Connecting each report to a data source
- Adding controls to each report to display the data
- Adding code to display reports in the subreport controls
To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\GrapeCity\ActiveReports 6\Data\NWIND.MDB (on a 64-bit Windows operating system, a copy is located in C:\Program Files (x86)\GrapeCity\ActiveReports 6\Data\NWIND.MDB).
When you have finished this walkthrough, you will have a report that looks similar to the following.
To create parent, child, and grandchild reports
- Open a new project in Visual Studio.
- From the Project menu, select Add New Item.
- Select ActiveReports 6 (code-based) File and rename the file rptEmployees.
- Click Open.
- Repeat for rptCustomers and rptOrders.
To connect the parent report, rptEmployees, to a data source
- Click the gray report DataSource icon on the Detail section band to open the Report Data Source dialog.
- On the "OLE DB" tab, next to Connection String, click the Build button.
- In the Data Link Properties window that appears, select Microsoft Jet 4.0 OLE DB Provider and click the Next button.
- Click the ellipsis (...) button to browse to the Northwind database. Click Open once you have selected the appropriate access path.
- Click OK to close the window and fill in the Connection String field.
- In the Query field, enter the following SQL query
SQL Query |
Copy Code |
SELECT Employees.EmployeeID, Employees.LastName, Employees.FirstName, Employees.Extension, Customers.CustomerID
FROM Customers, Employees
ORDER BY Employees.EmployeeID, Customers.CustomerID |
- Click OK to save the data source and return to the report design surface.
|
Note: This query joins the Employees table for the parent report to the Customers table for the subreport. |
To connect the child report, rptCustomers, to a data source
- Click the gray report DataSource icon on the Detail section band to open the Report Data Source dialog.
- On the "OLE DB" tab, next to Connection String, click the Build button.
- In the Data Link Properties window that appears, select Microsoft Jet 4.0 OLE DB Provider and click the Next button.
- Click the ellipsis (...) button to browse to the Northwind database. Click Open once you have selected the appropriate access path.
- Click OK to close the window and fill in the Connection String field.
- In the Query field, enter the following SQL query
SQL Query |
Copy Code |
SELECT Customers.*, Employees.EmployeeID, Orders.OrderID FROM Employees INNER JOIN (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) ON Employees.EmployeeID = Orders.EmployeeID WHERE Customers.CustomerID = '<%CustomerID%>' |
- Click OK to save the data source and return to the report design surface.
|
Note: This SQL query uses parameters syntax: '<%CustomerID%>'. For more information on parameters, see the Parameters topic. |
To connect the grandchild report, rptOrders, to a data source
- Click the gray report DataSource icon on the Detail section band to open the Report Data Source dialog.
- On the "OLE DB" tab, next to Connection String, click the Build button.
- In the Data Link Properties window that appears, select Microsoft Jet 4.0 OLE DB Provider and click the Next button.
- Click the ellipsis (...) button to browse to the Northwind database. Click Open once you have selected the appropriate access path.
- Click OK to close the window and fill in the Connection String field.
- In the Query field, enter the following SQL query
SQL Query |
Copy Code |
SELECT * FROM Orders WHERE EmployeeID = <%EmployeeID%> |
- Click OK to save the data source and return to the report design surface.
To add controls to the parent report, rptEmployees, to display data
- Right-click on the design surface of rptEmployees and select Insert, then Group Header/Footer to add a group header and a group footer section to the report.
- With the group header selected in the Properties Window, make the following changes:
- Name: ghEmployees
- DataField: EmployeeID
- Height: 0.6
- Set the Height property of the page header section to 0.3.
- Set the CanShrink property of the Detail section to True to eliminate white space.
- In the Report Explorer, expand the Fields node, then the Bound node. Drag the following fields onto the group header section and set the properties of each textbox as indicated.
Group header fields
DataField |
Name |
Location |
Size |
EmployeeID |
txtEmployeeID1 |
0, 0.3 in |
1, 0.2 in |
LastName |
txtLastName1 |
1.05, 0.3 in |
1.35, 0.2 in |
FirstName |
txtFirstName1 |
2.5, 0.3 in |
1.3, 0.2 in |
Extension |
txtExtension1 |
3.85, 0.3 in |
1, 0.2 in |
- Drag the following controls from the ActiveReports Toolbox onto the indicated section of rptEmployees, setting the properties as indicated.
Other controls
Control |
Section |
Name |
Text |
Miscellaneous |
Location |
Size
|
Label |
PageHeader |
label1 |
Customer Orders by Employee |
Font size = 14 Alignment = Center |
0, 0 in |
6.5, 0.25 in |
Label |
GroupHeader |
label2 |
Employee ID |
Bold |
0, 0 in |
1, 0.198 in |
Label |
GroupHeader |
label3 |
Last Name |
Bold |
1.05, 0 in |
1, 0.198 in |
Label |
GroupHeader |
label4 |
First Name |
Bold |
2.5, 0 in |
1, 0.198 in |
Label |
GroupHeader |
label5 |
Extension |
Bold |
3.85, 0 in |
1, 0.198 in |
Subreport |
Detail |
subReport1 |
|
|
1.05, 0 in |
5.4, 1 in |
To add controls to the child report, rptCustomers, to display data
Since the subreport control in the parent report that displays this report is 5.4 inches wide, none of the controls on this report extend beyond 5.4 inches. If you need a visual reminder, you can resize the child report by dragging the right edge inward or by changing the PrintWidth property.
- Click in the gray area below rptCustomers to select the report.
- In the Properties Window, change the ShowParametersUI property to False. This prevents the subreport from requesting parameter values from the user.
- Select the Detail section and set the CanShrink property to True to eliminate white space.
- Set the BackColor property of the Detail section to AliceBlue to distinguish the subreport from the main report.
|
Tip: Even if you do not want colors in your finished reports, using background colors on subreports can help in troubleshooting layout issues. |
- Right-click the PageHeader or PageFooter section and select Delete. Subreports do not render these sections, so deleting them saves processing time.
- Drag the following controls from the ActiveReports toolbox onto the Detail section of rptCustomers, setting the properties as indicated.
Detail section controls
Control |
Name |
DataField |
Text |
Miscellaneous |
Location |
Size |
Label |
label1 |
|
Company Name |
Bold |
0, 0 in |
1.2, 0.198 in |
Label |
label2 |
|
Contact Name |
Bold |
2, 0 in |
1, 0.198 in |
Label |
label3 |
|
Phone |
Bold |
4.2, 0 in |
1, 0.198 in |
TextBox |
textBox1 |
CompanyName |
|
|
0, 0.29 in |
1.9, 0.198 in |
TextBox |
textBox2 |
ContactName |
|
|
2, 0.29 in |
2.125, 0.198 in |
TextBox |
textBox3 |
Phone |
|
|
4.2, 0.29 in |
1, 0.198 in |
Label |
label4 |
|
Order ID |
Bold |
2, 0.604 in |
1, 0.198 in |
Label |
label5 |
|
Order Date |
Bold Alignment = Right |
3.3, 0.604 in |
1, 0.198 in |
SubReport |
subReport1 |
|
|
|
2, 0.802 in |
3, 1 in |
To add controls to the grandchild report, rptOrders, to display data
Since the subreport control in the child report that displays this report is 3 inches wide, none of the controls on this report extend beyond 3 inches. If you need a visual reminder, you can resize the child report by dragging the right edge inward or by changing the PrintWidth property.
- Click in the gray area below rptOrders to select the report.
- In the Properties Window, change the ShowParametersUI property to False.
- Select the Detail section and set the CanShrink property to True to eliminate white space.
- Set the BackColor property of the Detail section to Lavender to distinguish the subreport from the main report.
- Right-click the PageHeader or PageFooter section and select Delete.
- Add the following controls to the Detail section of rptOrders, setting the properties as indicated.
Detail section controls
Control |
Name |
DataField |
Miscellaneous |
Location |
Size |
TextBox |
txtOrderID |
OrderID |
|
0, 0 in |
1, 0.198 in |
TextBox |
txtOrderDate |
OrderDate |
OutputFormat = M/d/yy Alignment = Right |
1.3, 0 in |
1, 0.198 in |
To add code to display rptCustomers in the subreport control on rptEmployees
|
Warning: Do not create a new instance of the subreport in the Format event. Doing so creates a new subreport each time the section Format code is run, which uses a lot of memory. |
To write the code in Visual Basic
- At the top left of the code view for the report, click the drop-down arrow and select (rptEmployees Events).
- At the top right of the code window, click the drop-down arrow and select ReportStart. This creates an event-handling method for the report's ReportStart event.
- Add code to the handler to create a new instance of the subreport.
The following example shows what the code for the method looks like.
Visual Basic.NET code. Paste INSIDE the ReportStart event. |
Copy Code |
Me.subReport1.Report = New rptCustomers() |
To write the code in C#
- Click in the gray area below rptEmployees to select it.
- Click the events icon in the Properties Window to display available events for the report.
- Double-click ReportStart. This creates an event-handling method for the report's ReportStart event.
- Add code to the handler to create a new instance of the subreport.
The following example shows what the code for the method looks like.
C# code. Paste INSIDE the ReportStart event. |
Copy Code |
this.subReport1.Report = new rptCustomers(); |
To add code to display rptOrders in the subreport control on rptCustomers
To write the code in Visual Basic
- At the top left of the code view for the report, click the drop-down arrow and select (rptCustomers Events).
- At the top right of the code window, click the drop-down arrow and select ReportStart. This creates an event-handling method for the report's ReportStart event.
- Add code to the handler to create a new instance of the subreport.
The following example shows what the code for the method looks like.
Visual Basic.NET code. Paste INSIDE the ReportStart event. |
Copy Code |
Me.subReport1.Report = New rptOrders() |
To write the code in C#
- Click in the gray area below rptCustomers to select it.
- Click the events icon in the Properties Window to display available events for the report.
- Double-click ReportStart. This creates an event-handling method for the report's ReportStart event.
- Add code to the handler to create a new instance of the subreport.
The following example shows what the code for the method looks like.
C# code. Paste INSIDE the ReportStart event. |
Copy Code |
this.subReport1.Report = new rptOrders(); |