Вы находитесь на странице: 1из 4

STEPS TO IMPLEMENT CRYSTAL REPORT TO YOUR VB APPLICATION

(My comments are in parentheses)

Using the Active Data Driver


====================================================================
Designing and generating reports using the Crystal Active Data Driver is a
straightforward process, but requires several specific steps:
� Select the design time data source
� Design the Report
� Obtain a Recordset from the Runtime Data Source
� Open the Report
� Pass the Recordset to the Active Data Driver
� Print the Report
The following sections demonstrate this process using the Crystal Active Data
Driver with the Report Designer Component Automation Server in Visual Basic 6.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 Crystal Reports:
1. Click New Report in the Crystal Reports Welcome dialog box, or click the New
button on the Crystal Reports toolbar.
2. In the Crystal Report Gallery dialog box click Using the Report Expert. In
this example, you can click Standard from the Choose an Expert box. Then Click OK.
3. In the Standard Report Expert Dialog box click Database. (OR FOR EXISTING
REPORT GO TO MENUE Database/Set Location…)
4. In the Data Explorer dialog box:
� expand More Data Sources
� expand Active Data (THIS STEP IS CRUCIAL (!!!!) IT ACTIVATES
ActiveDataDriver, if you connect to database other way, you wont be able to use
your report from VB environment. The report will not take Recordset as DataSource,
will try to get to the Database through report connection, it fails and Message Box
“Server not yet opened” will be shown up – this is for SQL
Server)
� expand Active Data (ADO).
5. In the Select Data Source dialog box click the ODBC (ADO) option, select
Xtreme Sample Database from the drop-down list, and then click OK.
6. In the Data Explorer dialog box click Add.
7. In the Select Recordset dialog box select Orders from the Object list, and
then click OK.
8. In the Data Explorer dialog box click Close. The Orders table appears as ado
in the Tables available for report box, under the data tab, in the Standard Report
Expert dialog box.
Note: For information on specifying an OLE DB provider or other ActiveX data
source at design time, see Using ActiveX Data Sources at Design Time.

Design the Report


====================================================================
(Create the report as usual)
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.
1. Click the Fields Tab of the Standard 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.
2. Add fields to your report just as you would normally add fields to a report
using the Standard Report Expert.
3. Continue designing the report using the Standard 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.
4. Apply 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.

Obtain a Recordset from the Runtime Data Source


====================================================================
(HOW YOU GET THE RECORDSET IS NOT IMPORTANT, WHAT IMPORTANT, THE RECORDSET MUST
(!!!!) BE EXACT MIRROR OF YOUR DESIGN-TIME DATABASE TABLE, INCLUDING THE ORDER OF
FIELDS !!!!!)

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 Report Designer Component
or one of the other Crystal Reports development tools. See the "Visual Basic
Solutions" chapter in the Crystal Reports Technical Reference Guide, or the Crystal
Reports Developer's Help (CrystalDevHelp.chm) for more information on the other
Crystal Reports development tools.
The following tutorials use the Report Designer Component Automation Server in
Visual Basic 6.0. This section assumes a familiarity with the Report Designer
Component Automation Server. If you need more information on how to use the
automation server, see the Report Designer Component Object Model.
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 . For
information on the Crystal Data Source Type Library, see Crystal Data Source Type
Library.
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).
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.
� 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
� Obtain a Database object from the Xtreme database.
Set db = DBEngine.Workspaces(0).OpenDatabase( _
"c:\Program Files\Seagate Software\Crystal Reports\xtreme.mdb")
� Obtain a Recordset object from the Orders table of the Xtreme database.
Set rs = db.OpenRecordset("Orders", dbOpenTable)

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 Report Designer Component
Automation Server to open a report file.
Note: You must add the Report Designer Component 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.
� Declare variables for the Application and Report objects that you will obtain
from the Report Designer Component Object Library in the automation server. This
can be handled in the declarations section of a form or module.

Dim CRXApplication As New Craxdrt.Application


Dim CRXReport As Craxdrt.Report

� Obtain a Report object by opening the report file you created earlier. This
example uses the file ORDERS.RPT.
Set CRXReport = CRXApplication.OpenReport("c:\reports\Orders.rpt", 1)

Pass the Recordset to the Active Data Driver


====================================================================
The Recordset object gets passed to the Active Data Driver through the
SetDataSource method of the Database object in the Report Designer Component Object
Library. You must first obtain a Database object from the Report object, then you
must use the SetDataSource method to set the report to point at the recordset
object for your Active data source. The Report Designer Component 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 Database object from the Report
object:

Dim CRXDatabase As Craxdrt.Database


Set CRXDatabase = CRXReport.Database

Once you have a Database object for the Report object, you can pass the Active data
source to the Report object using the SetDataSource method. This method requires
three parameters. The first is the data source itself. The second parameter 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 third is the table you are passing the data
source to. Since you should only have one table defining the structure of the
recordset , this should always be 1. For example:

CRXDatabase.SetDataSource rs, 3, 1

Print the Report


====================================================================
Now that the data source for the report has been set to the DAO Recordset, you can
print, preview, or export the report normally. For instance, the following code
prints the report to the default printer:
CRXReport.PrintOut
Once the data source has been set in the report object, runtime reporting can
proceed normally. All features of the Report Designer Component are available to
you. See Report Designer Component Object Model for more information

Or show the Report (I would prefer this way more)


====================================================================

(Create the form, and put CRViewer1 control on it, from any part of your
application you want create an instance of this form, and call InitializeForm
function with 2 parameters – DataSource recordset , and Path to your Report)
Option Explicit
Dim CRXApplication As CRAXDDRT.Application
Dim CRXReport As CRAXDDRT.Report
Dim CRXDatabase As CRAXDDRT.Database

Private Sub Form_Load()


Me.WindowState = 2 'Maximized
End Sub

Private Sub Form_Resize()


rptCRViewer.Top = 0
rptCRViewer.Left = 0
rptCRViewer.Height = ScaleHeight
rptCRViewer.Width = ScaleWidth
End Sub

Public Sub InitializeForm(rs As ADODB.Recordset, Path As String)


Set CRXApplication = New CRAXDDRT.Application
Set CRXReport = New CRAXDDRT.Report
Set CRXDatabase = CRXReport.Database

Set CRXReport = CRXApplication.OpenReport(Path, 1)


CRXDatabase.SetDataSource rs, 3, 1

CRViewer1.ReportSource = CRXReport
CRViewer1.ViewReport
Me.Show
End Sub

P.S. These 5 lines of code took 2 days out of my life! So, save your time and say
thanks to me and my boss Dave Raskin who helped me a lot to resolve this problem.

P.P.S. Sorry for the naming conventions, I just wanted to make it looks closer to
Segate examples.

If you’d like to say Thanks, e-mail me.


Sincerely,
Vladimir Rozenbaum

Вам также может понравиться