VB下数据报表打印输出方法有多种,一是通过自己的设计和编程完成报表打印输出。二是通过MS OFFICE实现报表打印输出。三是应用数据报表设计器的报表打印输出。本人经常使用第一种方法,它具有根据需要灵活应用的特点,但需编写程序。下附代码,第一段代码定义基本变量,第二段代码定义打印函数,第三段代码是具体打印代码,含数据转换。第四段代码是窗体加载代码,具体连接数据库和打印代码根据你所涉及的数据要求进行修改。
Option Explicit
Private n As Integer
Private m As Integer
Private zh_dm(10) As Double
Private dy_dm(3000, 10) As Variant
Private zsl As Integer
Private y As Integer
Private x As Integer
Private i As Integer
Private k As Integer
Private p As Integer
Private pa As Integer
Private j As Integer
Private txt As String
Private fnt As Integer
Private dd As Variant
Public Function prnt(x As Variant, y As Variant, fnt As Variant, txt0 As Variant)
Printer.CurrentX = x
Printer.CurrentY = y
Printer.FontSize = fnt
Printer.Print txt0
End Function
Private Sub cmdPrint_Click()
Adodc1.Recordset.MoveFirst
For i = 0 To zsl - 1
For j = 0 To 7
dy_dm(i, j) = Adodc1.Recordset(j)
Next j
Adodc1.Recordset.MoveNext
Next i
'确定页数
k = Int(zsl / 80)'80为每页行数
If k - zsl / 80 < 0 Then
k = k + 1
End If
fnt = 8
For p = 0 To k - 1
pa = p + 1
y = 300
x = 4500
txt = "机物料库存"
dd = prnt(x, y, fnt, txt)
x = 8500
txt = "第" & p + 1 & "页"
dd = prnt(x, y, fnt, txt)
y = 500
x = 600
txt = "备件代码"
dd = prnt(x, y, fnt, txt)
x = 1500
txt = "备件名称"
dd = prnt(x, y, fnt, txt)
x = 3500
txt = "备件规格"
dd = prnt(x, y, fnt, txt)
x = 5500
txt = "进口计算机号"
dd = prnt(x, y, fnt, txt)
x = 7500
txt = "最低储备量"
dd = prnt(x, y, fnt, txt)
x = 9000
txt = "库存量"
dd = prnt(x, y, fnt, txt)
For i = 0 + p * 80 To 79 + p * 80
y = 700 + 180 * (i - p * 80)
x = 600
txt = dy_dm(i, 1)
dd = prnt(x, y, fnt, txt)
x = 1500
txt = dy_dm(i, 2)
dd = prnt(x, y, fnt, txt)
x = 3500
txt = dy_dm(i, 3)
dd = prnt(x, y, fnt, txt)
x = 5500
txt = dy_dm(i, 4)
dd = prnt(x, y, fnt, txt)
x = 7500
txt = dy_dm(i, 5)
dd = prnt(x, y, fnt, txt)
x = 9000
txt = dy_dm(i, 6)
dd = prnt(x, y, fnt, txt)
If Int(i / 10) - i / 10 = 0 Then
Printer.Line (500, 695 + (i - p * 80) * 180)-(10000, 695 + (i - p * 80) * 180)
End If
Next i
If zsl - p * 80 > 80 Then
Printer.Line (500, 695 + 180 * 80)-(10000, 695 + 180 * 80)
Printer.NewPage
End If
Next p
Printer.EndDoc ' 打印完成。
Cls
End Sub
Private Sub Form_Load()
Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=jwl_dbf"
Adodc1.RecordSource = "select cs.js_dm,cs.备件代码,cs.备件名称,cs.备件规格,cs.进口计算机号,cs.最低库存量,sl.结存数量,sl.结存金额,sl.结存单价,sl.类别代码 from JWCK_BM as cs,jwl_jiec as sl" & _
" where cs.备件代码 = sl.备件代码" & " and cs.备件代码>" & "''" & " order by sl.类别代码,sl.备件代码"
Adodc1.Refresh
DataGrid1.Refresh
zsl = Adodc1.Recordset.RecordCount
Adodc3.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=jwl_dbf"
Adodc3.RecordSource = "select sum(结存金额) as hj from jwl_jiec"
Adodc3.Refresh
End Sub
以上供参考。其它请参考西安交通大学出版社出版的“VISUAL BASIC 6.0 高级编程技巧-ADO数据访问篇“