Desktop Applications

Desktop applications, while still popular today, were what started it all. These are standalone applications that run entirely on a single tier and are installed locally on a user's machine. These applications are most commonly built using Visual Basic, but are also sometimes built using Visual C++ or Delphi.

graphics/bookpencil_icon.gif

All sample code in this section will use Visual Basic syntax, but can easily be adapted to other languages that support COM. For sample code in other languages, visit Crystal Decisions' support site at http://support.crystaldecisions.com.


All these development environments, and many more, support Microsoft's COM technology. COM (Component Object Model) is a standard technology used for exposing SDKs in the Windows world. Much of Microsoft's own SDKs are based on COM. It follows that the recommended Crystal Reports SDK for desktop applications would also be based on COM. Its name is the Report Designer Component, and it consists of the following components:

  • A report designer integrated in to the Visual Basic environment

  • An object model built around the report engine used for manipulation of the report inside an application

  • A report viewer control used for displaying reports inside an application

The following sections describe each of these components in more detail.

The Visual Basic Report Designer

The Visual Basic Report Designer enables developers to create and edit reports from within the comfort of the Visual Basic environment. Figure 24.1 shows the report designer active inside Visual Basic.

Figure 24.1. The Visual Basic Report Designer.

graphics/24fig01.jpg

To add a new report to a project, select Add Crystal Reports 9 from the Project menu inside Visual Basic.

The Visual Basic Report Designer supports most of the features of the Crystal Reports designer and can be used to create everything from simple columnar reports to highly formatted, visually appealing reports. The menus that you would normally find in the standalone Crystal Reports designer can be found by right-clicking on an empty spot on the designer surface. The same dialogs and commands are available.

This report designer works only inside Visual Basic, but the viewer and object model work in any COM-compliant development environment. When using a non-Visual Basic environment, or just by personal preference, the regular Crystal Reports designer can be used to create the reports to be used inside the application.

Report Engine Object Model

The Object Model is the main entry point to the Crystal Reports engine for desktop applications. As mentioned earlier, it is based on COM and can be used from any COM-compliant development environment. In Visual Basic, this component shows up in the References dialog as Crystal Reports ActiveX Designer Runtime Library, as shown in Figure 24.2. (The filename is CRAXDRT9.dll.)

Figure 24.2. Referencing the Report Designer Component's Object Model.

graphics/24fig02.jpg

In addition to many other features, the Object Model provides the ability to open, create, modify, save, print, and export reports. Some of the most common uses of the report engine is to load a report file, provide database logon credentials, provide parameter field values, and then print or export the report. In the case of things like parameters and exporting, a default user interface will pop up to perform the associated action, but often developers want to present their own user interface or perhaps hide the user interface entirely. Listing 24.1 illustrates how to pass parameter field values and database logon credential.

Listing 24.1 Parameter and Logon via the Object Model
 Dim Application As New CRAXDRT.Application Dim Report As CRAXDRT.Report ' Open the report from a file Set Report = Application.OpenReport("C:\MyReport.rpt") ' Provide database logon credentials (in this case ' for an OLEDB connection to a SQL Server database) Dim tbl as CRAXDRT.DatabaseTable Set tbl = Report.Database.Tables(1) tbl.ConnectionInfo("Data Source") = "MyServer" tbl.ConnectionInfo("Initial Catalog") = "MyDB" tbl.ConnectionInfo("User ID") = "User1" tbl.ConnectionInfo("Password") = "abc" ' Provide parameter field values Report.ParameterFields(1).AddCurrentValue("USA") Report.ParameterFields(2).AddCurrentValue(10000) ' Export the report to a PDF file Report.ExportOptions.FormatType = crEFTPortableDocFormat Report.ExportOptions.DestinationType = crEDTDiskFile Report.ExportOptions.DiskFileName = "C:\MyReport.pdf" Report.Export False 

There are two ways to load reports: via a standalone RPT file or via an embedded designer class. In the first situation, you simply call the Application.OpenReport method, passing in a file path to an RPT file. The preceding code example uses this method. For the second situation, if you've got the report inside your Visual Basic project (either by creating a new report with the Visual Basic Report Designer, or importing an existing report into the project), a corresponding class will represent that report. For example, if you added a new report to your application, it will have a default name of CrystalReport1 and will have a DSR extension. Let's say that you renamed it as MySalesReport.dsr. You would then be able to declare an object like this:

 Dim Report As New MySalesReport  

The MySalesReport object is the same as a CRAXDRT.Report object, but it is stored inside the resources of the application and doesn't require an external RPT file.

Report Viewer

In the previous section, only printing and exporting was mentioned as options for delivering reports. You might have been wondering how to view reports onscreen. This is where the Report Viewer comes in. This report viewer control is usually referred to as the ActiveX viewer, or the Crystal Reports Viewer Control. It is an ActiveX control, which means that in addition to being able to be dropped on to any Visual Basic form like the other components of the Report Designer Component it can be used in any COM-compliant development environment. It's filename is CRViewer9.dll. Figure 24.3 depicts the ActiveX viewer displaying a report from a Visual Basic application.

Figure 24.3. The ActiveX viewer in action.

graphics/24fig03.jpg

The ActiveX viewer works in conjunction with the Object Model to render the report to the screen. The Object Model talks to the report engine to process the report, and then the ActiveX viewer asks the Object Model for each individual page, which it then displays onscreen. The following code snippet illustrates how to view a report with the Report Viewer control.

 Dim Application As New CRAXDRT.Application  Dim Report As CRAXDRT.Report Set Report = Application.OpenReport("C:\MyReport.rpt") CRViewer.ReportSource = Report CRViewer.ViewReport 

The ActiveX control has many properties and methods that enable the developer to customize its look and feel, such as hiding the toolbar or group tree. In addition, the control has a full event model that allows the developer to be notified when certain actions are performed, such as a drill-down or a page navigation. For more information on the Report Designer Component, consult the Crystal Reports 9 developer help file installed with the product.

These three components, the Visual Basic Report Designer, the Report Engine Object Model, and the ActiveX Report Viewer control (collectively called the Report Designer Component), are the recommended solution for integrating reporting into desktop applications.



Sams Teach Yourself Crystal Reports 9 in 24 Hours
Sams Teach Yourself Crystal Reports 9 in 24 Hours
ISBN: B003D7JUVW
EAN: N/A
Year: 2005
Pages: 230

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net