You are here:About>Computing & Technology>Visual Basic
About.comVisual Basic
Using Crystal Reports OCX Control in VB.NET 2003
Contributed by John Ferry
 Join the Discussion
The VB.NET Message Forum
 
 Related Resources
• 
The MS Press VB .NET Learning Edition
• 
Vote in the About.COM VB.NET Poll!

I've been out of development for the last eight years, so I lost contact with Visual Basic after version 4.0. But I've had some free time recently and I decided to investigate the whole .NET "thing".

I was shocked at the price for the Visual Studio, but I bit the bullet on VB.NET Standard and started digging into it.

(Guide Note: That's why we recommend VB .NET Standard and the MS Press "Learning Edition". See the updated article: Get VB.NET (still) Cheap! The book John mentions is included with this edition.)

I was disappointed to see that the VB .NET toolbox doesn't contain the Printer component that was there in previous versions. The MS Press book placed printing right before data access and labels it an advanced topic. Printing seemed to me to rank right up there with how to put gas in your car and how to change radio station channels.

Guide Note: Right again! VB .NET has a 'different' approach for printing. Like everything in .NET, printing has gone OOP and you need to learn GDI+ to print with VB .NET. In Visual Basic .NET, the PrintDocument object replaces the Printer object. But there are real advantages that are part of the change. For example, In Visual Basic .NET, the PrintPreviewDialog object can be used to provide a standard Windows Print Preview dialog box.

After some angry thoughts and seeing the prices of current versions of printing software for .NET, I decided to try something else. The MS Press book had a very short section on installing older Active X controls that will still work. Since it was covered in Chapter three, not eighteen, I started digging.

I knew that the Crystal Reports software that came with VB 4.0 Professional had worked great for me. So I thought that would be a good control to "salvage". Here is a short primer on what I discovered and hopefully it will help with your more straightforward printing needs.

Remember, all this is done using Crystal Reports engine version 3.0.1.12 that was bundled with VB 4.0 Professional.

Adding the Design Tool

The first thing to do is add the Crystal Reports tool to the Visual Studio IDE so we can create the report. Click External Tools from the Tools menu as shown below. Notice that Create GUID (Globally Unique ID) is already implemented as an External Tool. You'll see it again in the next dialog window.

Then click on the Add button seen below.

Adding Crystal Reports

You can now add the Crystal Report tool by locating the executable file for it. For my version of Crystal, it was CRW32.exe.

Locate Crystal Reports EXE

Click OK and you will then see that Crystal Reports has been added to your Tools menu.

After Crystal Reports Added

Before we actually use the tool in Visual Studio .NET, please note these things.

-- I had to install the full VB product that the Crystal Reports engine came with. I believe this was necessary because the DB Engine components are linked with VB in Version 4.0 Professional. When I installed only the Crystal Report component, I could not "access" my database.

-- Although you can use Access 2000 to create a database to use with the older version of Crystal Reports I have, I had to convert the database to an earlier version using the Convert Database utility in Access 2000. I believe this is necessary because my version of Crystal Reports, Version 3.0.1.32, won't work with a version of Access more recent than Access 97.

-- I have never found it necessary to restore the database to a more current version of Access. In fact, to take advantage of my version of Crystal, it's more convenient to leave the database in the older format. If I wanted to work with it in a more recent version of Access, it would be necessary to convert the database to a newer format, make the changes, and then convert to the prior version again.

The good news is that I found that even software as old as my Crystal Reports can still be used in Visual Studio .NET. To prove it, I created a simple database to show how it all comes together.

Creating the Database

I assume that everyone can create a database in Access 2000 and knows how to convert it to the prior version using the utility. The database I used has these properties.

Table name: NamAdr
Field name: Name 
            Text
            Length 20
            (Primary key)
Field name: Address
            Text
            Length 50

Save the database using the name adrs2000. Convert the database to the prior version and name it adrs.

Creating the VB.NET Application

Create a new Project in Visual Studio .NET. Go to the Tools menu and start the Crystal Reports tool that was added previously.

To use the Crystal editor, I made a new report using the Access database created earlier and saved the new report as report.rpt.

The Crystal Reports Layout

Now the Crystal Report Control must be added to the Toolbox. To customize the Toolbox in Visual Studio .NET 2003, right click inside the Toolbox and select Add/Remove Items. (This is called Customize Toolbox in the first release of VB .NET.) The Crystal Report Control can be selected from the COM components list as shown below.

Crystal COM Component

The result is the control in its own tab in the Toolbox as shown.

Crystal Tool in Toolbox

Now we're finally ready to put it all together.

Add the following controls to your form as shown below. I'll assume that you can do the database work to connect the Access database we created previously and bind it to the grid shown.

The Update list button issues the Update method to the OLE database adapter that is connected to the Access database we created.

The Print list button calls the crystal report Action method to run the report. Note the Crystal Reports tool added to the form in the lower right.

Completed Project Form

The only other task necessary to complete th project is to tell Crystal Reports what the name of the report is and where it is located. I did this by updating the properties. This could also be done with programming code.

You place the report name in the ReportFileName property. Remember our report name was report.rpt.

Properties

This code snippet shows all that you need to get Crystal Reports to run the report. This is the Click event for the Print list button.

Private Sub btnPrint_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnPrint.Click
      'print the list
      AxCrystalReport1.Action = 1
End Sub

Save the project, build the solution and we're ready to see it as work.

Running the Project

To run the project, add a couple of names as shown below. Make sure you click the update button to update the changes back to the database. Here is the code to do that. DataSet11 is the generated dataset.

Private Sub btnUpdate_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnUpdate.Click
        'Update the names to the table
        OleDbDataAdapter1.Update(DataSet11)
 End Sub

It can't get much easier than this.

Completed Application

Now click the Print list button and Viola! You have your report with data in preview mode. Not bad for a control that came with VB 4.0. It sure beats paying several hundred dollars for a version of Crystal Reports just so you can print a simple list.

Enjoy!

Guide Note: To show my appreciation for John's contribution to About Visual Basic, I'm sending him a free copy of Visual Basic Design Patterns by James Cooper. You can read the About Visual Basic review of this book here.

From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Newsletters & RSSEmail to a friendSubmit to Digg
 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.