A simple programming method for using Crystal Reports in VB

Source: Internet
Author: User

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 calling the crystal report, set the mouse to an hourglass.

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.

Step 3:
This step is critical. Add a Modual to the project file and define the global ADODB variable to achieve dynamic connection between the database and the crystal report. 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 this row is canceled

After the Form2.Show 1 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 5.

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 above line

Private Sub Form_Load ()

Dim oApp As New CRAXDRT. Application
Dim oRpt As CRAXDRT. Report
Dim reportName As String
The preceding three rows are newly added.

Screen. MousePointer = vbHourglass


ReportName = "ptPr1.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 enable 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.


Related Article

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.