Manually Working with the RDC and Visual Basic

book list add book to my bookshelf create a bookmark purchase this book online

mastering crystal reports 9
Chapter 19 - Building Windows Applications with the Report Designer Component
Mastering Crystal Reports 9
by Cate McCoy and Gord Maric
Sybex 2003

So far we have been loading our Crystal report using the Report Designer. As you have seen, the Designer provides an easy way to load Crystal Reports in VB. The Designer is good if you need to modify a Crystal report from within VB. If your Crystal report is already finished and you don’t need to design it in VB, then you can avoid using the Designer and load the report via code. Another advantage is that you can choose which report to load at runtime instead of choosing the report at design time in VB. We are now going to duplicate what the Crystal Expert does for us so we can get a better understanding of the RDC components and have more flexibility in our application.

Note 

If you are going to work with Crystal Reports using ASP to develop a web page, then you will need to take this manual approach. The ASP environment does not support the Crystal Expert and Report Designer. We will cover Crystal Reports and ASP in the next chapter.

Loading a Report without the Report Designer

Before you can work with Crystal Reports, you need to make a reference to the Crystal Report libraries. Start Visual Basic and start a new standard application. From the Project menu select References and choose the Crystal Reports 9 ActiveX Designer Run Time Library. Remember that there are two Automation Server libraries: Crystal Reports 9 ActiveX Designer Runtime Library and Crystal Reports 9 ActiveX Designer Design and Runtime library. We are going to open an existing Crystal report, so we require the Crystal Report 9 ActiveX Designer Runtime Library. You would use the other library if you were going to build a report via code.

Remember back in our discussion of the RDC objects that we said the main object in the hierarchy is the Application object; however, we have not used it yet. Actually we have—Crystal Expert created the Application object automatically for us via the Report Designer. We can create this object ourselves, as shown here:

Dim crApp As New CRAXDRT.application Dim Report As Report      Set Report = _   crApp.OpenReport(App.Path & "\" & "ResortList.rpt")

The first line of code creates the Crystal Report Application object; notice that we used the New keyword to build the object. The second line of code creates a variable that will reference a Crystal report. Notice that we don’t use the New keyword this time because the OpenReport method of the Application object will create the report and pass back a reference to the report.

At this point we have a report in memory, and we can access all its methods and properties via the Report object. We could repeat the samples we previously discussed, such as changing the Field object or the sort sequence by accessing the report via the Report object variable instead of CrystalReport1 variable, which is the name of the designer. The Report Designer gave us access to the Crystal Report via CrystalReport1, but we now have access to it via the Report variable. It does not matter how you get access to the report, as long as you have are reference to the Crystal Report, you can manipulate it via code. That why all of our example above still apply. An advantage of accessing a Crystal report without the Report Designer is that you can choose which report to load at runtime. You can provide a Windows Common dialog box so your users can choose the Crystal report, as shown in Listing 19.20. If you were using the Report Designer, you would have to preload all the reports.

Listing 19.20: Choosing a Report to Open at Runtime

start example
Private Sub cmdChooseReport_Click()             Dim crApp As New CRAXDRT.Application     Dim Report As Report     Dim strRptPath As String          CommonDialog1.Filter = _       "Reports (*.rpt)|*.rpt|All files|*.*"     CommonDialog1.ShowOpen          If CommonDialog1.FileName <> "" Then         strRptPath = CommonDialog1.FileName         Set Report = crApp.OpenReport(strRptPath)         CRViewer91.ReportSource = Report         CRViewer91.ViewReport     Else         MsgBox "Select a report"     End If End Sub
end example

At this point we have a report in memory that we can manipulate via code. If your application only needs to print or export the report and does not need to display it, then you don’t need to load the Crystal Report Viewer.

Displaying the Report

If you want to display the report in the application, you need to add the Crystal Report Viewer to a VB form. From the Project menu choose Components and select Crystal Report Viewer 9. Then from the VB toolbox add the Crystal Report Viewer to the form. All you need to do now is tell the viewer which report to display.

  CRViewer91.ReportSource = Report   CRViewer91.ViewReport

When you add a Report Viewer manually to the form, you will need to manually add the viewer resize code if you want the Report Viewer to resize with your form. The Report Expert did this for us automatically:

Private Sub Form_Resize()   CRViewer91.Top = 0   CRViewer91.Left = 0   CRViewer91.Height = ScaleHeight   CRViewer91.Width = ScaleWidth End Sub

By manually controlling the components of the RDC, we are using memory efficiently in our application. The Crystal Expert provides an easy way to start with VB and Crystal, but this approach is preferable.

Use of content on this site is expressly subject to the restrictions set forth in the Membership Agreement
 
Conello © 2000-2003     Feedback


Mastering Crystal Reports 9
Mastering Crystal Reports 9
ISBN: 0782141730
EAN: 2147483647
Year: 2005
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net