Report Designer Component

book list add book to my bookshelf create a bookmark purchase this book online

mastering crystal reports 9
Chapter 19 - Building Windows Applications with the Report Designer Component
Mastering Crystal Reports 9
by Cate McCoy and Gord Maric
Sybex 2003

The RDC is not one component but a set of components. Figure 19.3 shows the components and their relationships.

click to expand
Figure 19.3. The RDC components

Report Designer The Report Designer allows the VB developer to create Crystal Reports from a blank report or modify existing Crystal Reports inside VB. You do not need to develop your reports with Crystal Reports and then import them into VB as you had to do previously. Using the Report Designer is like having Crystal Reports inside VB.

Embeddable Crystal Report Designer Control The Crystal Report Designer control allows the end user of your applications to develop or modify a Crystal report from within your application. With your application you can give users the power of Crystal Reports without installing Crystal Reports on their system. However, the purchase of additional licensing is required.

Automation Server The Automation Server is the central component in the RDC. This is a COM-based object hierarchy that allows you to control all the functionality of Crystal Reports through code, by manipulating methods and properties of the objects that represent the components of a report. Automation Server is a fancy marketing term; this is a set of objects (a rather large set) with methods and properties that allow you to control Crystal Reports. Two separate Automation Servers are provided with the RDC:

Crystal Reports ActiveX Designer Runtime Library (craxdrt.dll) This library is known as the RDC runtime engine. To access this library in VB, select Project > References and choose Crystal Reports 9 ActiveX Designer Runtime Library. Then you can dimension a variable to access the objects in the object library, shown in Figure 19.4.

click to expand
Figure 19.4. Crystal Reports ActiveX Designer Runtime Library

Crystal Reports ActiveX Designer Design and Runtime Library (craxddrt.dll) This library is known as the RDC design and runtime engine.

To access this library in VB, select Project > References and choose Crystal Reports 9 ActiveX Designer Design And Runtime Library. Figure 19.5 shows the object library.

click to expand
Figure 19.5. Crystal Reports ActiveX Designer Design and Runtime Library

Warning 

If you have the Developer Edition and try to use some of the report-creation features of the ActiveX Designer Design and Runtime Library, you will receive a “Creation feature not enabled error” or the application will not run.

Both RDC Automation Servers provide the same manipulation of Crystal Reports. However, the Crystal Reports ActiveX Designer Design Runtime Library provides the additional functionality to create Crystal Reports.

The craxdrt.dll can be used in either a VB application or an ASP application. The craxddrt.dll (design) should be used only in a Windows VB application because it is not multithreaded for a web environment.

Crystal Report Viewer Crystal Report Viewer control is similar to the preview window in Crystal Reports. The Report Viewer displays Crystal Reports. Modifications you make to the report in the RDC, via the Report Designer or Automation Server, are displayed in the Report Viewer. There are two viewers available:

ActiveX Viewer The ActiveX Viewer can be added to any application that supports the ActiveX standard, such as Visual Basic, Delphi, or C++. In VB, the Crystal Report Viewer is added to the VB toolbox and placed on the form. To add the viewer to the VB toolbox, select Project > Components and choose Crystal Report Viewer Control 9.

Report Bean Viewer The Report Bean Viewer can be added to any development environment that supports Java version 1.1. The viewer gets Crystal Reports from a web server. You set the reportName property to an address for the web server that contains the Crystal report.

Crystal Reports Select Expert The Select Expert is the Select Expert and Search Expert from Crystal Reports. Using this control you can provide in your application the same Search and Select Expert as in Crystal Reports.

Each of these RDC components has a specific task, such as displaying a report, allowing you to view a report, or building a report either at runtime or design time. You can use the RDC components in any combination. Table 19.1 examines some possibilities.

Table 19.1: Examples of Using the RDC Components

Task

RDC component

Design a report at design time and modify the report at runtime via code.

Use the Report Designer with the Automation Server.

Design a report at design time, modify the report at runtime via code, and display the report to the users.

Use the Report Designer, Automation Server, and Crystal Report Viewer.

Load an existing report or create a report using code, and view the report.

Use the Automation Server and the Crystal Report Viewer.

Allow your users to develop a report in your application, modify the report at runtime via code, and view the report in the application.

Use the Automation Server, Embeddable Designer, and the Crystal Report Viewer.

Create a report via code; print or export the report.

Use the Automation Server.

Tip 

Full Developer documentation, license documentation, and runtime deployment documentation can be found at C:\Program Files\Crystal Decisions\Crystal Reports 9\Developer Files\Help\en, assuming you installed Crystal Reports in the default directory. The installation CD also contains a \Docs directory with documentation in Adobe PDF format.

Adding a Report with the Crystal Report Expert

The Crystal Report Expert is the easiest way to start working with Crystal Reports and Visual Basic. The Report Expert hides some of the complexity of the various RDC components by automatically making project references and adding components to the VB toolbox. Later on in this chapter we will show you how to use Crystal Reports without the expert so you can have the greatest flexibility and understand what the expert does for you. For now, let’s examine how to configure VB and take a tour of what the Report Expert does for you.

Start Visual Basic and select a Standard EXE project. Select Project > Add Crystal Report 9. Visual Basic will display the Crystal Report Gallery dialog box, as shown in Figure 19.6. Does this look familiar to you? This is the same interface as in Crystal Reports! The Report Expert will guide you through creating a report, creating a blank report, or opening an existing report.

click to expand
Figure 19.6. Crystal Report Gallery with VB

If you choose Using The Report Expert or As A Blank Report, you will be prompted to choose the data source for the report, as shown in Figure 19.7. You can choose the normal data sources for a Crystal Report; Visual Basic also adds Project Data as a new data source. Project Data is Microsoft’s Data Environment that was added to Visual Basic 6. The Data Environment is a graphical interface to ADO (ActiveX Data Objects). This allows Crystal Reports to report from an ADO record set in your application.

click to expand
Figure 19.7. Project data using the Report Expert

To report from Project Data, add a Data Environment to your VB project by choosing Project > Add Data Environment (if you don’t have VB 6 SP 5, then the menu will be Project > More ActiveX Designers > Data Environment) before adding a Crystal report. Configure the Data Environment to connect to an ADO data source by right-clicking Connection1 and choosing Properties. Choose the OLEDB provider for your data source, click the Next button, and then select the database server and database to report from. This creates a connection to the data source. Once that is done, right-click the connection and choose Add Command. The command represents the command you want to send to the database and the data that will be returned to the data environment. The command can be a SQL statement, the name of a database table, or a stored procedure. Right-click the command, and choose Properties to configure the command for the database. (Refer to VB documentation for more details on the data environment.)

Once you’ve configured a Data Environment, you can add a Crystal Report Designer and choose the Project Data as a source for your report.

Note 

Crystal Reports can also report from an ADO record set without using the Data Environment.

After you have chosen the Crystal report, the Crystal Expert will prompt you to add a Crystal Report Viewer to a VB form and change the VB project properties so that the newly added form is the form that starts when the application runs, as shown in Figure 19.8. If you choose not to add the Crystal Report Viewer at this time, you can add it yourself later. Or, if you select not to change the startup object, you can modify this yourself later in VB.


Figure 19.8. Crystal Report Expert

When you click OK in the Crystal Report Expert, you will be presented with the Crystal Report Designer that contains the Crystal report, and CrystalReport1 will be added to the Project Explorer in VB under the Designers folder. Refer to Figure 19.9 to see the additions and changes to the VB development environment.

click to expand
Figure 19.9. Visual Basic design environment

The Report Designer provides Crystal Report functionality. Reports can be modified or designed from scratch in the designer. To access the Crystal menus, right-click the white area to the left of the report in the Report Designer; the menus should be familiar Crystal menus. The toolbar is also similar to the one in Crystal Reports. Reports created in the Report Designer are saved as DSR files. DSR files can be opened only by Visual Basic. You can also save reports created in the Report Designer by clicking the Save To Crystal Report icon on the toolbar (seventh from the left).

The Crystal Report Expert will also add the Crystal Report Viewer to a VB form, Form2 in our case. The Report Viewer is used to display the report from the designer.

Note 

You can add multiple Crystal Reports to your VB project. Each one will be contained in its own designer.

RDC Object Model

What you don’t see in Figure 19.9 is the Automation Server. The Crystal Report Expert automatically made a reference in VB (Project > References) to both Crystal Reports 9 ActiveX Designer Design and Runtime Library and Crystal Reports 9 ActiveX Design Runtime Library. It is not necessary to make a reference to both libraries for a VB project; however, the Crystal Report Expert adds both because it does not know which library you will need. If you are not using the design capabilities of the automation server (refer back our discussion on the Automation Server), you may want to remove the reference to Crystal Reports 9 ActiveX Designer Design and Runtime Library from VB to avoid confusion. Both automation servers have similar methods and properties, and they are only distinguished by one letter—craxddrt versus craxdrt—so it is easy to make a mistake and add bugs to your project.

The Automation Server has no visual interface; you manipulate it through code. The Automation Server is a set of objects that represent the functionality of Crystal Reports. You need to be familiar with working with objects in order to work with the Automation Server; refer to Chapter 18 if necessary. Figure 19.10 contains a diagram of the Automation Server objects and their relationships, and Figure 19.11 shows the methods for the Application object.

click to expand
Figure 19.10. RDC Object model hierarchy diagram

click to expand
Figure 19.11. Methods for the Application object

Tip 

For a complete list of all the objects, methods, properties, and events of the RDC, refer to CrystalDevHelp.chm in C:\Program Files\Crystal Decisions\Crystal Reports 9\Developer Files\Help\en.

The object model looks very complicated, especially when you look at it for the first time, because it represents all the functionality of Crystal Reports. Imagine if we showed you all the menus of Crystal Reports on one page; it would look very complicated too. The main objects of the RDC are as follows:

Application Object The Application object is the main object in the hierarchy. Think of the Application object as the Crystal Reports application you use to build reports. This object has methods such as OpenReport or NewReport, which allow you to open and create new reports. The Crystal Expert automatically creates this object when you add the Report Designer to your VB project. You can also create this object manually if you choose not to use the expert. We will discuss this in more detail later in the chapter.

Report Object The Report object represents a Crystal report and all the objects that can be included in the report. Notice in Figure 19.10 how the Report object contains collections of sections and other Report objects. Each collection contains the actual object that we see in Crystal Reports, such as a Graph object or Field object. The Report object contains methods such as SaveAs to save the Crystal report or properties such as RecordSelectionFormula to specify the records you would like to see in the report. The Crystal Report Designer is the Report object with a visual interface. You can also access the Report object without the Crystal Report Designer. We will discuss this in more detail later in the chapter.

Database Object The Database object represents the data sources that are used in the report. It contains a collection of tables and fields within the tables. The Database object contains information about the data sources in the report as well as the connection information to the database, such as the name of the server and the user ID and password required to access the server. Using the Database object you can change data sources and modify user IDs and passwords. We will examine this in more detail later in the chapter.

Sections and Report Objects Notice back in Figure 19.9 how the report is broken down into sections, and each section contains objects (just like Crystal Reports). Each section has a name: Report Header is called ReportHeaderSection1, Page Header is called PageHeaderSection1, etc. Each of the Report objects in Crystal also has a name. Notice in the Properties window that the PostalCode object is named PostalCode1. Examine the Properties window further and you will see the properties for PostalCode1; for example, it has 2 decimal places, a height of 221 twips, and is indented left 7200 twips. Through the Report Designer you can access all the objects in a Crystal Report. You can also access all the methods and properties of the Report object via code and manipulate the report at runtime instead of at design time.

Note 

1,440 twips equal one inch, and 567 twips equal one centimeter.

Visual Basic Code

The last item in our tour is the code that was generated by the Crystal Expert. Listing 19.1 contains all the code generated by the expert.

Listing 19.1: Crystal Expert-Generated Code

start example
Dim Report As New CrystalReport1 Option Explicit Private Sub Form_Load()    Screen.MousePointer = vbHourglass    CRViewer91.ReportSource = Report    CRViewer91.ViewReport    Screen.MousePointer = vbDefault End Sub Private Sub Form_Resize()    CRViewer91.Top = 0    CRViewer91.Left = 0    CRViewer91.Height = ScaleHeight    CRViewer91.Width = ScaleWidth   End Sub
end example

In the first line, a variable called Report is created that represents a Crystal Report object. When you added the Crystal Report Designer to VB, VB automatically named it CrystalReports1. Remember that the Crystal Report Designer is the Report object in the Automation Server with a visual interface. In this line of code we create a new instance of a Report object and copy CrystalReport1 into that object. We can then access the methods and properties of the Report object, through the Report object variable.

CRViewer91.ReportSource = Report

CRViewer91 is name of Crystal Report Viewer that was added to the form; the ReportSource property of the viewer accepts a Report object. Once a Report object is assigned to the report source, the viewer can display the report.

CRViewer91.ViewReport

The ViewReport method tells the Crystal Viewer to display the report in the viewer. The rest of the code that is generated by the expert changes the mouse pointer from an arrow to an hourglass to indicate to the user that something is happening.

The expert adds code in the Form_Resize event to resize the Crystal Viewer to the dimensions of the form that contains it. The Crystal Viewer supports the standard Top, Left, Height, and Width properties of all objects in VB.

We find that the sample code generated by the Crystal Report Expert can lead to confusion when working with Crystal Reports in VB. For example, the line

Dim Report As New CrystalReport1 

causes VB to create two copies of the same report because of the New keyword in this line. You can then access both of them through the variable CrystalReport1 or Report and they both will have methods and properties of the Automation Server Report object. If you always refer to the Crystal report using the Report variable, then you will not run into bugs. However, memory is wasted because of two instances of the same report. We find that it is too easy to refer to CrystalReport1 in one part of your code and Report in another part of your code, thinking that you are modifying properties of the same report, and later you spend hours trying to figure out why things are not working.

You can achieve the same results if you don’t declare the Report variable and just use CrystalReport1, the name of the Report Designer. Here the default code is rewritten to use CrystalReport1 instead of Report. We deleted the first Dim Report line and assigned CrystalReport1 to the ReportSource; see the bolded line of code:

Option Explicit Private Sub Form_Load()    Screen.MousePointer = vbHourglass    CRViewer91.ReportSource = CrystalReport1    CRViewer91.ViewReport    Screen.MousePointer = vbDefault  End Sub

If you prefer to use the Report variable you still can do so, but don’t use the New keyword to create a new instance of a report. Here is the sample code rewritten so that Report is used but two copies of the report are not created. Again the lines that are changed are bolded:

 Dim Report As CrystalReport1 Option Explicit Private Sub Form_Load()     Set Report = CrystalReport1     Screen.MousePointer = vbHourglass     CRViewer91.ReportSource = Report     CRViewer91.ViewReport     Screen.MousePointer = vbDefault End Sub

Now that you have the basics under your belt, we can start working with the VB and RDC to build Windows applications.

Use of content on this site is expressly subject to the restrictions set forth in the Membership Agreement
 
Conello © 2000-2003     Feedback


Mastering Crystal Reports 9
Mastering Crystal Reports 9
ISBN: 0782141730
EAN: 2147483647
Year: 2005
Pages: 217

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