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

Connecting to data with DAO

Use the following steps as a guide for connecting to a data source through DAO:

1On the Data Tab of the Create Report Expert, click Project. The Select Data Source dialog box appears.

2Click the DAO option to connect to your data through DAO. The DAO controls are activated.

3From the Database drop-down list, select the database format you want to use for your report. For this example, select Access.

4In the DAO text box, enter the path and file name of the database you want to use, or use the Browse button to locate the database. For this example, select the Xtreme.mdb file. It is located in the Seagate Crystal Reports directory on your system (C:\Program Files\Seagate Software\Crystal Reports by default).

5When the path and file name of the database appear in the DAO text box, click Next in the Select Data Source dialog box. The Select Recordset dialog box appears.

6If the database requires logon information, enter a user name and password in the Logon section of the dialog box.

7In the Recordset section of the dialog box, select either Pick from a list of Database Objects or SQL.

The Pick from a list option provides a list of object types and objects for each type. For this example, verify that Pick from a list is selected.

The SQL option allows you to write an SQL query statement.

8From the Object Type drop-down list, select Table to see a list of database tables.

9From the Object drop-down list, select Customer.

10Click Finish in the Select Recordset dialog box. dao appears in the list of Tables on the Data Tab of the Create Report Expert.

Data Environments

Data Environments (introduced with VB 6) allow for interactive creation of ADO Objects at design time, and easy access to data at run time. Through the Data Environment Designer you create Data Environment objects to connect with Data sources (Connection Objects), and access data from those sources (via Command Objects) at design time and run time.

The following steps illustrate how to create and connect to Data Environment objects for Microsoft Access type data sources (.mdb files). This algorithm can be used as a general guide for any data source for which an OLE DB provider exists. Consult the Microsoft documentation for more detailed information on the Data Environment Designer.

Add the Seagate Crystal Report Designer Component and the Data Environment Designer to your project

1Select Components from the project menu.

2In the Components dialog box, click on the Designers tab.

3Click on the Crystal Reports 7 checkbox and the Data Environment checkbox.

The Report Designer Component

163

Create a Data Environment Object

1 Select Add Data Environment from the Project menu. The Data Environment dialog box appears, showing a tree containing the Data Environment icon and two folders labeled Connections and Commands respectively.

2 Double click on the Connections folder. A Connection object icon should appear (if not, right click on the Connections folder and select Add Connection from the menu).

3 Right click on the Connection object icon and select Properties. The Data Link Properties dialog box will appear.

4 Click on the tab labeled Provider. From the list of data providers select an Microsoft Jet 3.51 OLE DB Provider. Click Next.

5Click on the Connection tab. Enter the database name and logon information.

6 Click on the button labeled Test Connection to verify that you have a valid connection to the database. Then click Okay

7 Right click on the folder labeled Commands and select Add Command. The Command object dialog box will appear.

8 Click on the tab marked General. Make sure that the setting marked Connection has the name of the connection object.

9 In the area of the dialog box marked Source of Data click on Database Object and select Table from the pull down list.

10From the pull down list labeled Object Name select Customer.

11Click Apply, then Click Okay

Connect to data with the Data Environment Object

1From the Project menu select Add Crystal Reports 7. The Report Gallery dialog box will appear.

2In the area of the Report Gallery dialog box titled Choose Expert, click on Standard. the Standard Report Expert dialog box will appear.

3 Click on the tab marked Data. In the area of the dialog box marked Data Source click on Data Environment. The Data Environment dialog box will appear.

4Expand the Data Environment object tree. Click on the Command object icon, then click Okay. The name of the command object should appear in the list of tables.

Data Definition Files

Data Definition Files allow you to create reports using the Report Designer Component without specifying an actual data source at design time. Instead, you base your report 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. The data source applied to the report at runtime can be created through

The Report Designer Component

164

Data Access Objects (DAO), Remote Data Objects (RDO), or ActiveX Data Objects (ADO).

A report designed with a Data Definition File 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. These field types are specified in the Data Definition File. For example, the following is an example of the contents of a Data Definition file that could be replaced at runtime by the Orders table in the Xtreme database:

Order ID

 

long

1

Order Amount

currency

1.00

Customer

ID

long

1

Employee

ID

long

1

Order Date

date

Jan 5, 1994

Required Date

date

Jan 5, 1994

Ship Date

 

date

Jan 5, 1994

Ship Via

 

string 20

string sample value

Shipped

 

boolean

TRUE

PO#

 

string 50

string sample value

Payment Received

boolean

TRUE

As you can see, the Data Definition File is a simple ASCII text file with fields listed on separate lines. Each field in the text file represents a field that must exist in the true ADO, RDO, or DAO data source that replaces the Data Definition file at runtime. At design time, you create your report based on the Data Definition file. The three columns of the file provide the name of the fields, the data type for the fields, and a sample value. String fields, as you can see, also require a value for the length of the string. The values in the sample value column are optional and will only be used if you neglect to replace the Data Definition file with a true data source.

At runtime, you change the data source pointed at by the report using the SetDataSource method. The new data source can be a Recordset or Resultset object produced using DAO, RDO, ADO, or through the Visual Basic Data Control. Once the Report Designer Component obtains the Recordset or Resultset object from the runtime data source, it can produce and display the final report using actual data. The process saves you the time of creating a complete data source prior to designing the report and your application.

Passing fields in the correct order

When defining a Recordset or Resultset object to pass to your report at runtime, you must pay attention to the order in which the fields are added to the recordset. Fields must appear in the recordset in the same order in which they appear in the Data Definition file. In addition, all fields that appear in a Data Definition file must be included in the recordset, whether or not they are actually used in the report. For more information, see

Passing fields in the correct order, Page 177.

The Report Designer Component

165

Creating a Data Definition File

The Report Designer Component provides the Database Definition Tool, a built in tool for designing and producing Data Definition Files. The following steps explain how to design a Data Definition File. From the Data Tab of the Create Report Expert:

1Click the Project button. The Select Data Source dialog box appears.

2Click the Data Definition option to create a report based on a Data Definition File.

3Click the New button next to the Data Definition text box. The Database Definition Tool appears.

4Use the Database Definition Tool to create fields for your Data Definition File. Use the controls to enter a field name, field type, and sample data. If you select String as the field type, you will also be asked to specify a maximum string length.

5Click Add to add each new field to your file. The new field will appear in the list at the bottom of the Database Definition Tool.

6Continue adding as many fields as necessary for your Data Definition File by entering the information in the controls of the Database Definition Tool, and clicking Add each time.

7You can delete a field that you have created by selecting the field in the list box and clicking Delete.

8Click the Close button in the right corner of the title bar when you are finished designing your Data Definition File. You will be prompted to save the new file.

9Click Yes, and the Save File As dialog box appears. Save the file in a convenient directory for your Visual Basic application. The resulting path and file name appear in the Data Definition text box of the Select Data Source dialog box.

10Click Finish. The name of the Data Definition File appears in the Tables list of the Data Tab in the Create Report Expert.

11Continue designing your report using the Create Report Expert and the Report Designer Component.

Obtaining a Recordset from the Runtime Data Source

Once you have created the Data Definition File and designed a report based on that file, you can begin programming your application to obtain a Recordset or Resultset object from a runtime data source. This data source can be opened through DAO, RDO, ADO, or the Visual Basic Data Control. The following tutorial briefly demonstrates how you can obtain a simple ADO Recordset object.

1Declare an instance of the ADO Recordset object in your Visual Basic application. This can be handled in the declarations section of a form or module.

Dim rs As New ADODB.Recordset

2Obtain a set of records from an ODBC data source.

rs.Open “SELECT * FROM Customer”, “DSN=Xtreme sample data;”, adOpenKeyset

The Report Designer Component

166

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