A Simple Method for programming with Crystal Reports in VB

Source: Internet
Author: User
Crystal Report is the most professional and functional Report System in the industry. In addition to powerful report functions, crystal report has achieved integration and interfaces with most popular development tools. Programmers who have worked in report development on vs. NET platform must have been impressed by the powerful, efficient, and integrated features of Crystal Reports. In addition to developing new programs, we often need to meet the needs of many early software system report function upgrades. If we can combine the powerful tool of Crystal Report, we can always get twice the result with half the effort.

VB is a popular database development platform in the past. Its C/S system has a huge social inventory, however, over-weak reporting functions of VB often make programmers unable to cope with customers' upgrade requirements. This article does not do the use of the Crystal Report and programming teaching, in fact, the use of the crystal report itself and. NET platform is not much different. I mainly discuss with you a more convenient interface method of VB and Crystal Reports. My development and testing platform is Windows 9.0 standard Simplified Chinese version, VB6.0 + SP5 English version, and crystal Simplified Chinese development version.

The method for making a report template in the crystal report is not the scope of this article. You can refer to the Help file of the Crystal Report and the technical materials on the official website. To put it simply, you must first manually connect to the corresponding table structure through the database engine of the Crystal Report, create a report template, and save it as a rpt file. This operation is similar to using the report tool that comes with VB to create a report.

To put it simply, the simple method of calling the Crystal Report for report development using VB is to use "field definition only" in the crystal report to obtain the field distribution file and create a table field using a virtual file, use the craxdrt object to forcibly change the data source (ADO. recordset). The effect is equivalent to calling the rpt file in VB. The following describes the programming method in steps.

Step 1:

Add "add crystal report 9" to the project menu of the VB project. Use the default report name. In this case, form2 (the form automatically added by crystal rerport, assuming the name is form2) is automatically assigned the following code:

Option Explicit
Dim Report as New Cystal1

Private Sub Form_Load ()

Screen. MousePointer = vbHourglass
'When you call the crystal Report, set the mouse to an hourglass-like CRViewer91.ReportSource = Report'. The assignment of this statement will be modified later.

CRViewer91.ViewReport
Screen. MousePointer = vbDefault 'Call the Crystal Report to complete the post-mouse for the default shape

End Sub

Private Sub Form_Resize ()

CRViewer91.Top = 0
CRViewer91.Left = 0
CRViewer91.Height = ScaleHeight
CRViewer91.Width = ScaleWidth

End Sub

Step 2:

Click "database Field" in the Crystal Report designer and select "database expert... ", Click" Create New Connection ", and then click" field definition only "to create a" database definition "file. The field name and width are consistent with those of the original database table. Finally, the corresponding fields are obtained in the database field, placed on the report, and configured according to the requirements of the crystal report.

Defines global ADODB variables to achieve dynamic connection between databases and Crystal Reports. The Code is as follows:

Public conn As New ADODB. Connection
Public rs As New ADODB. Recordset

Step 4:

For more information about how to connect to the ADO database of vbprogram, see the following print button routine.

Private Sub commandementclick ()

Dim connstr As String

If conn. State = adStateOpen Then conn. Close

Connstr = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = "& App. path & "/prtest. mdb; Persist Security Info = False "'prtest. mdb is the Test Access Database of the current program directory.

Conn. ConnectionString = connstr
Conn. Open
Conn. CursorLocation = adUseClient

If rs. State = adStateOpen Then rs. Close
Rs. Open "test", Conn, adopenkeyset, adlockreadonly
'Report. database. setdatasource RS, 3, 1
Form2.show 1 'after the database connection is complete, call the form2 Crystal Report Project
End sub

Note that the Report in the code above. database. setDataSource rs, 3, 1 is an easy mistake for programmers who first use Crystal Reports. Using this statement will cause connection failure between the database and Crystal Reports. How to dynamically call the crystal report? See step 4.

Step 5:

To create a connection between a crystal report and a database data source, you must modify the Form2 code above.

Option Explicit
'Dim Report as New Cystal1
'Cancel the previous line.

Private sub form_load ()

Dim oapp as new craxdrt. Application
Dim orpt as craxdrt. Report
Dim reportname as string
'The above three rows are newly added

Screen. mousepointer = vbhourglass
Reportname = "/Rpt/pr1.rpt" 'defines the rpt file to be referenced

Set orpt = oapp. openreport (App. Path & reportname, 1)

Orpt. database. setdatasource Rs 'connects to the Crystal Report and Data Source
Orpt. readrecords

Crviewer91.reportsource = orpt 'enables the preview function of the crystal report.
 
Crviewer91.viewreport
Screen. mousepointer = vbdefault

End sub

Private sub form_resize ()

Crviewer91.top = 0
Crviewer91.left = 0
CRViewer91.Height = ScaleHeight
CRViewer91.Width = ScaleWidth

End Sub

Private Sub Form_Unload (Cancel As Integer)

'Set Report = Nothing

Set rs = Nothing
Set conn = Nothing
Unload Form2
End Sub

The preceding describes a method for developing reports using Crystal Reports in VB. This method is easy to use and is suitable for beginners. After you are familiar with it, you can continue to learn the API functions provided by Crystal Report (Lib library Crpe32.dll) for the development of Crystal Reports, which gives you more flexibility.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.