当前位置: 源码素材网 » VB教程 » 详情页

Vb读取access内容并分页打印输出

  •   时间:2018-12-03
  • 概述:分页打印

Vb分页打印例子,数据读取自Access,本程序亮点是当打印第二页或其它任意非第一页的时候,会自动打印表头,这样会打印出一个完整的数据表格,具体实现请参阅以下代码:

VERSION 5.00
Object = "{67397AA1-7FB1-11D0-B148-00A0C922E820}#6.0#0"; "MSADODC.OCX"
Object = "{CDE57A40-8B86-11D0-B3C6-00A0C90AEA82}#1.0#0"; "MSDATGRD.OCX"
Begin VB.Form Form1
   Caption         =   "分页打印"
   ClientHeight    =   4230
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6870
   LinkTopic       =   "Form1"
   ScaleHeight     =   4230
   ScaleWidth      =   6870
   StartUpPosition =   3  '窗口缺省
   Begin VB.TextBox Text2
      Height          =   330
      Left            =   5160
      TabIndex        =   6
      Text            =   "80"
      Top             =   105
      Width           =   435
   End
   Begin VB.TextBox Text1
      Height          =   360
      Left            =   720
      TabIndex        =   3
      Top             =   105
      Width           =   2610
   End
   Begin VB.CommandButton cmdPrint
      Caption         =   "打印"
      Height          =   390
      Left            =   5895
      TabIndex        =   2
      Top             =   75
      Width           =   870
   End
   Begin VB.CommandButton Command1
      Caption         =   "查询"
      Height          =   390
      Left            =   3390
      TabIndex        =   1
      Top             =   75
      Width           =   870
   End
   Begin MSAdodcLib.Adodc Adodc1
      Height          =   330
      Left            =   870
      Top             =   4965
      Width           =   1515
      _ExtentX        =   2672
      _ExtentY        =   582
      ConnectMode     =   0
      CursorLocation  =   3
      IsolationLevel  =   -1
      ConnectionTimeout=   15
      CommandTimeout  =   30
      CursorType      =   3
      LockType        =   3
      CommandType     =   2
      CursorOptions   =   0
      CacheSize       =   50
      MaxRecords      =   0
      BOFAction       =   0
      EOFAction       =   0
      ConnectStringType=   1
      Appearance      =   1
      BackColor       =   -2147483643
      ForeColor       =   -2147483640
      Orientation     =   0
      Enabled         =   -1
      Connect         =   "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db_books.mdb;Persist Security Info=False"
      OLEDBString     =   "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db_books.mdb;Persist Security Info=False"
      OLEDBFile       =   ""
      DataSourceName  =   ""
      OtherAttributes =   ""
      UserName        =   ""
      Password        =   ""
      RecordSource    =   "图书信息表"
      Caption         =   "Adodc1"
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}
         Name            =   "宋体"
         Size            =   9
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      _Version        =   393216
   End
   Begin MSDataGridLib.DataGrid DataGrid1
      Bindings        =   "Form1.frx":0000
      Height          =   3555
      Left            =   30
      TabIndex        =   0
      Top             =   600
      Width           =   6780
      _ExtentX        =   11959
      _ExtentY        =   6271
      _Version        =   393216
      HeadLines       =   1
      RowHeight       =   15
      FormatLocked    =   -1  'True
      BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851}
         Name            =   "宋体"
         Size            =   9
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}
         Name            =   "宋体"
         Size            =   9
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ColumnCount     =   7
      BeginProperty Column00
         DataField       =   "条形码"
         Caption         =   "条形码"
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED}
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
      EndProperty
      BeginProperty Column01
         DataField       =   "书名"
         Caption         =   "书名"
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED}
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
      EndProperty
      BeginProperty Column02
         DataField       =   "作者"
         Caption         =   "作者"
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED}
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
      EndProperty
      BeginProperty Column03
         DataField       =   "出版社"
         Caption         =   "出版社"
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED}
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
      EndProperty
      BeginProperty Column04
         DataField       =   "数量"
         Caption         =   "数量"
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED}
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
      EndProperty
      BeginProperty Column05
         DataField       =   "单价"
         Caption         =   "单价"
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED}
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
      EndProperty
      BeginProperty Column06
         DataField       =   "金额"
         Caption         =   "金额"
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED}
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
      EndProperty
      SplitCount      =   1
      BeginProperty Split0
         BeginProperty Column00
            ColumnWidth     =   1500.095
         EndProperty
         BeginProperty Column01
            ColumnWidth     =   2505.26
         EndProperty
         BeginProperty Column02
            ColumnWidth     =   1005.165
         EndProperty
         BeginProperty Column03
            ColumnWidth     =   1500.095
         EndProperty
         BeginProperty Column04
            ColumnWidth     =   494.929
         EndProperty
         BeginProperty Column05
            ColumnWidth     =   494.929
         EndProperty
         BeginProperty Column06
            ColumnWidth     =   494.929
         EndProperty
      EndProperty
   End
   Begin VB.Label Label2
      Caption         =   "每页打印       行"
      Height          =   255
      Left            =   4290
      TabIndex        =   5
      Top             =   180
      Width           =   1620
   End
   Begin VB.Label Label1
      Caption         =   "出版社:"
      Height          =   240
      Left            =   60
      TabIndex        =   4
      Top             =   180
      Width           =   750
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Command1_Click()
 Adodc1.RecordSource = " 图书信息表 where 出版社 like+ '" + Text1.Text + "'+'%'"
' "select * from tb_kc  where 商品编号 like+ '" + Text1.Text + "'+'%'"
 Adodc1.Refresh
End Sub
Private Sub Form_Load()
  '自动识别数据库路径
 Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db_books.MDB;Persist Security Info=False"
End Sub
Private Sub cmdPrint_Click()
    Dim i As Integer, Row As Integer, Rows As Integer
    Dim BColWidth As Integer, ColWidth(4) As Integer, ColPosition(4) As Integer
    Adodc1.RecordSource = " 图书信息表 where 出版社 like+ '" + Text1.Text + "'+'%'"
    Adodc1.Refresh
    With Adodc1.Recordset
        If .RecordCount = 0 Then
            MsgBox "没有要打印的数据!", vbOKCancel, "提示"
            Exit Sub
        End If
        Printer.ScaleMode = 3
        '根据字宽设置基本列宽
        BColWidth = Int(Printer.TextWidth("宽")) + 1
        '根据纸张大小和字高设置每页打印行数
        '        Rows = Printer.ScaleHeight \ (Printer.TextHeight("高") + 1) - 4
        Rows = Text2.Text
        '设置每列的宽度
        ColWidth(0) = 8 * BColWidth
        ColWidth(1) = 25 * BColWidth
        ColWidth(2) = 5 * BColWidth
        ColWidth(3) = 10 * BColWidth
        ColWidth(4) = 8 * BColWidth
        '定位要打印的数据
        ColPosition(0) = 260
        ColPosition(1) = ColPosition(0) + ColWidth(0)
        ColPosition(2) = ColPosition(1) + ColWidth(1)
        ColPosition(3) = ColPosition(2) + ColWidth(2)
        ColPosition(4) = ColPosition(3) + ColWidth(3)
        '打印表头
        Printer.FontName = "黑体"
        Printer.FontSize = "10"
        Printer.Print
        Printer.Print
        Printer.CurrentX = ColPosition(0)
        Printer.Print "条形码";
        Printer.CurrentX = ColPosition(1)
        Printer.Print "书名";
        Printer.CurrentX = ColPosition(2)
        Printer.Print "作者";
        Printer.CurrentX = ColPosition(3)
        Printer.Print "出版社";
        Printer.CurrentX = ColPosition(4)
        Printer.Print "单价"
        '打印数据
        .MoveFirst
        Row = 0
        Do While Not .EOF
            Printer.FontName = "宋体"
            Printer.FontSize = "10"
            Printer.CurrentX = ColPosition(0)
            Printer.Print .Fields("条形码");
            Printer.CurrentX = ColPosition(1)
            Printer.Print .Fields("书名");
            Printer.CurrentX = ColPosition(2)
            Printer.Print .Fields("作者");
            Printer.CurrentX = ColPosition(3)
            Printer.Print .Fields("出版社");
            Printer.CurrentX = ColPosition(4)
            Printer.Print Format(.Fields("单价"), "##,##0.00")
            Row = Row + 1
            '换页重复打印表头
            If Row = Rows Then
                '打印页码
                Printer.Print "第" & Printer.Page & "页"
                Row = 0
                Printer.NewPage
                Printer.Print
                Printer.FontName = "黑体"
                Printer.FontSize = "10"
                Printer.Print
                Printer.CurrentX = ColPosition(0)
                Printer.Print "条形码";
                Printer.CurrentX = ColPosition(1)
                Printer.Print "书名";
                Printer.CurrentX = ColPosition(2)
                Printer.Print "作者";
                Printer.CurrentX = ColPosition(3)
                Printer.Print "出版社";
                Printer.CurrentX = ColPosition(4)
                Printer.Print "单价"
            End If
            .MoveNext
            '打印最后一页的页码
            If .EOF = True Then
                Printer.Print "第" & Printer.Page & "页"
            End If
        Loop
        Printer.EndDoc
    End With
End Sub

 完整带数据库的可编译源码,在这里下载:分页打印-VB重复打印表头数据

    相关声明:

      若“Vb读取access内容并分页打印输出”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。