Programming | Crystal Report
Crystal Reports (Crystal report) of the industry's most professional and powerful reporting system, in addition to the powerful report function, the biggest advantage is to achieve with the vast majority of popular development tools integration and interface. In the Vs.net platform has done the report development programmer, must have the Crystal report strong, high efficiency, the integration and so on the characteristic has left the deep impression. In addition to the development of new programs, in the work we often need to contact many of the earlier software system report function upgrade requirements, if the combination of Crystal Report this powerful tool, often can be more than twice.
VB is the previous popular database development platform, with its development of C/s system in the community has a very large amount of, but the VB Super weak report function often let programmers face the customer's upgrade requirements helpless. This article does not do the use of Crystal Report and programming teaching, in fact, the Crystal report itself using methods and vs.net platform is not too much difference, I am mainly and you discuss VB and Crystal Report a more convenient interface. My development test platform is Windows2003 standard Simplified Chinese version, VB6.0+SP5 English version, Crystal 9.0 Simplified Chinese development version.
The method of making report template in Crystal Report is not the scope of this article, the reader can refer to the Help file of crystal and the technical data of the official website. To put it simply, the database engine of the Crystal Report must first manually connect the corresponding table structure, make the report template, and save it as rpt file, the operation and the use of VB from the report tools produced a report similar.
Using VB to call the Crystal Report to develop a simple interface method is to use the Crystal Report "only field definition" to get the field distribution file, using virtual files to create table fields, Using the CRAXDRT object to force the change of the data source (ado.recordset), the effect is equivalent to calling the Rpt file in VB. The following step-by-step describes the programming method.
First step:
In the VB Project menu to add "Add Crystal Report 9", the name is used by default. The Form2 (Crystal rerport automatically added form, assuming the name Form2) is automatically assigned the following code:
Option Explicit
Dim the As New Cystal1
Private Sub Form_Load ()
Screen.MousePointer = Vbhourglass ' call Crystal Report when the mouse is hourglass-like
Crviewer91.reportsource = "The assignment of the statement will be modified later
Crviewer91.viewreport
Screen.MousePointer = Vbdefault ' call Crystal Report complete after the mouse is the default shape
Step Two:
Click Database field in Crystal, select Database Expert ..., and then click Create New connection, click Field Definition only, and create a database definition file with the field name and width consistent with the original database table. Finally, in the database field to obtain the corresponding field, put it on the report, according to the requirements of the Crystal Report configuration.
Step Three:
This step is critical, adding a modual to the engineering file, defining the Global ADODB variable, and implementing the 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
Fourth Step:
About VB Program of ADO database connection notice, please see the following Print button routines.
Private Sub Command1_Click ()
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 for the program's current 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
You need to draw attention to the above code in the Report.Database.SetDataSource RS, 3, 1 is the initial use of the Crystal Report of the programmer easy to make mistakes, the use of this statement will cause the database and Crystal Report connection failed. How to dynamically invoke the Crystal Report? Please see step fifth.
Fifth Step:
To create a connection between a crystal report and a database data source, you need to modify the Form2 code above.
Option Explicit
' Dim ' As New Cystal1
' The line above cancels
Private Sub Form_Load ()
Dim oapp as New craxdrt. Application
Dim Orpt as CRAXDRT. The
Dim ReportName as String
' The top three lines are newly added
Screen.MousePointer = Vbhourglass
ReportName = "\rpt\pr1.rpt" defines the RPT file to reference
Set orpt = Oapp.openreport (App.Path & ReportName, 1)
ORpt.Database.SetDataSource rs ' connect Crystal Reports and data sources
Orpt.readrecords
Crviewer91.reportsource = Orpt ' Enable preview of Crystal Report
This paper introduces a method of using Crystal Report to develop report in VB, this method is simple and easy to use and suitable for beginners. When you are familiar with it, you can continue to learn the API functions provided by Crystal Report (Lib Library Crpe32.dll) to develop Crystal Reports, you can gain greater flexibility. Finish
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.