VB程式列印水晶報表的典型方法1
來源:互聯網
上載者:User
程式|列印|水晶報表
描述:
本文介紹流行的一種VB程式列印水晶報表的方法,具有比較好的擴充性.
環境:
MS SQL SERVER 2000 / VB6 / CRYSTAL REPORT8.5
步驟
1: 建立ODBC串連
2: 建立一個為Crystal Report檢索資料的過程(procedure)
舉例: (通過日期查詢總額)
if exists (select * from sysobjects where name = 'usp_testfjs')
drop proc usp_testfjs
go
create proc usp_testfjs
@strDate varchar(20)
as
select sum(tot_amt) as total_amount
from trans_header
where convert(varchar(10),bus_dt,120) = @strDate
go
3. 建立使用procedure的crystal 報表
步驟和建立一般報表相同,但是注意在選擇資料來源時,把options中的Stored Procedures勾上
4. 建立VB程式
注意加入一個水晶報表控制項(crystal report control)
一段最簡單的程式:
例如:
Private Sub Command1_Click()
Dim iRet As Integer
CrystalReport1.Reset /*Reset Data*/
CrystalReport1.ReportFileName = App.Path + "\totalamount.rpt"
/*Link the Crystal Report Control with actual rpt file */
CrystalReport1.StoredProcParam(0)= Format(Trim$(DTPicker1.Value), "yyyy-mm-dd")
/*Assign the Parameter*/
CrystalReport1.WindowState = crptMaximized
CrystalReport1.WindowTitle = "HELLO"
iRet = CrystalReport1.PrintReport
/*Retrieve the Data and display the Printpreview Screen */
總結:
這個方法實現了水晶報表和VB程式的獨立性,使用者對於報表格式的改變將被局限於水晶報表的修改範圍中. 建議大家採用這種方法.