Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Peter Norton's Guide to Visual Basic 6
(Publisher: Macmillan Computer Publishing)
Author(s): Peter Norton
ISBN: 0672310546
Publication Date: 09/09/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Using the Crystal Reports Custom Control

The first step in accessing Crystal Reports is to make the control available to your program. If Visual Basic is not started, start it and then create a new project. Now you must add the Crystal Reports Custom control to your Visual Basic toolbox using the Components dialog box (see Figure 14.17). To open the Components dialog box choose Project, Components. In this dialog box, you can specify which custom controls are available in your project.


Figure 14.17.  Adding the Crystal Reports Custom control to the project.


Tip:  The Components dialog box can also displayed by right-clicking the tool-box and selecting Components from the pop-up menu.

After the Crystal Reports control is available in your toolbox, you can use it in your program. To gain access to the control, simply select it from the toolbox and place it on the form where you will access the reports from. Because the Crystal Reports control is not visible at runtime, it appears only as an icon on your form. After the control is on the form, you can set the properties that access the reports you create with the report designer. Additionally, you should change the Name property to reflect your program’s use of the control. For this example, set the Name property to crReport.

Selecting the Report to Run

The key property you must specify is the ReportFileName property. This property specifies the actual report that you will run from your program. You can easily set this property using the Property Pages dialog box. Click the ellipsis button that appears to the right of the property in the Properties window as shown in Figure 14.18. From this page, you can specify the name of the report and the report’s destination.


Figure 14.18.  Specify the report filename and destination on the Custom Controls Property page.

On the General tab of the Property Pages dialog box, either type the name of the report into the field for the ReportFileName property, or select the report from a file dialog box by clicking the ellipsis button on the Property Pages dialog box. Selecting the ReportFileName is the minimum requirement for Crystal Reports to run a report. The other tabs on the Properties page enable you to customize the report by specifying the following options for the report:

Print Window Sets the properties of the Print display window
Printer Sets the number of copies to print
Print to File Sets the filename and file type when printing a report to the printer
Database Enables you to enter the user ID and connection info for a database
Selection Enables entry of the SelectionFormula and GroupSelectionFormula
Data-Bound Report Sets the heading for a database report

Modifying the Optional Properties

Although the ReportFileName is the only required property for the report, you can choose to set several optional properties for the report. The first of these properties is the SelectionFormula property. This property enables you to limit the number of records that will be included in the report. The SelectionFormula property is similar to the Where clause of an SQL statement, but it uses its own particular format to enter the information. To specify the SelectionFormula, you must specify the name of the recordset and the field to be compared. You must express this recordset/field combination in dot notation and enclose it in curly brackets. After specifying the recordset and field, you must specify the comparison operator and the value to be compared. The final result is an expression like the following:

{MemberShipList.OrgCode}=1

You also can use multiple expressions by including the And or Or operators.


Note:  For a short discussion on SQL, see Chapter 17, “Creating Queries in Visual Basic.”


Warning:  If you enter a SelectionFormula when you’re designing your report, any formula you enter in the SelectionFormula property of the Crystal Reports control provides an additional filter on the records.

The CopiesToPrinter property enables the user to specify how many copies of the report are needed. This property can be set to any integer value.

Activating the Custom Control

After you have added the Crystal Reports control to your form and set the properties, you are almost ready to start printing. The final step is to have the Visual Basic code tell Crystal Reports when it should print the report. This is done by adding a single line of code to initiate the report. The following line of code sets the Action property of the Crystal Reports control to 1, which is the value that starts the printing process:

crReport.Action = crRunReport    'This is a constant with the value of 1


Tip:  To make it easier to understand your code, you should define a constant to use in place of the number when requesting Crystal Reports to run a report.

In the project, add a command button to the default form and place the previous line of code in its Click event routine. Now run the program and click the command button to run the report. The report you specified in the Properties page is then executed and displayed in its own window (see Figure 14.19).


Figure 14.19.  Display the specified report to the screen from a Visual Basic program.

Remember that this was just a simple example of displaying a report from a program. There are many more options, properties, and methods you can access that enable you to customize how the report looks when it is displayed.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.