This is a walk through for changing the datasource of a VS 2008 Crystal Report at runtime.
Create a new form in VB, add the Crystal Report Viewer control, dock it. For the sake of simplicity, I've kept the code as concise as possible. The form has two public properties, the report name and the data source. We pass those in before opening the form.
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class ReportCrystal
Dim crDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument
'public variables
Public ReportName As String
Public ReportDataTable As DataTable
Private Sub ReportCrystal_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strStartupPath As String = Application.StartupPath
strStartupPath += "\reports\" & Me.ReportName
crDoc.Load(strStartupPath)
crDoc.SetDataSource(ReportDataTable)
Me.CrystalReportViewer1.ReportSource = crDoc
End Sub
End Class
So, the report is called like this. Naturally there will be parameters, but that is not covered here.
Private Sub btnPrintTickets_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintTickets.Click
Dim frm As New ReportCrystal
frm.ReportName = "PickingTickets.rpt"
'code to populate a datatable. This code uses my custom data access classes, but any data access code will do.
frm.ReportDataTable = SPs.sp_SOP10200_SEL_bySopnumbe(Me.Orders, Me.txtPalletID.Text, Me.chkOptimize.Checked, AppUser.Database).getTable
frm.ShowDialog()
End Sub