Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

17.25 Working with Subreports

Working with Subreports

When you open a report and set its data connectivity properties, you have to do the same with any subreports that are used. Subreports have the same data connectivity issues that standard reports have. You have do things such as log on to one or more data sources and change the record selection formula. Fortunately, the subreport object is based on the same class as the main report (the ReportDocument class). Modifying the properties of a subreport uses the same code that was written for the main report. The only difference being that you have to first drill down to the subreport object and create a reference to an instance of it. Drilling down to find the subreport object was shown in Chapter 15.

If the subreport uses the same database as the main report, you can skip setting the subreport’s logon properties. By logging on to the main report you are automatically logged on to the subreport. If the subreport uses a different database than the main report, you have to set the logon properties for both reports.

If a subreport uses the Push Model, you have to pass a populated dataset to the subreport. This is easily accomplished with the SetDataSource() method. Pass this method the dataset object. The following code demonstrates setting the data source for the main report and two subreports. Please note that I don’t show the code to populate the datasets here. Call the appropriate FillDataSet() methods for your data sources.

Listing 17-15. Set subreport credentials
Dim MyReport As New CrystalReport1
Dim ds0, ds1, ds2 As DataSet
FillDataSet(ds0)
FillDataSet(ds1)
FillDataSet(ds2)
MyReport.SetDataSource(ds0)
MyReport.OpenSubreport("subreport1").SetDataSource(ds1)
MyReport.OpenSubreport("subreport2").SetDataSource(ds2)
CrystalReportViewer1.ReportSource = MyReport