Crystal Report Designer Component in VC++
Crystal Report Designer Component in VC++
Crystal Report Designer Component in VC++
Overview
This document discusses integrating the Crystal Report Designer Component
(RDC) in Microsoft Visual C++.
Contents
INTRODUCTION ............................................................................................2
ADDING THE RDC RUNTIME LIBRARY IN VISUAL C++ ....................................2
WORKING WITH THE REPORT OBJECT ...........................................................3
VARIANT AND BSTR.................................................................................4
BSTR............................................................................................................ 4
VARIANT..................................................................................................... 5
Table of Variant Types ................................................................................ 5
WORKING WITH THE CRYSTAL REPORT VIEWER CONTROL ............................6
CLEANING UP..............................................................................................6
LOGGING ON TO A DATABASE ......................................................................7
LogonServer ................................................................................................ 7
SetLogonInfo ............................................................................................... 8
PASSING AN ACTIVE DATA RECORDSET TO A REPORT .................................9
PASSING PARAMETER VALUES ..................................................................10
Passing a String parameter....................................................................... 10
Passing a Number parameter.................................................................... 11
Passing a DateTime parameter................................................................. 11
OPENING A SUBREPORT............................................................................11
APPENDIX A - THE INTERFACES OF THE RDC.............................................14
CONTACTING CRYSTAL DECISIONS FOR TECHNICAL SUPPORT ....................16
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 1
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
Introduction
The Crystal Report Designer Component Automation Server (Craxdrt.dll, often
referred to as the RDC or Report Designer Component) is designed to take
advantage of several features of the Microsoft Visual Basic IDE. However, the
RDC can be integrated into other developer tools such as Microsoft Visual C++.
Visual C++ 5 and 6 provide native COM support which the RDC requires.
Accessing and implementing this support in Visual C++ is somewhat more
involved than is the case with Visual Basic. The method used to expose the
RDC’s object model in Visual C++ also differs as to how it is exposed in Visual
Basic.
The RDC is broken into two main components. The Automation server
component (Craxdrt.dll), and the Crystal Report Viewer control (Crviewer.dll)
The “Previewing a Report” section of this paper shows how to use these two
components of the RDC to preview a report. The “Setting report properties”
section extends on this and shows how to pass values (such as database logon
information or parameter field values) to a report at runtime.
NOTE You may find the RDC Browser utility helpful to understand and visualize the RDC’s
object hierarchy. The RDC Browser utility allows you to navigate through the object
hierarchy using an "Explorer tree" type interface.
RDC Browser can be downloaded from the Crystal Decisions Support site at:
support.crystaldecisions.net/docs
If you are using Crystal Reports 8.0 or higher, download the file:
RDC8_Browser.exe
If you are using Crystal Reports 7.0, download the file:
RDC_Browser.exe
2. In the application’s header file add the runtime library using the #import
keyword, for example:
IApplicationPtr pApplication;
4. In the class where you want use the Application object, create the
Application object, for example:
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 2
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
Whereas the steps for Visual Basic are fairly intuitive, the same perhaps cannot
be said for the Visual C++ example given above. A few points bear explanation.
Two objects are mandatory when working with the RDC, the Application object
and the Report object. Other objects may or may not be necessary when
working with a particular .rpt file (for example, if you need to connect a report
to its ODBC datasource, you would also need to use the Database object,
DatabaseTables collection and DatabaseTable object).
theApp.pApplication.CreateInstance("CrystalRuntime.Applicat
ion");
_bstr_t FileName("c:\\Reports\\myReport.rpt");
theApp.pReport = theApp.pApplication->OpenReport(FileName);
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 3
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
• Since the RDC runtime uses the IDispatch interface, method arguments
need to be passed using Basic Strings (type BSTR) for strings, and as
variants for every other data type. There will be more details on this in the
next section. “_bstr_t” is a class that creates a BSTR by passing a string in
quotations to the classes constructor.
• The back-slash is an escape character in Visual C++ so we need to double
them up. The first slash indicates that the next is to be interpreted literally.
The OpenReport method creates the Report object pReport. The OpenReport
method of the Application object takes the path to the .rpt file as a BSTR and
returns an IReport object.
When building a user-interface, CStrings are typically used to collect user input.
Fortunately, the CString class has a method called AllocSysString() which turns
the CStrings contents into a convenient BSTR.
For example, to instantiate an Application and Report object using a file dialog
prompting for a .rpt file, use the following lines of code:
theApp.pApplication.CreateInstance("CrystalRuntime.Applicat
ion");
theApp.pReport = theApp.pApplication->OpenReport(FileName);
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 4
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
With the BSTR containing the path to the .rpt file ready, the BSTR can now be
passed to the OpenReport() method of the Application object. This creates a
Report object that has all of the attributes of the .rpt file
VARIANT
For other argument data types, such as number, use a VARIANT. Whereas
VARIANT is a data type in VB, in Visual C++ VARIANT is a union. There are
always two members of the union that you need to set values to.
When using the VARIANT union, the first member indicates the VariantType
(.vt). The second member contains a value dependent upon the first member.
For example, if you want a VARIANT to contain an Integer value of ‘3’, use the
following code:
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 5
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
To add the Crystal Viewer to a project, open the Dialog resource for the class
that is too be responsible for displaying the report. With the Dialog resource
displayed, right-click on the dialog and choose "Insert ActiveX control…"
from the contextual-menu. A list of all of the registered controls will appear
from which the Crystal Viewer Control should be chosen. This will place the
Crystal Viewer control on the Dialog on which it should be resized as
appropriate.
The Crystal Viewer control has properties and methods that can be accessed
using the following steps:
m_Viewer.SetReportSource(theApp.pReport);
3. Once the Crystal Viewer knows which report to preview, use the
ViewReport() method to display the report in the Crystal Viewer
control.
m_Viewer.ViewReport();
Cleaning Up
After the report has been previewed, there are some clean-up tasks that should
be attended to.
Assuming that clicking the OK button in your application dismisses the preview
dialog and terminates the application, some housekeeping code should be added
to the handler for the OK button.
In this example, only an Application and Report object have been created.
These objects should be released and destroyed in reverse order from which they
were created.
theApp.pReport.Release();
theApp.pReport = NULL;
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 6
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
theApp.pApplication.Release();
theApp.pApplication = NULL;
Logging on to a Database
There are two methods that can be used to connect a report to a database:
• LogonServer
• SetLogonInfo
LogonServer
When connecting a report to a database at runtime, if the logon information is
the same, as used when designing the report, the LogOnServer method of the
Application object should be used.
1. The LogOnServer method takes five BSTR arguments. Use the “_bstr_t”
class to create BSTRs for the arguments.
NOTE • To find the DLLname for a report, click Convert Database Driver from the
Database menu in the Crystal Reports Designer. The DLLname is the value
displayed beside the From field.
• The Server, Database, UserID, parameters for the LogonServer method can be
found by clicking Set Location from the Database menu in the Crystal Report
Designer. The dialog screen that appears will contain the members just
mentioned.
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 7
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
SysFreeString(Server);
SysFreeString(Database);
SysFreeString(UserID);
SysFreeString(Password);
SysFreeString(DLLName);
SetLogonInfo
The other method that can be used to connect a report to a database is the
SetLogOnInfo method of the DatabaseTable object. This method does not
establish a connection immediately, but rather provides the report with the
information it needs to create a connection.
1. The LogOnServer method takes four BSTR arguments. Use the “_bstr_t”
class to create BSTRs for the arguments.
NOTE Unlike LogOnServer, there is no need to pass the DLL name argument (the DLL is read
from the report.
NOTE The Server, Database, UserID, parameters for the LogonServer method can be found by
clicking Set Location from the Database menu in the Crystal Report Designer. The
dialog screen that appears will contain the members just mentioned.
theApp.pReport->Database->Tables->GetItem(1)-
>SetLogOnInfo(Server, Database, UserID, Password);
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 8
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
NOTE The last line of code above (used to access the first DatabaseTable object) is equivalent
to the following lines of code:
IDatabasePtr pDatabase;
pDatabase = theApp.pReport->GetDatabase();
IDatabaseTablesPtr pTables;
pTables = pDatabase->GetTables();
IDatabaseTablePtr pTable;
pTable = pTables->GetItem(1);
1. Import the ADO library and rename EOF as EndOfFile to avoid naming
conflicts
pConnection.CreateInstance(__uuidof(Connection));
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 9
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
pRecordSet.CreateInstance(__uuidof(Recordset));
pRecordSet->Open("Customer", _variant_t((IDispatch
*)pConnection,true), adOpenKeyset, adLockBatchOptimistic,
8);
theApp->pReport->Database->Tables->GetItem(1)->SetDataSource
(_variant_t((IDispatch *)pRecordSet,true));
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 10
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
VARIANT NumberParam;
VariantInit(&NumberParam);
NumberParam.vt = VT_I2;
NumberParam.iVal = 10;
app->pReport->ParameterFields->Item[2]-
>AddCurrentValue(NumberParam);
VARIANT DateParam;
VariantInit(&DateParam);
DateParam.vt = VT_DATE;
Opening A Subreport
Sometimes it is necessary to open a subreport into its own Report object. For
example, you may want to log on for each table in the subreport using the
SetLogonInfo method.
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 11
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
To open a subreport:
1. Get the number of sections in the report in order iterate through each section
searching for subreport objects.
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 12
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 13
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
These objects and collections are listed in the Developer’s Help file index
(minus the leading "I").
IApplication
IArea
IAreas
IBlobFieldObject
IBoxObject
ICROleObject
ICrossTableGroup
ICrossTablGroups
ICrossTabObject
IDatabase
IDatabaseFieldDefinition
IDatabaseFieldDefinitions
IDatabaseTable
IDatabaseTables
IExportOptions
IFieldDefinition
IFieldDefinitions
IFieldMappingData
IFieldObject
IFormattingInfo
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 14
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
IFormulaFieldDefinition
IFormulaFieldDefinitions
IGraphObject
IGroupNameFieldDefinition
IGroupNameFieldDefinitions
ILineObject
IMapObject
IObjectSummaryFieldDefinitions
IOlapGridObject
IPage
IPageEngine
IPageGenerator
IPages
IParameterFieldDefinition
IParameterFieldDefinitions
IPrintingStatus
IReport
IReportEvent
IReportObject
IReportObjects
IRunningTotalFieldDefinitions
IRunningTotalFieldDefintion
ISection
ISectionEvent
ISections
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 15
Crystal Decisions Getting Started with the Crystal Report Designer Component in Microsoft Visual C++
ISortFields
ISpecialVarFieldDefinition
ISQLExpressionFieldDefinition
ISQLExpressionFieldDefinitions
ISubreportLink
ISubreportLinks
ISubreportObject
ISummaryFieldDefinition
ITableLink
ITableLinks
ITextObject
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
6/24/2002 3:52 PM Copyright 2001 Crystal Decisions, Inc. All Rights Reserved. Page 16