Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
81
Добавлен:
11.05.2015
Размер:
2.11 Mб
Скачать

Component

Description

 

 

 

 

Data Set

One of the following:

 

Data Access Object (DAO) Recordset

 

Remote Data Object (RDO) Resultset

 

Active Data Object (ADO) Recordset

 

VB Data Environment

 

Crystal Data Object (CDO)

 

Crystal Data Source Type Library object

 

ODBC Direct

 

Crystal Data Source Type Library object

 

These objects do not need to be valid at design time, for example you

 

could construct a report template at design time without the data being

 

available. This is handled through a Data Definition Files, Page 164.

 

However, the data set objects must be present and valid at runtime to

 

generate a report.

 

 

VB Form

The Seagate Crystal Reports Smart Viewer Control must be embedded on

 

a Visual Basic Form in order to display the report on screen. The Create

 

Report Expert can automatically add a Form with the Smart Viewer

 

embedded to the project when you finish designing a report with the

 

Expert.

 

 

Dual - Interface

The object model for the Report Designer Component is exposed as a dual-interface. This means the component can be accessed and utilized easily in all kinds of development environments including Visual Basic, Visual InterDev, Visual C++, and Delphi. Although the component may not work as an ActiveX designer in all environments, the object model can be used as an automation server or ActiveX DLL in any development environment that supports ActiveX.

Report Designer Object Model Programming

The Seagate Crystal Report Designer Component provides a strong yet accessible object model for manipulating reports within your Visual Basic application. Through the object model, you can leverage the full power of Visual Basic to control the final look and content of your data.

Though easy to use, the object model requires knowing several basic techniques and strategies. This section provides examples of how to work directly with the object model to accomplish specific types of tasks. If you need information on report design using the Report Designer Component, refer to the Seagate Crystal Reports User’s Guide. Designing reports in the Report Designer Component is very similar to designing reports in the Report Designer for Seagate Crystal Reports.

The Report Designer Component

173

The tutorials in this section concentrate on the objects, properties, methods, and events of the object model exposed by the Report Designer Component. If you need to learn some general programming techniques that can be applied to all types of reports, review the topics listed below. The tutorials listed here do not assume that you are trying to create a specific type of report. Instead, they discuss the object model from a programming point of view, regardless of the report that you are trying to create.

The following topics are discussed in this section:

Report Designer Object Model Introduction, Page 175

Obtaining a Report object, Page 175

Displaying the report in the Smart Viewer, Page 175

Setting a new data source for the report, Page 176

Using ReadRecords, Page 177

Passing fields in the correct order, Page 177

Working with secure data in reports, Page 179

Handling the Format event, Page 179

...including comments regarding Calculating Results, Page 180 Changing the contents of a Text object, Page 180

Changing OLE object images, Page 182

Working with Sections, Page 182

Working with the ReportObjects collection, Page 183

Working with the FieldObject object, Page 184

Working with the SubreportObject object, Page 185

Working with the Database and DatabaseTables objects, Page 186

Working with the CrossTabObject object, Page 186

Exporting a report, Page 187

The Application object, Page 187

Report events, Page 188

Microsoft Access Sessions, Page 189

Programmatic ID, Page 189

Distributing reports as part of the application, Page 190

Saving reports as external files, Page 190

Saving data with reports, Page 191

The Report Designer Component

174

Report Designer Object Model Introduction

The Seagate Crystal Report Designer Object Model has been optimized for easy access to reports created using the Report Designer Component and the objects within those reports. Rather than forcing you to navigate through a complex hierarchy of collections and objects to get at a single report element, the Report Designer Object Model presents a flatter hierarchy that requires only obtaining, at most, a couple layers of objects to get at any report element. This hierarchy begins with the Report object.

Obtaining a Report object

Many existing ActiveX object models require declaring and creating a high level object, such as an Application object, before you can work directly with the data. The Report Designer Component, however, allows direct access to the Report object, giving you control over your reports with a small amount of Visual Basic code.

Assuming you have added the Report Designer Component to a Visual Basic application, and the (Name) property of that component is set to CrystalReport1, the following code demonstrates how to obtain a Report object representing that component:

Dim cr As CRAXDRT.Report

Set cr = New CrystalReport1

The first line declares cr as Report object from the CRAXDRT object library, the Report Designer Component’s object library. The second line of code defines cr as a new instance of the report in the CrystalReport1 component in your application.

Displaying the report in the Smart Viewer

Obtaining a Report object allows you to manipulate the report at runtime. Of course, once you make changes to the report, you will frequently want to display it through the Smart Viewer. The Smart Viewer is an ActiveX control that actually sits inside a Visual Basic form. Assuming you have added the Smart Viewer to a form named frmViewer, and the Smart Viewer control is named CRViewer1, the following code displays the report.

frmViewer.CRViewer1.ReportSource = cr

frmViewer.CRViewer1.ViewReport

frmViewer.CRViewer1.Visible = True

This code can be added to the Load event of the frmViewer form. When the form loads, the report is displayed.

After looking over the previous sections on obtaining a Report object and displaying the report in the Smart Viewer, you will quickly see how little code is required to perform the basic operations of displaying a report on screen. The Report Designer object model is designed to simplify your code and reduce project development time.

The Report Designer Component

175

Setting a new data source for the report

One of the most common tasks with the Report Designer Component is to specify a different data source at runtime from the one used to design the report. For example, you may want to provide a report containing a default set of data, then allow your users to customize the results by making selections in your application. At runtime, you create a new set of data based on the user’s choices, then use that new data source to replace the default data in the report.

Data Definition Files allow you to design the report at runtime without even specifying an actual data source. For instance, your data source may be highly dynamic, or it may even be nonexistent until runtime. You can design the report based on the Data Definition file, then create your data source at runtime and hand it off to the report before printing.

The following code creates a new ADO Recordset object, assigns the new recordset to the report, then assigns the report to the Smart Viewer control for display on screen.

Private Sub Form_Load()

Dim Report As New CrystalReport1

Dim rs As New ADOR.Recordset

rs.Open "SELECT * from Customer WHERE country = 'USA'", _ "DSN=Xtreme sample data;", adOpenKeyset

Report.Database.SetDataSource rs

CRViewer1.ReportSource = Report

CRViewer1.ViewReport

End Sub

Notice that this code is handled in the Load event of a Form. This would normally be the form that contains the Smart Viewer ActiveX control. Handling this process during the load event of the Smart Viewer form is the quickest and easiest means of assigning new data to the report.

In the first line of code, a new Report variable is defined as an instance of the Report Designer Component that has been added to the Visual Basic project. Immediately after that, a new ADO Recordset object is defined. The Recordset object is then assigned a set of data (a recordset) using a SQL query statement. In this example, the statement is hard-coded into the application. However, you could easily design an application that dynamically generates a query statement based on a user’s input or selections.

Next, the SetDataSource method of the Report Designer Component’s Database object is used to specify that the new ADO Recordset should be used to generate the report. SetDataSource accepts a single argument: the Recordset object itself. This argument could also be a DAO Recordset or an RDO Resultset, or any other ActiveX data source, as described in Active Data Driver, Page 118. The only restriction on the data source assigned using SetDataSource is that it contain fields that match the fields used to originally create the report. For instance, if the report was created using three string fields named SSN, LastName, and FirstName, the new data source must also consist of three string fields named SSN, LastName, and FirstName.

The last two lines of code in this example assign the report object containing the new data source to the Smart Viewer ActiveX control that appears in the Form, and displays the report. When the Form is loaded by your Visual Basic application, the report will be displayed with the new data from the ADO Recordset.

The Report Designer Component

176

Соседние файлы в папке crystal