威尔士亲王cos:用VB 5.0从Recordset中打印列表

来源:百度文库 编辑:中财网 时间:2024/04/27 20:49:51

用VB 5.0从Recordset中打印列表

发布时间:2008-04-03 10:37:15  来源:编程爱好者网站  作者:编程爱好者网站  点击:209

   Visual Basic 所 附 带 的 报 表 生 成 器-Crystal Reports, 功 能 强 大, 能 完 成 大 部 分 报 表 的 制 作。 但 在 某 些 情 况 下, 用Crystal Reports 却 很 难 作 出 报 表 来。 例 如, 根 据 用 户 输 入 不 同 的 过 滤(Filter) 条 件, 将 产 生 不 同 的 虚 拟 表, 此 时 用Crystal Reports 制 作 报 表 就 勉 为 其 难 了, 在 这 种 情 况 下, 可 使 用VB 提 供 的Printer 对 象 来 予 以 解 决。

---- 下 面 是 本 人 在 给 单 位 开 发 一 个 产 品 销 售 情 况 统 计 分 析 软 件 的 过 程 中, 使 用Printer 对 象 从Recordset 对 象 的 虚 拟 表 中 打 印 数 据 的 通 用 代 码:

Sub PrintRecordset(recRecordset as Recordset)
Dim LeftMargin As Integer
Dim HeadTopPosition As Integer
Dim FieldNum As Integer
Dim PageCounter As Integer
Dim MyRecordset As Recordset
Const FooterTopPosition=24

Set MyRecordset=recRecordset
PageCounter = 1
’ 设 置Printer 对 象 坐 标 的 度 量 单 位 为 厘 米
Printer.ScaleMode = vbCentimeters

LeftMargin = 1.5
HeadTopPosition = 2

---- ’ 定 义 打 印 页 左 上 角 的X 坐 标 和Y 坐 标, 通 过 改 变ScaleLeft 和ScaleTop 的 值, 可 改 变 打 印 页 的 左 边 距 和 上 边 距

Printer.ScaleLeft = -LeftMargin
Printer.ScaleTop = -HeadTopPosition

Printer.Font.Name = "Times New Roman"
Printer.Font.Size = 12

Printer.Print "Lovesoft Corp."
Printer.Print ""

If MyRecordset.EOF And MyRecordset.BOF Then
MsgBox "No Record At Presend!",
vbCritical + vbOKOnly, "Print Error"
Exit Sub
End If
MyRecordset.MoveFirst

Do Until Printer.CurrentY > FooterTopPosition

’Print the fields of the recordset in sequence
For FieldNum = 0 To MyRecordset.Fields.Count - 1
Printer.Print MyRecordset.Fields
(FieldNum).Name & _
": " & _
MyRecordset.Fields(FieldNum).Value
If Printer.CurrentY > FooterTopPosition Then
Printer.CurrentX = 8
Printer.Print "Page: " & PageCounter
’ 创 建 多 页 文 档
Printer.NewPage
PageCounter = PageCounter + 1
End If
Next FieldNum

MyRecordset.MoveNext
If MyRecordset.EOF Then Exit Do
’ 在 记 录 之 间 空 一 行
Printer.Print ""
Loop

’Print the Page number as a footer
Printer.CurrentX = 8
Printer.CurrentY = FooterTopPosition
Printer.Print "Page: " & PageCounter
’ 将 输 出 送 到 打 印 机
Printer.EndDoc
End Sub

---- 调 用 上 述PrintRecordset 通 用 过 程 相 当 方 便, 下 面 是 通 过cmdPrint 按 钮 的Click 事 件 进 行 调 用 的 一 个 实 例:

Private Sub cmdPrint_Click()
PrintRecordset Data1.Recordset
End Sub