1、创建临时表(临时初婚表)
2、创建两个查询,“查询1”用于求得每组中的记录数,“初婚表打印查询”用作报表“初婚表打印2”的记录源
3、创建报表“初婚表打印2”,设置分组(ID村居)页眉和页脚
4、报表“初婚表打印2”格式化代码:
Option Compare Database
Option Explicit
Dim k, T1, T2 As Integer 'T1为组中的当前页数,T2为组中的总页数
Dim j, j1 As Integer
Dim Stemp As String
Private Sub 页面页眉_Format(Cancel As Integer, FormatCount As Integer)
Me!i = Me!i - 1
End Sub
Private Sub 主体_Format(Cancel As Integer, FormatCount As Integer)
Me!i = Me!i + 1 '行数计数累加
End Sub
Private Sub 主体_Print(Cancel As Integer, PrintCount As Integer)
On Error GoTo Err_主体_Print
j1 = 12 '设定每页打印行数
If i <= j1 - 1 Then
T1 = 1
Else
If i Mod j1 = 0 Then
k = Int(i / j1)
For j = 1 To k
T1 = j
Next
Else
T1 = k + 1
End If
End If
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Stemp = "select * from 临时初婚表 where [村居ID]='" & Me.[村居ID] & "'"
rs.Open Stemp, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
If rs.RecordCount <= j1 Then
T2 = 1
Else
T2 = Int(rs.RecordCount / j1) ' 如果不要补空行,则 T2=Int(rs.RecordCount / j1) +1,其实因为补了空行,每组的记录数(包括空行数),都是j1的倍数
End If
T0 = "第" & [T1] & "页/共" & [T2] & "页 "
rs.Close
Set rs = Nothing
Exit_主体_Print:
Exit Sub
Err_主体_Print:
MsgBox Err.Description
Resume Exit_主体_Print
End Sub
Private Sub 组页眉0_Format(Cancel As Integer, FormatCount As Integer)
Me!i = 0 '保证分组后,序号从1开始计数
End Sub
5、“连续打印”单击事件:
Private Sub Command10_Click()
On Error GoTo Err_Command10_Click
Dim i, i2, i3 As Long
Dim k As Long '打印总行数
Dim j As Integer
j = 12 '每页打印行数
Dim Stemp As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Stemp = "select * from 临时初婚表"
rs.Open Stemp, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
Dim rs2 As ADODB.Recordset
Set rs2 = New ADODB.Recordset
Stemp = "select * from 初婚表"
rs2.Open Stemp, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
Dim rs3 As ADODB.Recordset
Set rs3 = New ADODB.Recordset
Stemp = "Select 初婚表.村居ID, Count(初婚表.[家庭编号]) AS 记录数 FROM 初婚表 GROUP BY 初婚表.村居ID"
rs3.Open Stemp, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
'--------打印前,清空临时表中的记录
If rs.RecordCount > 0 Then
rs.MoveFirst
For i = 0 To rs.RecordCount - 1
rs.Delete 1
rs.Update
rs.MoveNext
Next
End If
'--------把所有要打印的记录,插入到临时表中
rs2.MoveFirst
For i = 0 To rs2.RecordCount - 1
rs.AddNew
rs![ID] = rs2![ID]
rs![村居ID] = rs2![村居ID]
rs![村居] = rs2![村居]
rs![家庭编号] = rs2![家庭编号]
rs![育妇姓名] = rs2![育妇姓名]
rs![出生日期] = rs2![出生日期]
rs![婚姻日期] = rs2![婚姻日期]
rs![婚姻状况] = rs2![婚姻状况]
rs![配偶姓名] = rs2![配偶姓名]
rs![配偶出生日期] = rs2![配偶出生日期]
rs![配偶婚姻状况] = rs2![配偶婚姻状况]
rs![婚姻备注] = rs2![婚姻备注]
rs![登记日期] = rs2![登记日期]
rs![变更日期] = rs2![变更日期]
rs.Update
rs2.MoveNext
Next
'--------确定要补的空行数,并插入到临时表中
rs3.MoveFirst
For i = 0 To rs3.RecordCount - 1
If rs3![记录数] Mod j = 0 Then
k = rs3![记录数]
Else
k = (Int(rs3![记录数] / j) + 1) * j
End If
For i3 = 1 To k - rs3![记录数]
rs.AddNew
rs![村居ID] = rs3![村居ID]
rs.Update
Next
rs3.MoveNext
Next
'--------以预览的方式 打开报表
DoCmd.OpenReport "初婚表打印2", acViewPreview
rs.Close
Set rs = Nothing
rs2.Close
Set rs2 = Nothing
rs3.Close
Set rs3 = Nothing
Exit_Command10_Click:
Exit Sub
Err_Command10_Click:
MsgBox Err.Description
Resume Exit_Command10_Click
End Sub
6、组合框[村居ID]的更新事件:
Private Sub 村居ID_AfterUpdate()
Call Command10_Click
End Sub