Access 报表打印之分组页码实现(轉)
生活随笔
收集整理的這篇文章主要介紹了
Access 报表打印之分组页码实现(轉)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/Files/Tonyyang/報表分組頁碼V1.2.rar ? Option?Compare?Database
Option?Explicit
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'分組報表顯示分組頁碼和頁數(shù)類模塊(方法一)
'
'功????能:在有分組的報表的每一頁上顯示組頁碼和組頁數(shù),在預(yù)
'?????????覽時改變頁面設(shè)置后仍能正確顯示。
'作????者:t小雨(tcl013@126.com)(t小寶)
'版????本:1.1
'創(chuàng)建日期:2008-05-??
'整理日期:2008-05-30
'補(bǔ)充說明:這個代碼是一年前做的,由于實現(xiàn)原理和過程有點復(fù)雜,
'?????????當(dāng)時沒有添加注釋,已忘得差不多,加上表達(dá)能力有限,
'?????????現(xiàn)在勉強(qiáng)添加了不完全的注釋,但能依照說明會用就行。
'?????????直接把代碼放到報表中也是可以。
'?????????做成類模塊只是為了好保存,以后調(diào)用方便,但由于在
'?????????類模塊中不能使用報表的節(jié)的事件,調(diào)用起來還是有些
'?????????麻煩,不過總要比直接把代碼放在報表簡單一點。
'
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'實現(xiàn)原理:
'通過報表上的輔助文本框獲得組的總行數(shù)(記錄數(shù),后同)、1頁的最大行數(shù)、當(dāng)前頁在當(dāng)前組的截止行數(shù),
'在頁面頁腳_Format事件中通過計算得到分組頁碼和頁數(shù)。
'報表設(shè)計要求:
'?1、報表應(yīng)包含組頁眉、頁面頁眉、頁面頁腳
'?2、在組頁眉上有一文本框,有如下屬性
'????ControlSource(控件來源)="=Count(*)"
'????RunningSum(運行總和)=0(不)
'?3、在主體有一文本框,有如下屬性
'????ControlSource(控件來源)="=1"
'????RunningSum(運行總和)=1(工作組之上)
'調(diào)用方法,有2種:
'?第1種:
'?1、在報表用New關(guān)鍵字聲明一個?CreateGroupPage1?類的新實例
'?2、在報表的打開事件執(zhí)行實例的?Init?方法,傳入全部參數(shù)
'?3、在頁面頁腳的?Format?(格式化)事件執(zhí)行實例的?FormatPageFooter?方法
'?4、在頁面頁腳的?Print?(打印)事件執(zhí)行實例的?PrintPageFooter?方法
'?這種方法在顯示分組頁碼的標(biāo)簽上顯示效果如?分組字段值:?1?/?2
'?在報表中的代碼類似下面:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'?Dim?newGroupPage?As?New?CreateGroupPage1
'
'?Private?Sub?Report_Open(Cancel?As?Integer)
'?????newGroupPage.Init?Me,?Me.TxtGrpRows,?Me.txtRunSum,?Me.LplGrpPages
'?End?Sub
'
'?Private?Sub?頁面頁腳_Format(Cancel?As?Integer,?FormatCount?As?Integer)
'?????newGroupPage.FormatPageFooter
'?End?Sub
'
'?Private?Sub?頁面頁腳_Print(Cancel?As?Integer,?PrintCount?As?Integer)
'?????newGroupPage.PrintPageFooter
'?End?Sub
'
'?Private?Sub?組頁眉0_Format(Cancel?As?Integer,?FormatCount?As?Integer)
'?????newGroupPage.FormatGroupLevel1Header
'?End?Sub
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'?第2種:
'?1、在報表用?WithEvents?關(guān)鍵字聲明一個?CreateGroupPage1?類的變量
'?2、在報表的打開事件用?Set?New?語句創(chuàng)建新實例
'?3、在報表的打開事件執(zhí)行實例的?Init?方法,不須傳入最后一個參數(shù)(用于顯示分組頁碼的標(biāo)簽)
'?4、在頁面頁腳的?Format(格式化)?事件執(zhí)行實例的?FormatPageFooter?方法
'?5、在頁面頁腳的?Print(打印)?事件執(zhí)行實例的?PrintPageFooter?方法
'?6、在組頁眉的?Format(格式化)?事件執(zhí)行實例的?FormatGroupLevel1Header?方法
'?7、在類的?Current?事件過程將事件參數(shù)返回的分組頁碼和頁數(shù)賦給用于顯示的標(biāo)簽
'?在報表中的代碼類似下面:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'?Dim?WithEvents?newGroupPage?As?CreateGroupPage1
'
'?Private?Sub?Report_Open(Cancel?As?Integer)
'?????Set?newGroupPage?=?New?CreateGroupPage1
'?????newGroupPage.Init?Me,?Me.TxtGrpRows,?Me.txtRunSum
'?End?Sub
'
'?Private?Sub?頁面頁腳_Format(Cancel?As?Integer,?FormatCount?As?Integer)
'?????newGroupPage.FormatPageFooter
'?End?Sub
'
'?Private?Sub?頁面頁腳_Print(Cancel?As?Integer,?PrintCount?As?Integer)
'?????newGroupPage.PrintPageFooter
'?End?Sub
'
'?Private?Sub?組頁眉0_Format(Cancel?As?Integer,?FormatCount?As?Integer)
'?????newGroupPage.FormatGroupLevel1Header
'?End?Sub
'
'?Private?Sub?newGroupPage_Current(GrpPage?As?Integer,?GrpPages?As?Integer)
'?????Me.LplGrpPages.Caption?=?Me.類別名稱?&?"?共?"?&?GrpPages?&?"?頁,第?"?&?GrpPage?&?"?頁"
'?End?Sub
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public?Event?Current(GrpPage?As?Integer,?GrpPages?As?Integer)
'在此自定義事件中可以獲取分組頁碼信息,以便自定義顯示頁碼格式
Dim?MyRpt?As?Report
Dim?txtRunSum?As?TextBox???????????'?取得每個組的記錄數(shù)的文本框
Dim?TxtGrpRows?As?TextBox??????????'?取得每個組的記錄在組中的序號的文本框
Dim?lblShowPage?As?Label???????????'?用于顯示分組頁碼信息的標(biāo)簽
Dim?inMaxRows?As?Integer???????????'?1頁的最大行數(shù)(記錄數(shù))
Dim?inRptPage?As?Integer???????????'?報表本身頁碼
Dim?blPrint?As?Boolean?????????????'?是否已經(jīng)發(fā)生頁面頁腳的Print事件,為避免頁面頁腳的Format事件中的代碼重復(fù)運行
Dim?blFistPage?As?Boolean??????????'?當(dāng)前頁是否是所在組的第一頁
Public?Sub?Init(rpt?As?Report,?GrpRows?As?TextBox,?RunSum?As?TextBox,?Optional?ShowPage?As?Label)
'?過程中的檢查參數(shù)代碼不是必須的,僅為了防止以后忘記如何設(shè)計報表
????Dim?st1?As?String
????Set?MyRpt?=?rpt
????
????Set?TxtGrpRows?=?GrpRows
????With?TxtGrpRows
????????If?.Section?<>?acGroupLevel1Header?Then
????????????st1?=?"作為第二個參數(shù)的文本框必須在分組頁眉節(jié)上!"
????????ElseIf?.ControlSource?<>?"=Count(*)"?Then
????????????st1?=?"作為第二個參數(shù)的文本框的ControlSource屬性必須是""=Count(*)""!"
????????ElseIf?.RunningSum?<>?0?Then
????????????st1?=?"作為第二個參數(shù)的文本框的RunningSum屬性必須是0!"
????????End?If
????End?With
????If?Len(st1)?>?0?Then
????????MsgBox?st1,?vbExclamation,?"參數(shù)錯誤"
????????Exit?Sub
????End?If
????
????Set?txtRunSum?=?RunSum
????With?txtRunSum
????????If?.Section?<>?acDetail?Then
?????????????st1?=?"作為第三個參數(shù)的文本框必須在報表主體節(jié)上!"
????????ElseIf?.ControlSource?<>?"=1"?Then
????????????st1?=?"作為第三個參數(shù)的文本框的ControlSource屬性必須是""=1""!"
????????ElseIf?.RunningSum?<>?1?Then
????????????st1?=?"作為第三個參數(shù)的文本框的RunningSum屬性必須是1!"
????????End?If
????End?With
????If?Len(st1)?>?0?Then
????????MsgBox?st1,?vbExclamation,?"參數(shù)錯誤"
????????Exit?Sub
????End?If
????
????If?Not?(ShowPage?Is?Nothing)?Then?Set?lblShowPage?=?ShowPage
???
End?Sub
Public?Sub?FormatGroupLevel1Header()
????If?txtRunSum?=?1?Then?blFistPage?=?True???????????'?為頁面頁腳Format事件作標(biāo)記
End?Sub
Public?Sub?FormatPageFooter()
????Dim?inGrpPage?As?Integer???????????'?組頁碼
????Dim?inGrpPages?As?Integer??????????'?組頁數(shù)
????Dim?inLastRows?As?Integer??????????'?截止當(dāng)前頁,所在組的所有行數(shù)
????inLastRows?=?txtRunSum???????????????????????????????????????'?從文本框獲得截止行數(shù)
????If?inLastRows?=?0?Then?inLastRows?=?TxtGrpRows
????
????If?MyRpt.Page?=?1?Then
????'在第1頁初始變量
????????If?MyRpt.Pages?>?0?And?MyRpt.Pages?=?inRptPage?Then
????????'?這里已經(jīng)是第2輪格式化第1頁,報表加載時進(jìn)行兩輪格式化,第一輪Pages=0
????????Else
????????????inMaxRows?=?0
????????End?If
????????inRptPage?=?0
????????blPrint?=?False
????End?If
????
????If?Not?blPrint?Then
????'僅在第1輪格式化中,獲取每組第一頁的行數(shù)
????????If?blFistPage?Then
????????'每組第一頁的行數(shù)即是本組任一頁的最大行數(shù)
????????????If?inMaxRows?<?inLastRows?Then?inMaxRows?=?inLastRows
????????????blFistPage?=?False
????????End?If
????????inRptPage?=?inRptPage?+?1
????End?If
????
????If?MyRpt.Pages?>?0?Then
????????inGrpPages?=?Int(TxtGrpRows?/?inMaxRows?+?0.9999)?????????'?組的總行數(shù)除以1頁的行數(shù),得到組的頁數(shù)
????????inGrpPage?=?Int(inLastRows?/?inMaxRows?+?0.9999)??????????'?截止當(dāng)前頁的累計行數(shù)除以1頁的行數(shù),得到當(dāng)前頁的頁碼
????????If?Not?(lblShowPage?Is?Nothing)?Then
????????????lblShowPage.Caption?=?inGrpPage?&?"?/?"?&?inGrpPages
????????End?If
????????RaiseEvent?Current(inGrpPage,?inGrpPages)
????End?If
End?Sub
Public?Sub?PrintPageFooter()
????blPrint?=?True
End?Sub ? 方法二: ? Option?Compare?Database
Option?Explicit
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'分組報表顯示分組頁碼和頁數(shù)類模塊(方法二)
'
'功????能:在有分組的報表的每一頁上顯示組頁碼和組頁數(shù),在預(yù)
'?????????覽時改變頁面設(shè)置后仍能正確顯示。
'作????者:t小雨(tcl013@126.com)(t小寶)
'版????本:1.1
'創(chuàng)建日期:2008-05-??
'整理日期:2008-05-30
'補(bǔ)充說明:這個代碼是一年前做的,由于實現(xiàn)原理和過程相當(dāng)復(fù)雜,
'?????????當(dāng)時沒有添加注釋,已忘得差不多,加上表達(dá)能力有限,
'?????????現(xiàn)在勉強(qiáng)添加了不完全的注釋,但能依照說明會用就行。
'?????????直接把代碼放到報表中也是可以。
'?????????做成類模塊只是為了好保存,以后調(diào)用方便,但由于在
'?????????類模塊中不能使用報表的節(jié)的事件,調(diào)用起來還是有些
'?????????麻煩,不過總要比直接把代碼放在報表簡單一點。
'
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'實現(xiàn)原理:
'?報表加載后:會發(fā)生兩輪從第1頁到最后1頁每頁的格式化事件,每輪結(jié)束后發(fā)生一次打印事件,
'?在第一輪格式化事件中報表的Pages屬性始終為0。最后還會移到第一頁。
'?重設(shè)紙張邊距、方向或大小等后也發(fā)生類似上述過程,情況相對復(fù)雜一點,就不細(xì)說了。
'?在這些事件中把頁碼信息保存到數(shù)組中,數(shù)組元素和報表頁數(shù)一樣,每個元素代表一頁的信息。
'?移動頁后:也會發(fā)生一次格式化事件,在這些事件中把數(shù)組中頁碼的信息顯示出來。
'?上面所說的事件都是頁面頁腳的事件。
'報表設(shè)計要求:
'?很簡單,有一個分組字段和頁面頁腳即可
'調(diào)用方法有2種:
'?第1種:
'?1、在報表用New關(guān)鍵字聲明一個?CreateGroupPage2?類的新實例
'?2、在報表的打開事件執(zhí)行實例的?Init?方法,傳入報表、分組字段和用于顯示分組頁碼的標(biāo)簽
'?3、在頁面頁腳的?Format?(格式化)事件執(zhí)行實例的?FormatPageFooter?方法
'?4、在頁面頁腳的?Print?(打印)事件執(zhí)行實例的?PrintPageFooter?方法
'?這種方法在顯示分組頁碼的標(biāo)簽上顯示效果如?分組字段值:?1?/?2
'?在報表中的代碼類似下面:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'??Dim?newGroupPage?As?New?CreateGroupPage2
'
'??Private?Sub?Report_Open(Cancel?As?Integer)
'??????newGroupPage.Init?Me,?Me.類別ID,?Me.LplGrpPages
'??End?Sub
'
'??Private?Sub?頁面頁腳_Format(Cancel?As?Integer,?FormatCount?As?Integer)
'??????newGroupPage.FormatPageFooter
'??End?Sub
'
'??Private?Sub?頁面頁腳_Print(Cancel?As?Integer,?PrintCount?As?Integer)
'??????newGroupPage.PrintPageFooter
'??End?Sub
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'?第2種:
'?1、在報表用?WithEvents?關(guān)鍵字聲明一個?CreateGroupPage2?類的變量
'?2、在報表的打開事件用?Set?New?語句創(chuàng)建新實例
'?3、在報表的打開事件執(zhí)行實例的?Init?方法,傳入報表、分組字段,不須傳入用于顯示分組頁碼的標(biāo)簽
'?4、在頁面頁腳的?Format(格式化)?事件執(zhí)行實例的?FormatPageFooter?方法
'?5、在頁面頁腳的?Print(打印)?事件執(zhí)行實例的?PrintPageFooter?方法
'?6、在類的?Current?事件過程將事件參數(shù)返回的分組頁碼和頁數(shù)賦給用于顯示的標(biāo)簽
'?在報表中的代碼類似下面:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'??Dim?WithEvents?newGroupPage?As?CreateGroupPage2
'
'??Private?Sub?Report_Open(Cancel?As?Integer)
'??????Set?newGroupPage?=?New?CreateGroupPage2
'??????newGroupPage.Init?Me,?Me.類別ID,?Me.LplGrpPages
'??End?Sub
'
'??Private?Sub?頁面頁腳_Format(Cancel?As?Integer,?FormatCount?As?Integer)
'??????newGroupPage.FormatPageFooter
'??End?Sub
'
'??Private?Sub?頁面頁腳_Print(Cancel?As?Integer,?PrintCount?As?Integer)
'??????newGroupPage.PrintPageFooter
'??End?Sub
'
'??Private?Sub?newGroupPage_Current(GrpPage?As?Integer,?GrpPages?As?Integer)
'??????Me.LplGrpPages.Caption?=?Me.類別名稱?&?"?共?"?&?GrpPages?&?"?頁,第?"?&?GrpPage?&?"?頁"
'??End?Sub
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'?這種方法在顯示分組頁碼的標(biāo)簽上顯示效果是自定義的
Public?Event?Current(GrpPage?As?Integer,?GrpPages?As?Integer)
'在此自定義事件中可以獲取分組頁碼信息,以便自定義顯示頁碼格式
Dim?MyRpt?As?Report
Dim?ctrGroup?As?Control
Dim?lblShowPage?As?Label
Dim?blPrint?As?Boolean????????????'?是否已經(jīng)發(fā)生頁面頁腳的Print事件,為避免頁面頁腳的Format事件中的代碼重復(fù)運行
Dim?stGroupText?As?String?????????'?分組字段值
Dim?inRptPage?As?Integer??????????'?報表頁號
Dim?inGrpPage?As?Integer??????????'?組頁號
Dim?ainGrpPage()?As?Integer???????'?保存組頁號的數(shù)組,用于顯示
Dim?ainGrpPages()?As?Integer??????'?保存每個組的總頁數(shù)的數(shù)組,用于顯示
Dim?ainGrpPageTmp()?As?Integer????'?保存組頁號的數(shù)組,臨時
Dim?ainGrpPagesTmp()?As?Integer???'?保存每個組的總頁數(shù)的數(shù)組,臨時
Public?Sub?Init(rpt?As?Report,?Group?As?Control,?Optional?ShowPage?As?Label)
'rpt??????:報表本身,必須
'Group????:用于分組的字段,必須
'ShowPage?:用于顯示分組頁碼的標(biāo)簽,可選
????Set?MyRpt?=?rpt
????Set?ctrGroup?=?Group
????If?Not?(ShowPage?Is?Nothing)?Then?Set?lblShowPage?=?ShowPage
End?Sub
Public?Sub?FormatPageFooter()
Dim?inShowGrpPage?As?Integer???????????'?顯示的組頁碼
Dim?inShowGrpPages?As?Integer??????????'?顯示的組頁數(shù)
Dim?i?As?Integer,?j?As?Integer
If?MyRpt.Page?=?1?Then
'?在第1頁初始變量
????If?inRptPage?>?0?And?inRptPage?=?MyRpt.Pages?Then
????'?報表加載后第一輪格式化完畢發(fā)生
????????For?j?=?inRptPage?-?inGrpPage?+?1?To?inRptPage???''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
????????????ainGrpPagesTmp(j)?=?inGrpPage????????????????'
????????Next?????????????????????????????????????????????'
????????ReDim?ainGrpPage(1?To?inRptPage)?????????????????'?這一段代碼與后面一段代碼一樣,因為后面無法判斷加載完成
????????ReDim?ainGrpPages(1?To?inRptPage)????????????????'
????????For?i?=?1?To?inRptPage???????????????????????????'
????????????ainGrpPage(i)?=?ainGrpPageTmp(i)?????????????'
????????????ainGrpPages(i)?=?ainGrpPagesTmp(i)???????????'
????????Next?????????????????????????????????????????????'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
????End?If
????
????inRptPage?=?0
????inGrpPage?=?0
????stGroupText?=?""
????blPrint?=?False
End?If
If?Not?blPrint?Then
'僅在第一輪格式化中獲取頁位置,保存到臨時數(shù)組
????inRptPage?=?inRptPage?+?1
????ReDim?Preserve?ainGrpPageTmp(1?To?inRptPage)
????ReDim?Preserve?ainGrpPagesTmp(1?To?inRptPage)
????
????If?stGroupText?=?ctrGroup?Then??????????????????????'?當(dāng)前頁與上一頁在同一組
????????inGrpPage?=?inGrpPage?+?1???????????????????????'?累計本組頁數(shù),也即獲得當(dāng)前頁在當(dāng)前組中的頁碼
????Else????????????????????????????????????????????????'?當(dāng)前頁與上一頁不在同一組,換組
????????For?j?=?inRptPage?-?inGrpPage?To?inRptPage?-?1??'?循環(huán)上一組的每一頁
????????????ainGrpPagesTmp(j)?=?inGrpPage???????????????'?每個元素都儲存總頁數(shù),頁在組中的最大序號即總頁數(shù)
????????Next
????????inGrpPage?=?1???????????????????????????????????'?重新開始累計本組頁數(shù)
????????stGroupText?=?ctrGroup
????End?If
????ainGrpPageTmp(inRptPage)?=?inGrpPage????????????????'?每個元素都儲存頁在組中的頁碼
End?If
If?MyRpt.Page?=?MyRpt.Pages?Then
'報表已打開后重設(shè)紙張邊距大小方向等會發(fā)生
????If?inRptPage?=?MyRpt.Pages?Then
????'僅在最后一頁把臨時數(shù)組中的頁碼信息更新到用于顯示頁碼的數(shù)組
????????For?j?=?inRptPage?-?inGrpPage?+?1?To?inRptPage
????????????ainGrpPagesTmp(j)?=?inGrpPage???????????????'?這個循環(huán)代碼與上面有重復(fù),因為上面無法判斷最后一頁
????????Next
????????
????????ReDim?ainGrpPage(1?To?inRptPage)????????????????'?數(shù)組大小為報表頁數(shù)
????????ReDim?ainGrpPages(1?To?inRptPage)
????????For?i?=?1?To?inRptPage
????????????ainGrpPage(i)?=?ainGrpPageTmp(i)
????????????ainGrpPages(i)?=?ainGrpPagesTmp(i)
????????Next
????End?If
End?If
On?Error?Resume?Next
If?MyRpt.Pages?>?0?Then
????inShowGrpPages?=?ainGrpPages(MyRpt.Page)???'
????inShowGrpPage?=?ainGrpPage(MyRpt.Page)????'
????If?Not?(lblShowPage?Is?Nothing)?Then?lblShowPage.Caption?=?_
????????ctrGroup?&?":?"?&?inShowGrpPage?&?"?/?"?&?inShowGrpPages
????RaiseEvent?Current(inShowGrpPage,?inShowGrpPages)
End?If
End?Sub
Public?Sub?PrintPageFooter()
'?區(qū)分兩輪格式化
????blPrint?=?True
End?Sub ? ?
Option?Explicit
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'分組報表顯示分組頁碼和頁數(shù)類模塊(方法一)
'
'功????能:在有分組的報表的每一頁上顯示組頁碼和組頁數(shù),在預(yù)
'?????????覽時改變頁面設(shè)置后仍能正確顯示。
'作????者:t小雨(tcl013@126.com)(t小寶)
'版????本:1.1
'創(chuàng)建日期:2008-05-??
'整理日期:2008-05-30
'補(bǔ)充說明:這個代碼是一年前做的,由于實現(xiàn)原理和過程有點復(fù)雜,
'?????????當(dāng)時沒有添加注釋,已忘得差不多,加上表達(dá)能力有限,
'?????????現(xiàn)在勉強(qiáng)添加了不完全的注釋,但能依照說明會用就行。
'?????????直接把代碼放到報表中也是可以。
'?????????做成類模塊只是為了好保存,以后調(diào)用方便,但由于在
'?????????類模塊中不能使用報表的節(jié)的事件,調(diào)用起來還是有些
'?????????麻煩,不過總要比直接把代碼放在報表簡單一點。
'
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'實現(xiàn)原理:
'通過報表上的輔助文本框獲得組的總行數(shù)(記錄數(shù),后同)、1頁的最大行數(shù)、當(dāng)前頁在當(dāng)前組的截止行數(shù),
'在頁面頁腳_Format事件中通過計算得到分組頁碼和頁數(shù)。
'報表設(shè)計要求:
'?1、報表應(yīng)包含組頁眉、頁面頁眉、頁面頁腳
'?2、在組頁眉上有一文本框,有如下屬性
'????ControlSource(控件來源)="=Count(*)"
'????RunningSum(運行總和)=0(不)
'?3、在主體有一文本框,有如下屬性
'????ControlSource(控件來源)="=1"
'????RunningSum(運行總和)=1(工作組之上)
'調(diào)用方法,有2種:
'?第1種:
'?1、在報表用New關(guān)鍵字聲明一個?CreateGroupPage1?類的新實例
'?2、在報表的打開事件執(zhí)行實例的?Init?方法,傳入全部參數(shù)
'?3、在頁面頁腳的?Format?(格式化)事件執(zhí)行實例的?FormatPageFooter?方法
'?4、在頁面頁腳的?Print?(打印)事件執(zhí)行實例的?PrintPageFooter?方法
'?這種方法在顯示分組頁碼的標(biāo)簽上顯示效果如?分組字段值:?1?/?2
'?在報表中的代碼類似下面:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'?Dim?newGroupPage?As?New?CreateGroupPage1
'
'?Private?Sub?Report_Open(Cancel?As?Integer)
'?????newGroupPage.Init?Me,?Me.TxtGrpRows,?Me.txtRunSum,?Me.LplGrpPages
'?End?Sub
'
'?Private?Sub?頁面頁腳_Format(Cancel?As?Integer,?FormatCount?As?Integer)
'?????newGroupPage.FormatPageFooter
'?End?Sub
'
'?Private?Sub?頁面頁腳_Print(Cancel?As?Integer,?PrintCount?As?Integer)
'?????newGroupPage.PrintPageFooter
'?End?Sub
'
'?Private?Sub?組頁眉0_Format(Cancel?As?Integer,?FormatCount?As?Integer)
'?????newGroupPage.FormatGroupLevel1Header
'?End?Sub
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'?第2種:
'?1、在報表用?WithEvents?關(guān)鍵字聲明一個?CreateGroupPage1?類的變量
'?2、在報表的打開事件用?Set?New?語句創(chuàng)建新實例
'?3、在報表的打開事件執(zhí)行實例的?Init?方法,不須傳入最后一個參數(shù)(用于顯示分組頁碼的標(biāo)簽)
'?4、在頁面頁腳的?Format(格式化)?事件執(zhí)行實例的?FormatPageFooter?方法
'?5、在頁面頁腳的?Print(打印)?事件執(zhí)行實例的?PrintPageFooter?方法
'?6、在組頁眉的?Format(格式化)?事件執(zhí)行實例的?FormatGroupLevel1Header?方法
'?7、在類的?Current?事件過程將事件參數(shù)返回的分組頁碼和頁數(shù)賦給用于顯示的標(biāo)簽
'?在報表中的代碼類似下面:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'?Dim?WithEvents?newGroupPage?As?CreateGroupPage1
'
'?Private?Sub?Report_Open(Cancel?As?Integer)
'?????Set?newGroupPage?=?New?CreateGroupPage1
'?????newGroupPage.Init?Me,?Me.TxtGrpRows,?Me.txtRunSum
'?End?Sub
'
'?Private?Sub?頁面頁腳_Format(Cancel?As?Integer,?FormatCount?As?Integer)
'?????newGroupPage.FormatPageFooter
'?End?Sub
'
'?Private?Sub?頁面頁腳_Print(Cancel?As?Integer,?PrintCount?As?Integer)
'?????newGroupPage.PrintPageFooter
'?End?Sub
'
'?Private?Sub?組頁眉0_Format(Cancel?As?Integer,?FormatCount?As?Integer)
'?????newGroupPage.FormatGroupLevel1Header
'?End?Sub
'
'?Private?Sub?newGroupPage_Current(GrpPage?As?Integer,?GrpPages?As?Integer)
'?????Me.LplGrpPages.Caption?=?Me.類別名稱?&?"?共?"?&?GrpPages?&?"?頁,第?"?&?GrpPage?&?"?頁"
'?End?Sub
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public?Event?Current(GrpPage?As?Integer,?GrpPages?As?Integer)
'在此自定義事件中可以獲取分組頁碼信息,以便自定義顯示頁碼格式
Dim?MyRpt?As?Report
Dim?txtRunSum?As?TextBox???????????'?取得每個組的記錄數(shù)的文本框
Dim?TxtGrpRows?As?TextBox??????????'?取得每個組的記錄在組中的序號的文本框
Dim?lblShowPage?As?Label???????????'?用于顯示分組頁碼信息的標(biāo)簽
Dim?inMaxRows?As?Integer???????????'?1頁的最大行數(shù)(記錄數(shù))
Dim?inRptPage?As?Integer???????????'?報表本身頁碼
Dim?blPrint?As?Boolean?????????????'?是否已經(jīng)發(fā)生頁面頁腳的Print事件,為避免頁面頁腳的Format事件中的代碼重復(fù)運行
Dim?blFistPage?As?Boolean??????????'?當(dāng)前頁是否是所在組的第一頁
Public?Sub?Init(rpt?As?Report,?GrpRows?As?TextBox,?RunSum?As?TextBox,?Optional?ShowPage?As?Label)
'?過程中的檢查參數(shù)代碼不是必須的,僅為了防止以后忘記如何設(shè)計報表
????Dim?st1?As?String
????Set?MyRpt?=?rpt
????
????Set?TxtGrpRows?=?GrpRows
????With?TxtGrpRows
????????If?.Section?<>?acGroupLevel1Header?Then
????????????st1?=?"作為第二個參數(shù)的文本框必須在分組頁眉節(jié)上!"
????????ElseIf?.ControlSource?<>?"=Count(*)"?Then
????????????st1?=?"作為第二個參數(shù)的文本框的ControlSource屬性必須是""=Count(*)""!"
????????ElseIf?.RunningSum?<>?0?Then
????????????st1?=?"作為第二個參數(shù)的文本框的RunningSum屬性必須是0!"
????????End?If
????End?With
????If?Len(st1)?>?0?Then
????????MsgBox?st1,?vbExclamation,?"參數(shù)錯誤"
????????Exit?Sub
????End?If
????
????Set?txtRunSum?=?RunSum
????With?txtRunSum
????????If?.Section?<>?acDetail?Then
?????????????st1?=?"作為第三個參數(shù)的文本框必須在報表主體節(jié)上!"
????????ElseIf?.ControlSource?<>?"=1"?Then
????????????st1?=?"作為第三個參數(shù)的文本框的ControlSource屬性必須是""=1""!"
????????ElseIf?.RunningSum?<>?1?Then
????????????st1?=?"作為第三個參數(shù)的文本框的RunningSum屬性必須是1!"
????????End?If
????End?With
????If?Len(st1)?>?0?Then
????????MsgBox?st1,?vbExclamation,?"參數(shù)錯誤"
????????Exit?Sub
????End?If
????
????If?Not?(ShowPage?Is?Nothing)?Then?Set?lblShowPage?=?ShowPage
???
End?Sub
Public?Sub?FormatGroupLevel1Header()
????If?txtRunSum?=?1?Then?blFistPage?=?True???????????'?為頁面頁腳Format事件作標(biāo)記
End?Sub
Public?Sub?FormatPageFooter()
????Dim?inGrpPage?As?Integer???????????'?組頁碼
????Dim?inGrpPages?As?Integer??????????'?組頁數(shù)
????Dim?inLastRows?As?Integer??????????'?截止當(dāng)前頁,所在組的所有行數(shù)
????inLastRows?=?txtRunSum???????????????????????????????????????'?從文本框獲得截止行數(shù)
????If?inLastRows?=?0?Then?inLastRows?=?TxtGrpRows
????
????If?MyRpt.Page?=?1?Then
????'在第1頁初始變量
????????If?MyRpt.Pages?>?0?And?MyRpt.Pages?=?inRptPage?Then
????????'?這里已經(jīng)是第2輪格式化第1頁,報表加載時進(jìn)行兩輪格式化,第一輪Pages=0
????????Else
????????????inMaxRows?=?0
????????End?If
????????inRptPage?=?0
????????blPrint?=?False
????End?If
????
????If?Not?blPrint?Then
????'僅在第1輪格式化中,獲取每組第一頁的行數(shù)
????????If?blFistPage?Then
????????'每組第一頁的行數(shù)即是本組任一頁的最大行數(shù)
????????????If?inMaxRows?<?inLastRows?Then?inMaxRows?=?inLastRows
????????????blFistPage?=?False
????????End?If
????????inRptPage?=?inRptPage?+?1
????End?If
????
????If?MyRpt.Pages?>?0?Then
????????inGrpPages?=?Int(TxtGrpRows?/?inMaxRows?+?0.9999)?????????'?組的總行數(shù)除以1頁的行數(shù),得到組的頁數(shù)
????????inGrpPage?=?Int(inLastRows?/?inMaxRows?+?0.9999)??????????'?截止當(dāng)前頁的累計行數(shù)除以1頁的行數(shù),得到當(dāng)前頁的頁碼
????????If?Not?(lblShowPage?Is?Nothing)?Then
????????????lblShowPage.Caption?=?inGrpPage?&?"?/?"?&?inGrpPages
????????End?If
????????RaiseEvent?Current(inGrpPage,?inGrpPages)
????End?If
End?Sub
Public?Sub?PrintPageFooter()
????blPrint?=?True
End?Sub ? 方法二: ? Option?Compare?Database
Option?Explicit
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'分組報表顯示分組頁碼和頁數(shù)類模塊(方法二)
'
'功????能:在有分組的報表的每一頁上顯示組頁碼和組頁數(shù),在預(yù)
'?????????覽時改變頁面設(shè)置后仍能正確顯示。
'作????者:t小雨(tcl013@126.com)(t小寶)
'版????本:1.1
'創(chuàng)建日期:2008-05-??
'整理日期:2008-05-30
'補(bǔ)充說明:這個代碼是一年前做的,由于實現(xiàn)原理和過程相當(dāng)復(fù)雜,
'?????????當(dāng)時沒有添加注釋,已忘得差不多,加上表達(dá)能力有限,
'?????????現(xiàn)在勉強(qiáng)添加了不完全的注釋,但能依照說明會用就行。
'?????????直接把代碼放到報表中也是可以。
'?????????做成類模塊只是為了好保存,以后調(diào)用方便,但由于在
'?????????類模塊中不能使用報表的節(jié)的事件,調(diào)用起來還是有些
'?????????麻煩,不過總要比直接把代碼放在報表簡單一點。
'
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'實現(xiàn)原理:
'?報表加載后:會發(fā)生兩輪從第1頁到最后1頁每頁的格式化事件,每輪結(jié)束后發(fā)生一次打印事件,
'?在第一輪格式化事件中報表的Pages屬性始終為0。最后還會移到第一頁。
'?重設(shè)紙張邊距、方向或大小等后也發(fā)生類似上述過程,情況相對復(fù)雜一點,就不細(xì)說了。
'?在這些事件中把頁碼信息保存到數(shù)組中,數(shù)組元素和報表頁數(shù)一樣,每個元素代表一頁的信息。
'?移動頁后:也會發(fā)生一次格式化事件,在這些事件中把數(shù)組中頁碼的信息顯示出來。
'?上面所說的事件都是頁面頁腳的事件。
'報表設(shè)計要求:
'?很簡單,有一個分組字段和頁面頁腳即可
'調(diào)用方法有2種:
'?第1種:
'?1、在報表用New關(guān)鍵字聲明一個?CreateGroupPage2?類的新實例
'?2、在報表的打開事件執(zhí)行實例的?Init?方法,傳入報表、分組字段和用于顯示分組頁碼的標(biāo)簽
'?3、在頁面頁腳的?Format?(格式化)事件執(zhí)行實例的?FormatPageFooter?方法
'?4、在頁面頁腳的?Print?(打印)事件執(zhí)行實例的?PrintPageFooter?方法
'?這種方法在顯示分組頁碼的標(biāo)簽上顯示效果如?分組字段值:?1?/?2
'?在報表中的代碼類似下面:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'??Dim?newGroupPage?As?New?CreateGroupPage2
'
'??Private?Sub?Report_Open(Cancel?As?Integer)
'??????newGroupPage.Init?Me,?Me.類別ID,?Me.LplGrpPages
'??End?Sub
'
'??Private?Sub?頁面頁腳_Format(Cancel?As?Integer,?FormatCount?As?Integer)
'??????newGroupPage.FormatPageFooter
'??End?Sub
'
'??Private?Sub?頁面頁腳_Print(Cancel?As?Integer,?PrintCount?As?Integer)
'??????newGroupPage.PrintPageFooter
'??End?Sub
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'?第2種:
'?1、在報表用?WithEvents?關(guān)鍵字聲明一個?CreateGroupPage2?類的變量
'?2、在報表的打開事件用?Set?New?語句創(chuàng)建新實例
'?3、在報表的打開事件執(zhí)行實例的?Init?方法,傳入報表、分組字段,不須傳入用于顯示分組頁碼的標(biāo)簽
'?4、在頁面頁腳的?Format(格式化)?事件執(zhí)行實例的?FormatPageFooter?方法
'?5、在頁面頁腳的?Print(打印)?事件執(zhí)行實例的?PrintPageFooter?方法
'?6、在類的?Current?事件過程將事件參數(shù)返回的分組頁碼和頁數(shù)賦給用于顯示的標(biāo)簽
'?在報表中的代碼類似下面:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'??Dim?WithEvents?newGroupPage?As?CreateGroupPage2
'
'??Private?Sub?Report_Open(Cancel?As?Integer)
'??????Set?newGroupPage?=?New?CreateGroupPage2
'??????newGroupPage.Init?Me,?Me.類別ID,?Me.LplGrpPages
'??End?Sub
'
'??Private?Sub?頁面頁腳_Format(Cancel?As?Integer,?FormatCount?As?Integer)
'??????newGroupPage.FormatPageFooter
'??End?Sub
'
'??Private?Sub?頁面頁腳_Print(Cancel?As?Integer,?PrintCount?As?Integer)
'??????newGroupPage.PrintPageFooter
'??End?Sub
'
'??Private?Sub?newGroupPage_Current(GrpPage?As?Integer,?GrpPages?As?Integer)
'??????Me.LplGrpPages.Caption?=?Me.類別名稱?&?"?共?"?&?GrpPages?&?"?頁,第?"?&?GrpPage?&?"?頁"
'??End?Sub
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'?這種方法在顯示分組頁碼的標(biāo)簽上顯示效果是自定義的
Public?Event?Current(GrpPage?As?Integer,?GrpPages?As?Integer)
'在此自定義事件中可以獲取分組頁碼信息,以便自定義顯示頁碼格式
Dim?MyRpt?As?Report
Dim?ctrGroup?As?Control
Dim?lblShowPage?As?Label
Dim?blPrint?As?Boolean????????????'?是否已經(jīng)發(fā)生頁面頁腳的Print事件,為避免頁面頁腳的Format事件中的代碼重復(fù)運行
Dim?stGroupText?As?String?????????'?分組字段值
Dim?inRptPage?As?Integer??????????'?報表頁號
Dim?inGrpPage?As?Integer??????????'?組頁號
Dim?ainGrpPage()?As?Integer???????'?保存組頁號的數(shù)組,用于顯示
Dim?ainGrpPages()?As?Integer??????'?保存每個組的總頁數(shù)的數(shù)組,用于顯示
Dim?ainGrpPageTmp()?As?Integer????'?保存組頁號的數(shù)組,臨時
Dim?ainGrpPagesTmp()?As?Integer???'?保存每個組的總頁數(shù)的數(shù)組,臨時
Public?Sub?Init(rpt?As?Report,?Group?As?Control,?Optional?ShowPage?As?Label)
'rpt??????:報表本身,必須
'Group????:用于分組的字段,必須
'ShowPage?:用于顯示分組頁碼的標(biāo)簽,可選
????Set?MyRpt?=?rpt
????Set?ctrGroup?=?Group
????If?Not?(ShowPage?Is?Nothing)?Then?Set?lblShowPage?=?ShowPage
End?Sub
Public?Sub?FormatPageFooter()
Dim?inShowGrpPage?As?Integer???????????'?顯示的組頁碼
Dim?inShowGrpPages?As?Integer??????????'?顯示的組頁數(shù)
Dim?i?As?Integer,?j?As?Integer
If?MyRpt.Page?=?1?Then
'?在第1頁初始變量
????If?inRptPage?>?0?And?inRptPage?=?MyRpt.Pages?Then
????'?報表加載后第一輪格式化完畢發(fā)生
????????For?j?=?inRptPage?-?inGrpPage?+?1?To?inRptPage???''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
????????????ainGrpPagesTmp(j)?=?inGrpPage????????????????'
????????Next?????????????????????????????????????????????'
????????ReDim?ainGrpPage(1?To?inRptPage)?????????????????'?這一段代碼與后面一段代碼一樣,因為后面無法判斷加載完成
????????ReDim?ainGrpPages(1?To?inRptPage)????????????????'
????????For?i?=?1?To?inRptPage???????????????????????????'
????????????ainGrpPage(i)?=?ainGrpPageTmp(i)?????????????'
????????????ainGrpPages(i)?=?ainGrpPagesTmp(i)???????????'
????????Next?????????????????????????????????????????????'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
????End?If
????
????inRptPage?=?0
????inGrpPage?=?0
????stGroupText?=?""
????blPrint?=?False
End?If
If?Not?blPrint?Then
'僅在第一輪格式化中獲取頁位置,保存到臨時數(shù)組
????inRptPage?=?inRptPage?+?1
????ReDim?Preserve?ainGrpPageTmp(1?To?inRptPage)
????ReDim?Preserve?ainGrpPagesTmp(1?To?inRptPage)
????
????If?stGroupText?=?ctrGroup?Then??????????????????????'?當(dāng)前頁與上一頁在同一組
????????inGrpPage?=?inGrpPage?+?1???????????????????????'?累計本組頁數(shù),也即獲得當(dāng)前頁在當(dāng)前組中的頁碼
????Else????????????????????????????????????????????????'?當(dāng)前頁與上一頁不在同一組,換組
????????For?j?=?inRptPage?-?inGrpPage?To?inRptPage?-?1??'?循環(huán)上一組的每一頁
????????????ainGrpPagesTmp(j)?=?inGrpPage???????????????'?每個元素都儲存總頁數(shù),頁在組中的最大序號即總頁數(shù)
????????Next
????????inGrpPage?=?1???????????????????????????????????'?重新開始累計本組頁數(shù)
????????stGroupText?=?ctrGroup
????End?If
????ainGrpPageTmp(inRptPage)?=?inGrpPage????????????????'?每個元素都儲存頁在組中的頁碼
End?If
If?MyRpt.Page?=?MyRpt.Pages?Then
'報表已打開后重設(shè)紙張邊距大小方向等會發(fā)生
????If?inRptPage?=?MyRpt.Pages?Then
????'僅在最后一頁把臨時數(shù)組中的頁碼信息更新到用于顯示頁碼的數(shù)組
????????For?j?=?inRptPage?-?inGrpPage?+?1?To?inRptPage
????????????ainGrpPagesTmp(j)?=?inGrpPage???????????????'?這個循環(huán)代碼與上面有重復(fù),因為上面無法判斷最后一頁
????????Next
????????
????????ReDim?ainGrpPage(1?To?inRptPage)????????????????'?數(shù)組大小為報表頁數(shù)
????????ReDim?ainGrpPages(1?To?inRptPage)
????????For?i?=?1?To?inRptPage
????????????ainGrpPage(i)?=?ainGrpPageTmp(i)
????????????ainGrpPages(i)?=?ainGrpPagesTmp(i)
????????Next
????End?If
End?If
On?Error?Resume?Next
If?MyRpt.Pages?>?0?Then
????inShowGrpPages?=?ainGrpPages(MyRpt.Page)???'
????inShowGrpPage?=?ainGrpPage(MyRpt.Page)????'
????If?Not?(lblShowPage?Is?Nothing)?Then?lblShowPage.Caption?=?_
????????ctrGroup?&?":?"?&?inShowGrpPage?&?"?/?"?&?inShowGrpPages
????RaiseEvent?Current(inShowGrpPage,?inShowGrpPages)
End?If
End?Sub
Public?Sub?PrintPageFooter()
'?區(qū)分兩輪格式化
????blPrint?=?True
End?Sub ? ?
轉(zhuǎn)載于:https://www.cnblogs.com/Tonyyang/archive/2011/11/21/2256843.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Access 报表打印之分组页码实现(轉)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOS 多线程的一些总结
- 下一篇: 有关short与int的重载[jase基