Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

3.02 Code for Adding a Report

To start coding, declare and instantiate an object variable from the ReportDocument class. The ReportDocument class has all the properties and methods necessary for loading and printing a report.

Although I didn’t mention it before, if you use the viewer control to preview a report, the ReportDocument class is still used to load and print the report. The viewer control does all the work for you behind the scenes and you don’t have to be aware of it.

Dim MyReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
MyReport.Load("C:\Report1.rpt")
MyReport.PrintToPrinter(1, False, 0, 0)

The first line of code declares an object variable MyReport and instantiates a new instance of the ReportDocument class. As I said earlier, this ReportDocument class is responsible for managing all report related functions.

The second line of code calls the Load() method to load an Untyped report into memory. The last line of code calls the PrintToPrinter() method to send the report to the printer.

Once you get into advanced programming techniques, you’ll learn that there are times when you will use the viewer control and still create a ReportDocument object variable. This is when you want to perform runtime customization. See Part II of the book for more information.