资源下载地址: 点击下载
一、问题描述:
工作中经常遇到打印的问题,但很多时候复杂的打印需要编程来实现,本文介绍一下将数据库记录读取到数组,再进行打印。
二、程序实现原理:
(1)定义一个数组和编写一个打印函数
(2)按记录条数进行循环,再对每一条记录按字段数目进行循环,将后台Access记读取到数组中
三:实验数据:
表记录内容如下:
字段定义如下:
四、程序实现
1、数组定义
Option Explicit Private dy_dm(3000,10) As Variant
2、打印函数编写
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
3、VB实现:
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 = "2013年缴费明细" dd = prnt(x,y,fnt,txt) x = 8500 txt = "第" & p + 1 & "页" dd = prnt(x,txt) y = 500 x = 600 txt = "乡镇" dd = prnt(x,txt) x = 1500 txt = "集体" dd = prnt(x,txt) x = 2500 txt = "姓名" dd = prnt(x,txt) x = 3500 txt = "身份证号码" dd = prnt(x,txt) x = 5500 txt = "性别" dd = prnt(x,txt) x = 6000 txt = "缴费年份" dd = prnt(x,txt) x = 7000 txt = "缴费金额" dd = prnt(x,txt) x = 8500 txt = "代扣银行账号" dd = prnt(x,txt) For i = 0 + p * 80 To 79 + p * 80 y = 700 + 180 * (i - p * 80) x = 600 txt = dy_dm(i,0) dd = prnt(x,txt) x = 1500 txt = dy_dm(i,1) dd = prnt(x,txt) x = 2500 txt = dy_dm(i,2) dd = prnt(x,txt) x = 3500 txt = dy_dm(i,3) dd = prnt(x,txt) x = 5500 txt = dy_dm(i,4) dd = prnt(x,txt) x = 6000 txt = dy_dm(i,5) dd = prnt(x,txt) x = 7000 txt = dy_dm(i,6) dd = prnt(x,txt) x = 8000 txt = dy_dm(i,7) dd = prnt(x,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
五、程序总结
程序只能打印记录,所以代码实现中先利用打印函数prnt进行了字段的打印,再按字段的位置设置打印相应的记录内容。
六、程序优点之处
每10第记录一组,完了之后会打印一条线,每打印80条记录将会另起一页
七、程序待改进之处
1、假如只想让一页打印一条记录该怎么实现?
2、是否可以设置分散打印?
3、打印之后如避免再次打印已经打印过的记录?
5、能否提供提示总记录数和已经打印数目和未打印数目?
原文链接:https://www.f2er.com/vb/258502.html