Visual Basic Explorer
Visual Basic Explorer

 Source Code

 FAQ Center

 VB Tips




 VB Games

 VB News

 VB Award

 VB Forums

 Planet Source Code

 Rent a Coder






Need to hire
a VB coder?

Please support our sponsor:

 Site Map 

Using Crystal Reports

By Anne-Marie Wright

'Crystal Reports' is a popular third party package that is included with Visual Basic, which allows you to create reports for your application. The package consists of a designer - where you can design and test the reports, Crystal Reports API calls and Crystal Reports control.

Note: Crystal Reports is shipped and installed as part of the VB installation package except in VB6. This can be found in \common\tools\crysrep double click on crystl32.exe to install.

Creating a Report

VB5 or less
To access the Crystal Report Designer, select the Report Designer from the Add-In menu in the VB environment

To access the Crystal Report Designer, a Start menu group and shortcut will have been automatically created.

Lets have a quick look at the Designer

Initially there is one tab on the report - Design - when you select the print preview to see how the report looks with data in, another tab named Preview will appear.

While you are in design mode you can draw and arrange the data fields on the report. This is done in a similar way to creating controls on a form in VB.

For the purposes of this tutorial we will be creating a simple report from the sample database BIBLIO.

Start the Crystal Reports Designer
Create a new report by choosing File menu - New and the Create New Report dialog box will appear (see picture below)
Click on the Custom button to display the Choose Report Type and Data Type frames.
Select the Custom Report and click on the Data File.
Specify the location of the BIBLIO.MDB Access database
Select which tables from the database you want. For this example we will need the authors table and the Title table.
check that the joins are correct in the Linking Export

Now we are ready to draw the report

Drag and drop the fields you want on to the report. So that it looks like the report above.

Now if you run it by selecting Print Preview, you can see what the report looks like.

Now save the report and we have a template that now can be used in VB.

Note: Turn off the Save Data with report option if you want the data to contain new information each time the report is used. Going to the File menu, selecting Options and clicking on the Reporting Tab sets the option

This is fine I hear you say, but what if I don't want all the data. There are many options in the Crystal Reports and as this tutorial is more of an overview I will not be going into it. But one important thing to do is to get specific data. This is done by a selection formula (i.e. the Where clause of a SQL statement).

The above formula will only show Authors that have had their book published in 1984.

Using Crystal Reports in VB

When you have designed your report, the saved file should have an extension .rpt. You can use this report with the Crystal Report control to display or print your report in a VB application.

Note:Check the crystal report help files for the files you need to distribute with the control. You will also need any .rpt files that you have created

You will have to add the Crystal Report control to the toolbox, this is done by going to the Project menu and selecting Components, then looking down the list for the control and clicking on it

To use the control add it to a form and set the following properties

  • ReportFileName - The path and the filename of the .rpt file you have created
  • DataFiles(0) - The path and the filename of the database that you want it to use.
    If this is left blank it will use the one in the report.
  • SelectionFormula - The formula to select the subset of data that you want.
  • Destination - 0 to Preview, 1 to Print

To display the report use the .printreport method i.e CrystalReport1.PrintReport

There is alot more to crystal reports than I have covered here, but I hope it gives you an idea of how Crystal Reports works. I have not used the new features in Version 8, but it should still work.

Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and are trademarks of Exhedra Solutions, Inc.