I've been using Crystal Report since VB6 days, the earliest version I handled was Seagate Crystal Report 7.0. Microsoft included the OEM version of Business Objects Crystal Reports on Microsoft Visual Studio as a general purpose reporting tool. Crystal Reports became the de facto report writer when Microsoft released it with Visual Basic. Today, you will learn How to call Crystal Report on your application. This article includes a VB.NET project that you can customize for your own application.The step-by-step procedure to call Crystal Report
1. Add new form and named it as frmPrinter. This will serve as your Crystal Report Printer module.
2. Add references to CrystalReport.CrystalReport.Engine and CrystalDecisions.Shared
3. Add Crystal Report Viewer on the newly created form. Make sure that the Dock property is set to "Fill"

4. Switch to "View Code" and include the following namespaces required for this project.
Imports CrystalDecisions
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.CrystalReports
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.Shared
5. Inside the form class, declare private variables that will hold as crystal report parameters
Private _Parameter1 As String
6. Modify the form's constructor (new) event to include passing of parameters as shown below.
Public Sub New(ByVal prParameter As String)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_Parameter1 = prParameter
End Sub
7. Add the "SetConnectionInfo" function which will create the Login connection to your database.
Public Shared Sub SetConnectionInfo(ByVal RptDoc As Engine.ReportDocument, ByVal CnnInfo As [Shared].TableLogOnInfo)
For Each TableReport As CrystalDecisions.CrystalReports.Engine.Table In RptDoc.Database.Tables
TableReport.ApplyLogOnInfo(CnnInfo)
Next
For Each SectionReport As CrystalDecisions.CrystalReports.Engine.Section In RptDoc.ReportDefinition.Sections
For Each RptObj As CrystalDecisions.CrystalReports.Engine.ReportObject In SectionReport.ReportObjects
If RptObj.Kind = ReportObjectKind.SubreportObject Then
Dim SubRptObj As CrystalReports.Engine.SubreportObject = RptObj
Dim SubRpt As CrystalDecisions.CrystalReports.Engine.ReportDocument = SubRptObj.OpenSubreport(SubRptObj.SubreportName)
For Each tbl As CrystalDecisions.CrystalReports.Engine.Table In SubRpt.Database.Tables
tbl.ApplyLogOnInfo(CnnInfo)
Next
End If
Next
Next
End Sub
8. On your the "Shown" event of the frmPrinter, Paste the following codes.
Private Sub frmPrinter_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
'Prepare Database Login Info
Dim LogInfo As New TableLogOnInfo()
LogInfo.ConnectionInfo.ServerName = "mnlho8ap33"
LogInfo.ConnectionInfo.DatabaseName = "BS"
LogInfo.ConnectionInfo.UserID = "sa"
LogInfo.ConnectionInfo.Password = "12345678"
Dim oReport As New ReportDocument
oReport.Load("rptSample.rpt")
'Connect to database
SetConnectionInfo(oReport, LogInfo)
'Set Parameters
oReport.SetParameterValue("Parameter1", _Parameter1)
'Open preview
Me.WindowState = FormWindowState.Maximized
CR1.ReportSource = oReport
End Sub
9. Lastly on the form caller, put the code that will call the form printer.
Dim frm As New frmPrinter(txtInvoiceNumber.Text)
frm.MdiParent = Me.ParentForm
frm.Show()
Now Try to run the project, the result should display like the screenshot below.

To customize and make your own Crystal Report printing, download the sample project here.
For more Crystal Reports tips and tricks, subscribe now
3 comments:
Printing Barcodes with Crystal Reports. Examples in .Net C#
what do i put on the textbox in form 1?
ive also downloaded the file. does it include the database?
how do i make this work on my computer? does this include a database?
Post a Comment