再添加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
此代碼在VB6+SP5+Windows2000 Server下調試通過
以上代碼參考水晶報表9中文版