在VB6中使用水晶報表並動態設置數據源
首先引用Crystal Reports 9 ActiveX Designer Run Time Library再添加Crystal Repotr VIEw Control 9控件在工程資源管理器中右鍵--添加--Crystal Reports 9 新建一個Crystal Reports9報表設置數據源後將要顯示的字段加入報表內。
窗體代碼部分:
Dim m_Report As New CrystalReport1 '(這個CrystalReport1是你建立的報表的名稱)
Dim m_Connection As ADODB.Connection '(建立一個ADO連接)
Dim adoRS As ADODB.Recordset
Private Sub Form_Load()
Dim SQL As String
Dim strConnect As String
' Create and bind the ADO Recordset object
Set m_Connection = New ADODB.Connection
Set adoRS = New ADODB.Recordset
' Open the connection
strConnect = "你的數據庫連接字符串"
m_Connection.Open strConnect
SQL = "查詢語句"
adoRS.Open SQL, m_Connection, adOpenDynamic, adLockBatchOptimistic
m_Report.Database.SetDataSource adoRS'(動態設置報表數據源)
Screen.MousePointer = vbHourglass
CRVIEwer1.ReportSource = m_Report
CRViewer1.VIEwReport '(顯示報表)
Screen.MousePointer = vbDefault
End Sub
顯示打印機設置動話框
報表名.PrinterSetup Me.hWnd
用代碼設置紙張大小
報表名.PaperSize =crPaperA4
是否顯示右上角水晶報表的圖標
CRViewer1.EnableAnimationCtrl = Not CRVIEwer1.EnableAnimationCtrl
是否顯示導出報表按鈕
CRViewer1.EnableExportButton = Not CRVIEwer1.EnableExportButton
是否顯示關閉按鈕
CRViewer1.EnableCloseButton = Not CRVIEwer1.EnableCloseButton
是否顯示左側樹列表
CRViewer1.EnableGroupTree = Not CRVIEwer1.EnableGroupTree
是否顯示導航
CRViewer1.EnableNavigationControls = Not CRVIEwer1.EnableNavigationControls
是否顯示彈出菜單
CRViewer1.EnablePopupMenu = Not CRVIEwer1.EnablePopupMenu
是否顯示打印按鈕
CRViewer1.EnablePrintButton = Not CRVIEwer1.EnablePrintButton
是否顯示刷新按鈕
CRViewer1.EnableRefreshButton = Not CRVIEwer1.EnableRefreshButton
是否顯示工具欄
CRViewer1.EnableToolbar = Not CRVIEwer1.EnableToolbar
是否顯示比例調整窗口
CRViewer1.EnableZoomControl = Not CRVIEwer1.EnableZoomControl
報表顯示的樣式
CRViewer1.DisplayBackgroundEdge = Not CRVIEwer1.DisplayBackgroundEdge
報表控件是否有邊框
CRViewer1.DisplayBorder = Not CRVIEwer1.DisplayBorder
是否顯示報表的選項卡
CRViewer1.DisplayTabs = Not CRVIEwer1.DisplayTabs
vb中調用水晶報表生成的*.rpt文件
CrystalReport1.ReportFileName = App.Path & "liuhan.rpt"
CrystalReport1.WindowTitle = "打印預覽"
CrystalReport1.Windowstate = crptMaximized '使打印預覽窗口最大化********
CrystalReport1.Destination = crptToWindow '到窗口***********************
'CrystalReport1.Destination = crptToPrinter '到打印機********************
CrystalReport1.PrinterDriver = 1
CrystalReport1.PrintReport
解決水晶報表更新數據後需按刷新問題報表名.DiscardSavedData