Create a Subreport in Crystal Reports

A subreport in Crystal Reports refers to a report embedded within another report. It serves the purpose of displaying supplementary information about the data presented in the main Crystal Report. By utilizing subreports, you can include additional details or data that provide further context or insights.

Subreports can be displayed within the main Crystal Report, appearing alongside the primary data. Alternatively, they can be set up as on-demand reports. In the latter case, a hyperlink is placed within a row, and when a user clicks on that hyperlink, the corresponding subreport is generated and displayed.

In this section, we will focus on demonstrating the integration of subreports within the Crystal Reports. Each row in the main report will have its associated subreport, enabling the inclusion of specific details or expanded information relevant to that particular row.

vb.net_crystal_report_subreport_0.GIF

Please note that all the Crystal Reports programming samples provided in these tutorials are based on the crystaldb database. Before proceeding with this tutorial section, I recommend reviewing the database structure to familiarize yourself with its organization. To access the database structure, kindly click on the following link: Click here to view the Database Structure.

In this section, we will create an order report based on three tables within the database. Each row in the report will include a subreport displaying specific details related to the corresponding Product Name. To achieve this, we will utilize the previously discussed program, "Crystal Report from Multiple Tables." It is advisable to refer to the tutorial on Crystal Report from Multiple Tables before proceeding with this section, as it provides detailed instructions on how to display the main Crystal Reports data using multiple tables.

To begin, create a Crystal Report that incorporates the three tables and selects the following fields: CustomerName, Date, Product, and Quantity. For a more comprehensive understanding of this step, please refer to the previous section on Crystal Report from multiple tables, which explains the process in detail.

After create the main report , right click on Crystal Reports designer window and select Insert-Subreport.

vb.net_crystal_report_subreport_1.GIF

To add the subreport to your Crystal Reports, follow these steps:

Obtain the subreport object and drag it onto the designer window, specifically at the bottom part of the details tab, just below the fields in the details section.

Release the mouse button, triggering a dialog box to appear, prompting you to enter a report name. Specify the desired report name and click on the "Report Wizard" button.

The Report Wizard will be displayed, presenting a table selection screen. Choose the appropriate table for your subreport. In this case, select the "Product" table from the provided list and click "Next".

The subsequent screen will display the selected table, allowing you to choose the fields you wish to include in your subreport's data. Once you have made your selections, click "Finish".

After completing the above steps, you will be directed back to the main screen of the subreport. Proceed to the "Link" tab.

In the "Link" tab, establish the relationship between your main report and the subreport. Specifically, you will link the "productname" from the main report to the subreport. To do this, select "Product.Product_name" from the available fields.

vb.net_crystal_report_subreport_2.GIF

Accept the other settings as it is in the screen and click ok. Now you can see the subreport object in the screen , if you want to modify subreport design , double click on subreport object and you can design subreport.

vb.net_crystal_report_subreport_3.GIF

Now the designer part is over . Next step is to select the default form(Form1.vb) and add a Button and Crystal Reports Viewer to the Form.

Put the following vb.net source code in your form and run the program .

Full Source VB.NET
Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim crp As New CrystalReport1 CrystalReportViewer1.ReportSource = crp CrystalReportViewer1.Refresh() End Sub End Class