班主任,我这个下标越界,能帮我看看吗?
Private Sub Command1_Click()Dim i, A As Integer
Dim v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14 As Integer
v1 = 1000
v2 = v1 + MSFlexGrid1.ColWidth(0) * 1.1 '设备编号
v3 = v2 + MSFlexGrid1.ColWidth(1) * 1.1 '单位名称
v4 = v3 + MSFlexGrid1.ColWidth(2) * 0.8 '设备类型
v5 = v4 + MSFlexGrid1.ColWidth(3) * 0.8 '设备名称
v6 = v5 + MSFlexGrid1.ColWidth(4) * 0.8 '设备品牌
v7 = v6 + MSFlexGrid1.ColWidth(5) * 0.8 '设备型号
v8 = v7 + MSFlexGrid1.ColWidth(6) * 1.2 '设备序列号
v9 = v8 + MSFlexGrid1.ColWidth(7) * 1 '购入时间
v10 = v9 + MSFlexGrid1.ColWidth(8) * 1 '使用时间
v11 = v10 + MSFlexGrid1.ColWidth(9) * 0.7 '使用部门
v12 = v11 + MSFlexGrid1.ColWidth(10) * 0.8 '设备原值
v13 = v12 + MSFlexGrid1.ColWidth(11) * 1 '备注
v14 = 11500
Dim m As Integer '总记录数
Dim N As Integer '每页记录数
Dim ii As Integer '总页数
Dim j As Integer '页码数
Dim k As Integer '每页起始记录控制
Dim TmpLowerPageMoney As Single '每页合计金额
m = Int(MSFlexGrid1.Rows - 1) '(TxtWhm_2 入库品种数)----总记录数
N = 23 '每页 23 条记录
If Int(m / N) - m / N = 0 Then
ii = m / N
Else
ii = Int(m / N) + 1
End If
Printer.Height = 750 + 300 + 400 * N + 900 + 1000 '定义纸张高度----根据每页记录数
Printer.Width = 2000 '定义纸张宽度
For j = 1 To ii '外循环控制打印页数
' TmpLowerPageMoney = 0# '每页合计金额 ------ 新页开始清零
Printer.CurrentX = 5000
Printer.CurrentY = 200
Printer.FontSize = 16
Printer.Print "长沙市邮政局固定资产清理表(设备)"
Printer.CurrentX = 11000 '页码置于左上角
Printer.CurrentY = 750
Printer.FontSize = 10
Printer.Print "第 " + Trim(Str(j)) + " 页 共 " & Trim(Str(ii)) + " 页"
Printer.CurrentX = 1000
Printer.CurrentY = 750
Printer.FontSize = 10
Printer.Print "单位名称:" & TxtWhm5.Text
'列标题
'A -------- 表主体纵向高度
A = 1050
Printer.Line (v1, A)-(v13, A) '画顶横线
If MSFlexGrid1.ColWidth(0) > 0 Then '第1列 设备编号
Printer.Line (v1, A)-(v1, A + 450) '画左坚边线
Printer.CurrentX = v1 + 10
Printer.CurrentY = A + 100
Printer.FontSize = 10
Printer.Print "设备编号"
Printer.Line (v1, A + 450)-(v13, A + 450)
End If
If MSFlexGrid1.ColWidth(1) > 0 Then '第2列 单位名称
Printer.Line (v2, A)-(v2, A + 450)
Printer.CurrentX = v2 + 10
Printer.CurrentY = A + 100
Printer.FontSize = 10
Printer.Print "单位名称"
End If
If MSFlexGrid1.ColWidth(2) > 0 Then '第3列 设备类型
Printer.Line (v3, A)-(v3, A + 450)
Printer.CurrentX = v3 + 40
Printer.CurrentY = A + 100
Printer.FontSize = 10
Printer.Print "设备类型"
End If
If MSFlexGrid1.ColWidth(3) > 0 Then '第4列 设备名称
Printer.Line (v4, A)-(v4, A + 450)
Printer.CurrentX = v4 + 40
Printer.CurrentY = A + 100
Printer.FontSize = 10
Printer.Print "设备名称"
End If
If MSFlexGrid1.ColWidth(4) > 0 Then '第5列 设备品牌
Printer.Line (v5, A)-(v5, A + 450)
Printer.CurrentX = v5 + 40
Printer.CurrentY = A + 100
Printer.FontSize = 10
Printer.Print "设备品牌"
End If
If MSFlexGrid1.ColWidth(5) > 0 Then '第6列 设备型号
Printer.Line (v6, A)-(v6, A + 450)
Printer.CurrentX = v6 + 40
Printer.CurrentY = A + 100
Printer.FontSize = 10
Printer.Print "设备型号"
End If
If MSFlexGrid1.ColWidth(6) > 0 Then '第7列 设备序列号
Printer.Line (v7, A)-(v7, A + 450)
Printer.CurrentX = v7 + 40
Printer.CurrentY = A + 100
Printer.FontSize = 10
Printer.Print "设备序列号"
End If
If MSFlexGrid1.ColWidth(7) > 0 Then '第8列 设备购入时间
Printer.Line (v8, A)-(v8, A + 450)
Printer.CurrentX = v8 + 20
Printer.CurrentY = A + 100
Printer.FontSize = 10
Printer.Print "设备购入时间"
End If
If MSFlexGrid1.ColWidth(8) > 0 Then '第9列 设备使用时间
Printer.Line (v9, A)-(v9, A + 450)
Printer.CurrentX = v9 + 20
Printer.CurrentY = A + 100
Printer.FontSize = 10
Printer.Print "设备使用时间"
End If
If MSFlexGrid1.ColWidth(9) > 0 Then '第10列 使用部门
Printer.Line (v10, A)-(v10, A + 450)
Printer.CurrentX = v10 + 20
Printer.CurrentY = A + 100
Printer.FontSize = 10
Printer.Print "使用部门"
End If
If MSFlexGrid1.ColWidth(10) > 0 Then '第11列 设备原值(元)
Printer.Line (v11, A)-(v11, A + 450)
Printer.CurrentX = v11 + 20
Printer.CurrentY = A + 100
Printer.FontSize = 10
Printer.Print "设备值(元)"
' Printer.Line (v12 + 10, A)-(v12 + 10, A + 450) '画右坚边线(封口)
End If
If MSFlexGrid1.ColWidth(11) > 0 Then '第12列 设备原值(元)
Printer.Line (v12, A)-(v12, A + 450)
Printer.CurrentX = v12 + 20
Printer.CurrentY = A + 100
Printer.FontSize = 10
Printer.Print "备注"
Printer.Line (v13 + 10, A)-(v13 + 10, A + 450) '画右坚边线(封口)
End If
A = 1500 'A -------- 表主体纵向高度
Printer.Line (v1, A)-(v14, A) '画横线
If j = 1 Then 'k 每页起始记录控制
k = 1 'k=0 表示第1行,k=1 表示第2行
Else
k = (j - 1) * N + 1
End If
For i = k To j * N 'm - (ii - j) * N + 1 '
'内循环控制每页打印-------------分页打印
If MSFlexGrid1.ColWidth(0) > 0 Then '第1列 设备编号
Printer.Line (v1, A)-(v1, A + 400) '画左坚边线
Printer.CurrentX = 1000
Printer.CurrentY = A + 100
Printer.FontSize = 10
If MSFlexGrid1.TextMatrix(i, 0) <> "" Then 'i=0 时为第1行,标题行 ******提示在这里
Printer.Print MSFlexGrid1.TextMatrix(i, 0)
End If
Printer.Line (v1, A + 400)-(v13, A + 400)
End If
If MSFlexGrid1.ColWidth(1) > 0 Then '第2列 单位名称
Printer.Line (v2, A)-(v2, A + 400)
Printer.CurrentX = v2 + 10
Printer.CurrentY = A + 100
Printer.FontSize = 10
If MSFlexGrid1.TextMatrix(i, 1) <> "" Then
Printer.Print MSFlexGrid1.TextMatrix(i, 1)
End If
End If
If MSFlexGrid1.ColWidth(2) > 0 Then '第3列 设备类型
Printer.Line (v3, A)-(v3, A + 400)
Printer.CurrentX = v3 + 10
Printer.CurrentY = A + 100
Printer.FontSize = 10
If MSFlexGrid1.TextMatrix(i, 2) <> "" Then
Printer.Print MSFlexGrid1.TextMatrix(i, 2)
End If
End If
If MSFlexGrid1.ColWidth(3) > 0 Then '第4列 设备名称
Printer.Line (v4, A)-(v4, A + 400)
Printer.CurrentX = v4 + 20
Printer.CurrentY = A + 100
Printer.FontSize = 10
If MSFlexGrid1.TextMatrix(i, 3) <> "" Then
Printer.Print MSFlexGrid1.TextMatrix(i, 3)
End If
End If
If MSFlexGrid1.ColWidth(4) > 0 Then '第5列 设备品牌
Printer.Line (v5, A)-(v5, A + 400)
Printer.CurrentX = v5 + 50
Printer.CurrentY = A + 100
Printer.FontSize = 10
If MSFlexGrid1.TextMatrix(i, 4) <> "" Then
Printer.Print MSFlexGrid1.TextMatrix(i, 4)
End If
End If
If MSFlexGrid1.ColWidth(5) > 0 Then '第6列 设备型号
Printer.Line (v6, A)-(v6, A + 400)
Printer.CurrentX = v6 + 30
Printer.CurrentY = A + 100
Printer.FontSize = 10
If MSFlexGrid1.TextMatrix(i, 5) <> "" Then
Printer.Print MSFlexGrid1.TextMatrix(i, 5)
End If
End If
If MSFlexGrid1.ColWidth(6) > 0 Then '第7列 设备序列号
Printer.Line (v7, A)-(v7, A + 400)
Printer.CurrentX = v7 + 60
Printer.CurrentY = A + 100
Printer.FontSize = 10
If MSFlexGrid1.TextMatrix(i, 6) <> "" Then
Printer.Print MSFlexGrid1.TextMatrix(i, 6)
End If
End If
If MSFlexGrid1.ColWidth(7) > 0 Then '第8列 购入时间
Printer.Line (v8, A)-(v8, A + 400)
Printer.CurrentX = v8 + 30
Printer.CurrentY = A + 100
Printer.FontSize = 10
If MSFlexGrid1.TextMatrix(i, 7) <> "" Then
Printer.Print MSFlexGrid1.TextMatrix(i, 7)
End If
End If
If MSFlexGrid1.ColWidth(8) > 0 Then '第9列 使用时间
Printer.Line (v9, A)-(v9, A + 400)
Printer.CurrentX = v9 + 60
Printer.CurrentY = A + 100
Printer.FontSize = 10
If MSFlexGrid1.TextMatrix(i, 8) <> "" Then
Printer.Print MSFlexGrid1.TextMatrix(i, 8)
End If
End If
If MSFlexGrid1.ColWidth(9) > 0 Then '第10列 使用部门
Printer.Line (v10, A)-(v10, A + 400)
Printer.CurrentX = v10 + 150
Printer.CurrentY = A + 100
Printer.FontSize = 10
If MSFlexGrid1.TextMatrix(i, 9) <> "" Then
Printer.Print MSFlexGrid1.TextMatrix(i, 9)
End If
End If
If MSFlexGrid1.ColWidth(10) > 0 Then '第11列 设备原值
Printer.Line (v11, A)-(v11, A + 400)
Printer.CurrentX = v11 + 200
Printer.CurrentY = A + 100
Printer.FontSize = 10
If MSFlexGrid1.TextMatrix(i, 10) <> "" Then
Printer.Print MSFlexGrid1.TextMatrix(i, 10)
End If
' Printer.Line (v12, A)-(v12, A + 450) '画右坚边线(封口)
End If
If MSFlexGrid1.ColWidth(11) > 0 Then '第12列 备注
Printer.Line (v12, A)-(v12, A + 400)
Printer.CurrentX = v12 + 100
Printer.CurrentY = A + 100
Printer.FontSize = 10
If MSFlexGrid1.TextMatrix(i, 11) <> "" Then
Printer.Print MSFlexGrid1.TextMatrix(i, 11)
End If
Printer.Line (v13, A)-(v13, A + 400) '画右坚边线(封口)
End If
A = A + 400 '行距每次增加 450
Next i '内循环结束
Printer.Line (v1, A)-(v14, A) '画底边横线
Printer.NewPage '新的一页
Printer.EndDoc '换页
Next j '外循环结束
Call MsgBox("正在打印......!", vbOKOnly + vbInformation, "提示...")
End Sub
Private Sub Form_Load()
Dim sql As String
sql = "select * from EquInfo order by EID"
createlist (sql)
TxtWhm2.Text = MSFlexGrid1.Rows - 1
End Sub
Public Sub createlist(sql As String)
Dim rs As New ADODB.Recordset
Dim i As Integer
With Me.MSFlexGrid1
.TextMatrix(0, 0) = "设备编号"
.TextMatrix(0, 1) = "单位名称"
.TextMatrix(0, 2) = "设备类型"
.TextMatrix(0, 3) = "设备名称"
.TextMatrix(0, 4) = "设备品牌"
.TextMatrix(0, 5) = "设备型号"
.TextMatrix(0, 6) = "设备序列号"
.TextMatrix(0, 7) = "设备购入时间"
.TextMatrix(0, 8) = "使用时间"
.TextMatrix(0, 9) = "使用部门"
.TextMatrix(0, 10) = "设备原值(元)"
.TextMatrix(0, 11) = "备 注"
For i = 0 To 10
.ColAlignment(i) = 1 '所有表格对及方式
Next i
For i = 0 To 10
.ColWidth(i) = 1200 '每列宽度
Next i
'.ColWidth(3) = 1500
.ColWidth(11) = 1800
End With
Set rs = TransactSQL(sql)
With Me.MSFlexGrid1
.Rows = 1
While Not rs.EOF
.Rows = .Rows + 1
.TextMatrix(.Rows - 1, 0) = rs(1)
.TextMatrix(.Rows - 1, 1) = rs(2)
.TextMatrix(.Rows - 1, 2) = rs(3)
.TextMatrix(.Rows - 1, 3) = rs(4)
.TextMatrix(.Rows - 1, 4) = rs(5)
.TextMatrix(.Rows - 1, 5) = rs(6)
.TextMatrix(.Rows - 1, 6) = rs(7)
.TextMatrix(.Rows - 1, 7) = rs(8)
.TextMatrix(.Rows - 1, 8) = rs(9)
.TextMatrix(.Rows - 1, 9) = rs(10)
.TextMatrix(.Rows - 1, 10) = rs(11)
.TextMatrix(.Rows - 1, 11) = rs(12)
rs.MoveNext
Wend
End With
End Sub
我想要每页显23条记录,点打印总是下标越界,但是点调试后还是可以打。如果我的数据库只有23条记录,点打印却能正常打。
不能打印.rar
(57.43 KB)
[ 本帖最后由 hsbwp1314 于 2010-10-17 09:31 编辑 ]