Sep
22
Written by:
Steve Gray
9/22/2010 12:01 PM
In order to call Crystal Reports in VB.NET, create a form and drop a CrystalReportViewer control onto the form. Copy this code into the form:
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class CrystalReport
Public ReportName As String
Public DataSource As DataTable
Dim crDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument
Private Sub CrystalReport_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
'before opening this form, the calling form must set the REPORTNAME
‘and DATASOURCE properties
Try
If Me.ReportName = "" Then
Exit Sub
End If
'load the report into Crystal
Dim rpt As String
rpt = "\\shareName\folderName\" & Me.ReportName
crDoc.Load(rpt)
'set the datasource
crDoc.SetDataSource(Me.DataSource)
'load the report into the report viewer
CrystalReportViewer1.ReportSource = crDoc
Catch ex As Exception
'call the global error handler
globalErrorHandler(ex)
End Try
End Sub
End Class
Before opening the form we need to set the ‘ReportName’ property of the form with the report name, and the ‘DataSource’ property with a datatable.
As always, I welcome your comments!