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

In addition, a self-extracting executable located in the \Program Files\Seagate Software\Crystal Reports\sample\sam32aut (or sam16aut) directory contains three small sample applications that demonstrate various aspects of the Crystal Report Engine Automation Server. Simply run the SAM32AUT.EXE (32-bit) or SAM16AUT.EXE (16-bit) application to install the samples. The three samples are:

AUBASIC

Demonstrates the basic code required to open a report and print, preview, or export it using the Crystal Report Engine Automation Server.

AUBROWSE

Demonstrates how to browse through the areas of a report and access the objects in each area.

AUFMLA

Demonstrates how to get and set record selection formulas, group selection formulas, and SQL queries stored with a report.

Active Data Driver

Modern Visual Basic applications often use advanced ActiveX components to connect to data sources. These data sources may include Data Access Objects (DAO), Remote Data Objects (RDO), OLE DB providers, such as ActiveX Data Objects (ADO), or the Visual Basic data controls. Using the Active Data Driver for Seagate Crystal Reports, you can design reports for your Visual Basic applications that use these same ActiveX data sources. The Active Data Driver also supports Crystal Data Objects (CDO) and the Crystal Data Source Type Library. For more information on RDO, DAO, and ADO, refer to Microsoft documentation. For information on the Data Control, refer to your Visual Basic documentation. For information on CDO, see Crystal Data Object, Page 128. For information on the Crystal Data Source Type Library, see Crystal Data Source Type Library, Page 131.

Occasionally, you may also need to create a report when the data source is not actually available at design time. Highly dynamic data may only be available at runtime. In such cases, the Active Data Driver supports the use of Data definition files, tab separated text files that define the fields in a data source but not the actual data.

Normally, developing applications using the Crystal Report Engine requires designing and saving one or more report files in advance to be accessed by the application at runtime. This process requires that the programmer has access to the data during design time, and that the application, upon installation, also installs whatever database drivers and files are required to make sure the reports can connect to the required data.

An alternative to runtime connectivity, of course, is to save data with the report files. The data is neatly packaged and available whenever the report is requested from your custom application. However, saving data with a report increases the file size of the report, wasting disk space. Furthermore, this technique produces a static report file in which the data can not be updated without connectivity to the database.

The Crystal Active Data Driver allows you to create report files at design time without specifying an actual data source. Instead, the report is based on a data definition file, an ASCII text file with place holders to represent database fields. At runtime, you add code to your application to specify the actual source of data for the report.

Visual Basic Solutions

118

The following topics are discussed in this section.

Data Definition Files, Page 119

Using the Active Data Driver, Page 119

Creating Data Definition Files, Page 123

Using ActiveX Data Sources at Design Time, Page 126

Data Definition Files

A report file designed using a data definition file, instead of a specific database or ODBC data source, contains information about the kind of data to place in the report instead of information about an actual data source. It looks for field types, rather than actual fields. For an example of a data definition file, refer to the file ORDERS.TTX installed in the \Program Files\Seagate Software\Crystal Reports directory.

At design time, you create your report based on the data definition file. Previewing or printing the report at design time has little value except to format field placement and style. Since there is no real data in the text file, you can not preview or print any data at design time.

NOTE: You can add sample data to the data definition file so that values will appear for each field in the Preview Tab at design time, but the values will be identical for all records, and grouping will not be available.

At runtime, your application opens the report file, just as it would any other report file. Instead of simply formatting and printing the file at runtime, though, you change the data source pointed at by the Crystal Active Data Driver, which is the data definition file, to a Recordset or Rowset object for an ActiveX data source such as ADO, RDO, DAO, or the Crystal Data Sources (see Crystal Data Object, Page 128), and the Crystal Data Source Type Library (see Crystal Data Source Type Library, Page 131).

Once the Crystal Active Data Driver obtains the Recordset from the runtime data source, the Crystal Report Engine can generate the actual report using existing data. The entire process saves you time designing reports and produces reports that are much more flexible and portable at runtime. For more information on data definition files, see Creating Data Definition Files, Page 123.

Using the Active Data Driver

Designing and generating reports using the Crystal Active Data Driver is a straightforward process, but requires several specific steps:

1.Select the design time data source, Page 120

2.Design the Report, Page 120

3.Obtain a Recordset from the Runtime Data Source, Page 121

4.Open the Report, Page 122

5.Pass the Recordset to the Active Data Driver, Page 122

6.Print the Report, Page 123

Visual Basic Solutions

119

The following sections demonstrate this process using the Crystal Active Data Driver with the Crystal Automation Server in Visual Basic 4.0.

Select the design time data source

When designing a report for your Visual Basic application, you can specify any ActiveX data source using the Active Data Driver, or you can specify a data definition file so that the actual data is specified at runtime only. The following example uses the sample data definition file included with Seagate Crystal Reports:

1Click New Report in the Seagate Crystal Reports Welcome dialog box, or click the New button on the Seagate Crystal Reports toolbar. The Report Gallery appears.

2Click a Report Expert button in the Report Gallery. In this example, you can click Standard. The Create Report Expert appears.

3On the Data Tab, scroll down until you see the Active Data button. If this button does not appear on the Data Tab, you have not correctly installed the Active Data Driver. Run Seagate Crystal Reports Setup again.

4Click the Active Data button, and the Select Data Source dialog box appears.

5Click the Data Definition option in the Select Data Source dialog box, and click Browse to locate a data definition file. The Select Data Definition File dialog box appears.

6Locate the ORDERS.TTX file in the \Program Files\Seagate Software\Crystal Reports directory, and click Open. The specified file appears in the text box for the Data Definition option of the Select Data Source dialog box.

7Click Finish, and orders appears on the Data Tab of the Report Expert in Seagate Crystal Reports.

NOTE: For information on specifying an OLE DB provider or other ActiveX data source at design time instead of a data definition file, see Using ActiveX Data Sources at Design Time, Page 126.

Design the Report

Once you have selected a data definition file or an ActiveX data source, you can design your report just as you would design any other report.

1Click the Fields Tab of the Report Expert. The data definition file orders appears as a database table in the Database Fields list box. Each of the fields defined in orders.ttx appears as a field in the orders table.

2Add fields to your report just as you would normally add fields to a report using the Report Expert.

3Continue designing the report using the Report Expert. When finished, click Design Report. Since the report is based on a data definition file, there is no point in previewing it at this time.

4Apply any formatting or other changes that you feel are necessary to fine-tune the look of your report. Save the report when finished.

NOTE: Before saving your report, be sure to turn off the Save Data with Report option under the File menu. The sample data stored with the data definition file is unnecessary at runtime, and will only increase the size of your report file.

Visual Basic Solutions

120

Obtain a Recordset from the Runtime Data Source

Once you have selected a data source or data definition file and designed a report based on that data source or file, you can begin programming your Visual Basic application to obtain a recordset from an ActiveX data source, open the report file, set the report file to use the recordset object from the ActiveX data source, then print or export the report file. This process requires using the functionality of the Crystal Active Data Driver in conjunction with the Crystal Report Engine.

Either the Crystal Report Engine API or the Crystal Report Engine Automation Server can be used with the Active Data Driver. However, the following tutorials use the Crystal Report Engine Automation Server in Visual Basic 5.0. This section assumes a familiarity with the Crystal Report Engine Automation Server. If you need more information on how to use the automation server, see the Crystal Report Engine Automation Server, Page 111.

To begin, you must obtain a Recordset object from a runtime ActiveX data source. This data source can be opened through DAO, RDO, ADO, the Visual Basic Data Control, Crystal Data Objects (CDO), or a class that implements the Crystal Data Source Type Library. For information on DAO, RDO, and ADO, refer to Microsoft documentation. For information on the Visual Basic Data Control, refer to your Visual Basic documentation. For information on CDO, see Crystal Data Object (page 128). For information on the Crystal Data Source Type Library, see Crystal Data Source Type Library, Page 131.

This tutorial creates a Recordset object from the Orders table of the XTREME.MDB sample database using DAO. The Recordset concept is used by DAO, ADO, and the Crystal Data Source Type Library. If you are using RDO, you will need to obtain a rdoResultSet object. If you are using CDO, you will need to obtain a Rowset object (see Crystal Data Object (page 128)).

NOTE: You must add the Data Access Objects component to your Visual Basic project before performing the following steps. For instructions on using DAO with Visual Basic, refer to your Visual Basic documentation.

1.Declare variables for the Database and Recordset objects in your Visual Basic application. This can be handled in the declarations section of a form or module. Use code similar to this:

Dim db As New DAO.Database Dim rs As DAO.Recordset

2.Obtain a Database object from the Xtreme database.

Set db = DBEngine.Workspaces(0).OpenDatabase( _

“c:\Program Files\Seagate Software\Crystal Reports\xtreme.mdb”)

3.Obtain a Recordset object from the Orders table of the Xtreme database.

Set rs = db.OpenRecordset(“Orders”, dbOpenTable)

Visual Basic Solutions

121

Open the Report

Once you have obtained a Recordset object, you can begin working with the report file you created earlier. This example uses the Crystal Report Engine Automation Server to open a report file.

NOTE: You must add the Crystal Report Engine Automation Server component to your Visual Basic project before performing the following steps. For complete information on using the Automation Server, see Crystal Report Engine Automation Server, Page 111.

1.Declare variables for the Application and Report objects that you will obtain from the Crystal Report Engine Object Library in the automation server. This can be handled in the declarations section of a form or module.

Dim app As CRPEAuto.Application Dim report As CRPEAuto.Report

2.Create an Application object with the Crystal Report Engine Automation Server.

Set app = CreateObject(“Crystal.CRPE.Application”)

3.Obtain a Report object by opening the report file you created earlier. This example uses the file ORDERS.RPT.

Set report = app.OpenReport(“c:\reports\Orders.rpt”)

Pass the Recordset to the Active Data Driver

The Recordset object gets passed to the Active Data Driver through the SetPrivateData method of the DatabaseTable object in the Crystal Report Engine Object Library. You must first obtain a DatabaseTable object from the Report object, then you must use the SetPrivateData method to set the report to point at the recordset object for your Active data source. The Crystal Report Engine Automation Server uses the Active Data Driver itself to replace the data definition file, at runtime, with the Active data source.

The following code demonstrates how to obtain a DatabaseTable object from the Report object:

Dim reportDb As CRPEAuto.Database

Dim reportTables As CRPEAuto.DatabaseTables

Dim reportTable As CRPEAuto.DatabaseTable

Set reportDb = report.Database

Set reportTables = reportDb.Tables

Set reportTable = reportTables.Item(1)

The Item property in the DatabaseTables collection lets you specify which table in the database you are replacing. Since the data definition file acts as a database with a single table, you should pass 1 to the Item property.

Once you have a DatabaseTable object for the Report object, you can pass the Active data source to the Report object using the SetPrivateData method. This method requires two parameters. The first is a value indicating that the data source you are passing to the report is an ActiveX data source. This value must be 3. The second parameter is the data source itself. For example:

reportTable.SetPrivateData(3, rs)

Visual Basic Solutions

122

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