Using Automation with Crystal Reports and Delphi

By | March 11, 2011
Selec All Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
In order to use the automation features in Crystal Reports 8.0 and higher, you must first import the type libraries into Delphi.
 
1. From the Delphi IDE main menu, go to Project | Import Type Libary.
2. Highlight 'Crystal Report 8 Active X Designer Run Time Libary'.
3. In the Class Names box, change the following items (TReport and TDatabase
   conflict with standard Delphi components):
      TReport   >>> change to >>> TCrReport
      TDatabase >>> change to >>> TCrDatabase
4. Change the Palette page to Crystal Reports
5. Click 'Generate Component Wrapper'
6. Click 'Install...'
7. Click the Into New Package tab.
8. Click 'Browse', select a destination directory for the new package, enter.
   the filename 'CR_AUTO.DPK', then click the 'Open' button.
9. Enter the description 'Crystal Reports Automation Server' (or another one
   if you like), and click the 'OK' button.
10. At the confirmation box 'Package CR_AUTO.bpl will be built then installed.
   Continue?' choose 'No'.
11. Go back to the main menu and choose Project | Import Type Library.
12. This time choose the 'Crystal Report Viewer Control'.
13. Change the Palette page to Crystal Reports
14. Click 'Generate Component Wrapper'
15. Click 'Install...'
16. Click the Into Existing Package tab.
17. Browse or Select the 'CR_AUTO.DPK' package we created earlier.
18. Click 'OK'
19. This time the install confirmation box, click 'Yes'.
20. A message will appear notifying you that the following components have
    been registered: TApplication, TCRField, TCRFields, TCrReport,
    CRVEventInfo, TCRViewer, TCRVTrackCursorInfo, TReportSourceRouter.
    Click 'OK'
21. Save and Close the package.
22. We have now imported the appropriate type libraries and are ready to
    build a test application.
 
NOTE: Importing the Crystal Report Run Time Library registers a component TApplication.  This will conflict with TApplication - the Delphi standard component.  If you need to access the original TApplication in a form where you are using the Crystal Reports Server components or Crystal Reports viewer, you can access it as: Forms.Application.
 
-------------------------------------------------------------------------------
 
Next you must create a sample report in Crystal Reports to work with.  For purposes of this example, make a very simple report just until you are comfortable with the process, then expand on it from there.
 
Save this report to 'C:\MyReport.rpt'.
 
-------------------------------------------------------------------------------
 
Next let's setup the sample application:
 
1. In Delphi, create a New Application.
2. From the Crystal Reports tab drop an Application component onto the form
   and rename it 'CrystalApp'.
3. Next drop a CrReport component onto the form and rename it 'CrystalRpt'.
4. Finally drop a CRViewer component onto the form, rename it 'CrystalView',
   and change the align probably to alClient.
5. In the FormCreate procedure for the form, enter the following:
 
      CrystalApp.LogOnServer();   { <<<<<<<<<<<  SEE NOTES BELOW }
      CrystalRpt.ConnectTo(CrystalApp.OpenReport('C:\MyReport.rpt'));
      CrystalView.ReportSource := CrystalReport.DefaultInterface;
      CrystalView.ViewReport;
 
 
   The CrystalApp.LogOnServer call can take a wide variety of input depending
   on how you are connecting to the database used in your report.  The
   following is an excerpt from Crystal Reports Developer Help File:
 
   Syntax
   ------
   LogOnServer ( pDllName As String, pServerName As String, [pDatabaseName],
                 [pUserID], [pPassword] )
 
 
   Parameters      Description
   ----------      ------------------------------------------------------------
   pDllName        Specifies the name of the DLL for the server or password
                   protected non-SQL table you want to log on to, for
                   example "PDSODBC.DLL". Note that the dllName must be
                   enclosed in quotes. DLL names have the following naming
                   convention: PDB*.DLL for standard (non-SQL) databases,
                   PDS*.DLL for SQL/ODBC databases.  Specifies the name of
                   the DLL for the server or password protected non-SQL table
                   you want to log on to, for example "PDSODBC.DLL". Note that
                   the dllName must be enclosed in quotes. DLL names have the
                   following naming convention: PDB*.DLL for standard
                   (non-SQL)databases, PDS*.DLL for SQL/ODBC databases.
 
  pServerName      Specifies the log on name for the server used to create the
                   report. For ODBC, use the data source name. This value is
                   case-sensitive. See Remarks below.
 
  [pDatabaseName]  Specifies the name for the database used to create the
                   report. See Remarks below.
 
  [pUserID]        Specifies the User ID number necessary to log on to the
                   server. See Remarks below.
 
  [pPassword]      Specifies the password necessary to log on to the server.
 
 
  Remarks
  -------
  When you pass an empty string ("") for this parameter, the program uses the
  value that's already set in the report. If you want to override a value
  that's already set in the report, use a non-empty string (for
  example, "Server A").
 
 
NOTE: For an easy way to determine what your correct LogOnServer server is, open the report in Crystal Reports, and from the main menu go to Database | Set
Location.  The location screen will show you the settings for Server Type, Server Name, Database, and User ID.
 
NOTE: For an easy way to determine the correct dllName to use, open the report in Crystal Reports, and from the main menu go to Database | Convert Database Driver.  From the Convert Database Driver window, place a checkmark in the box 'Convert Database Driver on next Refresh'.  Your dllName is listed next to From.  CLICK CANCEL WHEN CLOSING THIS WINDOW SO YOU DO NOT ACTUALLY CHANGE THE DATABASE DRIVER!
 
I connect to a password protected Microsoft Access 97 database and use the
following format.
 
     LogOnServer('pdsodbc.dll','MyDSN.dsn','','DB_Username','DB_Password');
 
NOTE: For those unfamiliar with ODBC, the DSN is setup through ODBC Data Sources in Windows Control Panel, and contains the database type, location, system database, and additional logon info.
 
You may have to experiment around a bit to get the LogOnServer funciton working for your connectivity needs.  Reference the help file Developr.hlp in the Seagate Software directory for assistance.
 
That's ALL there is to it!