Crystall Reports
Crystall Reports
Crystall Reports
Overview
This document provides information about using Data Definition (TTX) files
and Active Data with Crystal Reports. This document outlines how to create a
report that has a Microsoft ActiveX Data Objects (ADO) recordset passed to it
at runtime.
Contents
INTRODUCTION ............................................................................................ 2
CREATING THE DATA DEFINITION (TTX) FILE ............................................... 2
Data Definition Tool....................................................................................3
Using Active Data driver functions..............................................................5
CreateFieldDefFile() .......................................................................................... 5
CreateReportOnRuntimeDS()............................................................................ 6
CREATING THE REPORT............................................................................... 6
CREATING A DATA DEFINITION (TTX) FILE IN VB OFF AN ADO RECORDSET .. 7
PASSING AN ADO RECORDSET TO THE REPORT VIA THE CRYSTAL
AUTOMATION SERVER ................................................................................. 8
PASSING AN ADO RECORDSET TO THE REPORT VIA THE RDC ...................... 9
PASSING AN ADO RECORDSET TO A SUBREPORT VIA THE CRYSTAL
AUTOMATION SERVER ............................................................................... 11
PASSING AN ADO RECORDSET TO A SUBREPORT VIA THE RDC .................. 13
FINDING MORE INFORMATION .................................................................... 15
CONTACTING CRYSTAL DECISIONS FOR TECHNICAL SUPPORT .................... 15
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 1
Crystal Decisions Using Data Definition (TTX) files
Introduction
Traditionally, reports created in Crystal Reports have been created from physical
databases. The only limitation a report had with connecting to a database was
whether the database had a native driver (direct connection) or an ODBC driver.
With the 32-bit Active Data driver, P2smon.dll (Pdsmon.dll for 16-bit), Crystal
Reports has opened up to other types of data sources. Now, a report can be
created so that it can report from Active Data that exists in an application.
This document uses Visual Basic (VB) 6 and Crystal Reports 8 for its steps and
samples. The following sections demonstrate how to create a report that reads
Active Data using either the Crystal Report Engine Automation server or the
Crystal Report Designer Component (RDC).
Steps 1 and 2 demonstrate how to create a Data Definition (TTX) file as well as
using that TTX file to create a report. Steps 3 to 7 demonstrate passing a
Microsoft ActiveX Data Objects (ADO) recordset to a Crystal Report.
A Data Definition file is a tab separated text file, with a TTX file extension.
Basically, it is an outline of a table structure. Each line in a TTX file contains a
field name, data type, length (if the data type is a string), and one piece of
sample data. The contents of the sample Data Definition file, Orders.ttx, are
shown below.
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 2
Crystal Decisions Using Data Definition (TTX) files
1. Open the Crystal Reports designer. Click the File menu then click New.
2. The Seagate Crystal Report Gallery appears. Select Using the Report
Expert and highlight the Standard expert then click OK.
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 3
Crystal Decisions Using Data Definition (TTX) files
3. The Standard Report Expert dialog box appears. Under the Data tab,
click Database. The Database Explorer appears. Click More Data
Sources then click Active Data.
4. Double click Active Data (Field Definitions Only). The Select Data
Source dialog box appears. Click Data Definitions.
5. The Database Definition Tool dialog box appears. Enter a value for
the Field Name, Field Type, and Sample Data. Click Add to place
the values into the grid.
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 4
Crystal Decisions Using Data Definition (TTX) files
7. Click the File menu then click Save As… to save the TTX file.
The TTX file has now been created using the Data Definition Tool.
The next step is to create a report based on this TTX file.
• CreateFieldDefFile()
These functions are purely for development purposes (they are not required at
runtime).
NOTE CreateReportOnRuntimeDS( ) does not place any fields on the report. A blank report is
created instead.
CreateFieldDefFile()
Parameter Description
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 5
Crystal Decisions Using Data Definition (TTX) files
CreateReportOnRuntimeDS()
Parameter Description
LpUnk The active data source used to create the field definition
file. In C or C++, this is a pointer to an IUnknown
derived COM interface relating to a DAO or ADO
Recordset. In Visual Basic, this is a Recordset or Rowset
object.
ReportFile The path and file name of the report file to be created.
FieldDefFile The path and file name of the field definition file to be
created.
BoverWriteFile If a field definition file already exists with the specified
path and file name, this flag indicates whether or not to
overwrite that file.
BlaunchDesigner If True (1), Crystal Reports is launched with the newly
created report file opened. Crystal Reports must be
installed on the system.
NOTE Since a TTX file is a tab-separated text file, it can be manually created or edited using
Microsoft Notepad or any other text editor. This method is not recommended for creating
TTX files due to possible typing errors.
Refer to the section Creating a data definition (TTX) file in VB off an ADO
recordset for an example of using CreateFieldDefFile() and
CreateReportOnRuntimeDS() in VB.
The following steps describe how to create a report from a Data Definition
(TTX) file.
1. Open the Crystal Reports designer. Click the File menu then click New.
2. The Seagate Crystal Report Gallery appears. Select Using the Report
Expert and highlight the Standard expert then click OK.
3. The Standard Report Expert dialog box appears. Under the Data tab,
click Database. The Database Explorer appears. Click More Data
Sources then click Active Data. .
4. Double click Active Data (Field Definitions Only). The Select Data
Source dialog box appears. Click the Browse button then browse to your
TTX file and click OK.
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 6
Crystal Decisions Using Data Definition (TTX) files
6. This will return you to the Data Explorer. Under the Active Data (Field
Definitions Only) tree is the path and filename of your TTX file. Highlight
your TTX file and click ADD then click Close.
7. You are brought back to the Standard Report Expert. Go to the Fields
tab and add some fields to display, then click Finish.
The report now displays in the Crystal Reports Designer with sample data.
Save this report.
1. Open a new project in Visual Basic (VB). Click the File menu and then
click New Project. Select Standard EXE and click OK.
2. Click the Project menu, and then click References. Select the Microsoft
ActiveX Data Object 2.x Library check box.
4. Under the Form_Load event of Form1, type the following lines of code:
NOTE The Xtreme Sample Database ODBC Data Source Name (DSN) is automatically created
when you install Crystal Reports 8.
5. Click the Project menu and click Add Module. Click Open and type the
following lines of code:
Declare Function CreateFieldDefFile Lib
"p2smon.dll"(lpUnk As Object, ByVal fileName As String,
ByVal bOverWriteExistingFile As Long) As Long
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 7
Crystal Decisions Using Data Definition (TTX) files
6. On a new line in the Form_Load event for Form1, type the following line
of code (Ensure that the folder C:\Temp exists. If it does not, modify the
FileName parameter to specify a valid path.):
HResult = CreateFieldDefFile(AdoRs,“C:\temp\AdoRs.ttx”,
True)
This creates the TTX file based off the ADO Recordset object.
Once the report is created using the TTX file, you need to pass your ADO
recordset to the report. This section shows you how to pass an ADO recordset
to a report using the Crystal Automation Server in VB 6.
The TTX file used to create the report in this project is created from the
Customer table in our sample database, Xtreme.mdb. The project connects to
the database using ADO and an ODBC connection to the Xtreme.mdb.
1. Open a new project in Visual Basic (VB). Click the File menu and then
click New Project. Select Standard EXE and click OK.
2. Click the Project menu, and then click References. Select the Microsoft
ActiveX Data Object 2.x Library and Crystal Report Engine 8 Object
Library check boxes.
This adds an ADO object library and a Crystal Automation Server reference
to your project.
3. Open the code window for Form1 and under the General Declarations
section, type the following lines of code:
NOTE When using the Crystal Automation Server it is important to DIM each object to avoid
errors in your code.
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 8
Crystal Decisions Using Data Definition (TTX) files
4. Under the Form_Load event of Form1, type the following lines of code:
‘sets our ADO recordset as the data for the first table
CrTable.SetPrivateData 3, AdoRs
5. Run this project to preview the report using the ADO recordset as data.
The TTX file used to create the report in this project is created from the
Customer table in our sample database, Xtreme.mdb. The project connects to
the database using ADO and an ODBC connection to the Xtreme.mdb.
1. Open a new project in Visual Basic (VB). Click the File menu and then
click New Project. Select Standard EXE and click OK.
2. Click the Project menu, and then click References. Select the Microsoft
ActiveX Data Object 2.x Library and the Crystal Reports 8 ActiveX
Designer Run Time Library check boxes.
This adds an ADO object library and the Crystal Report Designer
Component reference to your project.
3. Click the Project menu and then click Components. Select the Crystal
Report Viewer Control.
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 9
Crystal Decisions Using Data Definition (TTX) files
4. Open the code window for Form1 and under the General Declarations
section, type the following line of code:
5. Under the Form_Load event of Form1, type the following lines of code:
8. Under the Resize event of Form1, type the following lines of code:
‘Resize the Crystal Viewer if Form1 is resized
CrViewer1.Top = 0
CrViewer1.Left = 0
CrViewer1.Width = Form1.Width – 200
Crviewer1.Height = Form1.Height – 400
9. Run this project and the report will preview with the ADO recordset as the
data.
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 10
Crystal Decisions Using Data Definition (TTX) files
Once the main report and subreport are created using the TTX file, you need
to pass your Active Data recordset to the main report and subreport.
Since you have a subreport, you need to use the SetPrivateData method for
the subreport. You can do this by using the OpenSubreport method of the
Report object.
To pass an ADO recordset to a subreport and main report via the Crystal
Automation Server:
1. Open a new project in Visual Basic (VB). Click the File menu and
then click New Project. Select Standard EXE and click OK.
2. Click the Project menu, and then click References. Select the
Microsoft ActiveX Data Object 2.x Library and the Crystal Report
Engine 8 Object Library.
This adds a reference to the ADO object library and the Crystal
Automation Server to your project.
3. Open the code window for Form1 and under the General
Declarations section, type the following line of code:
NOTE When using the Crystal Automation Server it is important to DIM each object to avoid
errors in your code.
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 11
Crystal Decisions Using Data Definition (TTX) files
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 12
Crystal Decisions Using Data Definition (TTX) files
End Sub
5. Run this project to preview the main and subreport with the ADO
recordset as the data.
Once the main report and subreport are created using the TTX file, you need to
pass your ADO Recordset to the main report and subreport.
Since you have a subreport, you need to use the SetDataSource method for the
subreport. You can do this by using the OpenSubreport method of the
Subreport object.
To pass an ADO recordset to a subreport and main report via the RDC:
1. Open a new project in Visual Basic (VB). Click the File menu and then
click New Project. Select Standard EXE and click OK.
2. Click the Project menu, and then click References. Select the Microsoft
ActiveX Data Object 2.x Library and the Crystal Reports 8 ActiveX
Designer Run Time Library.
This adds a reference to the ADO object library and the Crystal Report
Designer Component reference to your project.
3. Click the Project menu and then click Components. Select the Crystal
Report Viewer Control.
4. Insert the Crystal Reports Viewer Control on Form1.
5. Open the code window for Form1 and under the General Declarations
section, type the following line of code:
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 13
Crystal Decisions Using Data Definition (TTX) files
NOTE When using the Crystal Automation Server it is important to DIM each object to avoid
errors in your code.
6. Under the Form_Load event of Form1, type the following lines of code:
Dim x As Integer
Dim y As Integer
‘open the ADO recordset for the main report
AdoRs.Open "Select * from Customer", "Xtreme Sample
Database", adOpenDynamic, adLockBatchOptimistic
‘open the ADO recordset for the subreport
AdoRs1.Open "Select * from Customer", "Xtreme Sample
Database", adOpenDynamic, adLockBatchOptimistic
‘Open the report.
Set CrRep = CrAppl.OpenReport(“C:\temp\temp.rpt”)
‘Set the database, database tables and database table for
‘the main report by using the SetDataSource method.
Set CrDatabase = CrRep.Database
Set CrDatabaseTables = CrDatabase.Tables
Set CrDatabaseTable = CrDatabaseTables.Item(1)
CrDatabaseTable.SetDataSource AdoRs, 3
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 14
Crystal Decisions Using Data Definition (TTX) files
CrDatabaseTable.SetDataSource AdoRs1, 3
End If
Next
Next
CRViewer1.ReportSource = CrRep
CRViewer1.ViewReport
End Sub
7. Run this project to preview the main and subreport with the ADO recordset
as the data.
Self-serve Support:
http://support.crystaldecisions.com/
Email Support:
http://support.crystaldecisions.com/support/answers.asp
Telephone Support:
http://www.crystaldecisions.com/contact/support.asp
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 15
Crystal Decisions Using Data Definition (TTX) files
1/20/2003 8:42 AM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 16