我有一个表格,一个接一个地调用一系列报告。我还有一个全局变量用作计数器。每次调用页面的PageFooterSection_Format时,我都想增加计数。问题是,计数器正在计算得更高。这是我的代码:
我有一个名为genericFunctions的模块:
Option Compare Database
Public pageCount As Integer
在我的表单中,我有一个循环调用它:
'before loop
pageCount = 1
'start loop, which I left out for brevity
'run in previewView first so page footer format function is called
DoCmd.OpenReport reportName, acViewPreview, , , acHidden, !ID
'then run this to open in report view, so onload event runs
DoCmd.OpenReport reportName, acViewReport, , , acHidden, !ID
'save report as a pdf
DoCmd.OutputTo acOutputReport, reportName, "PDF", rptPath
'close report
DoCmd.Close acReport, reportName
'I then have a method that "stitches" these individual reports into one PDF
现在,在我的报告中,我有这段代码:
Private Sub PageFooterSection_Format(Cancel As Integer, FormatCount As Integer)
If (Report.CurrentView = 5) Then
'a textbox in the report
Me.pgNumber.Value = pageCount
pageCount = pageCount + 1
End If
End Sub
然后我在事件中放置一个断点。这个事件应该被击中3次。所以,我希望pgNumber文本框的值为1,2,3,但它有2,4,6。我的代码中没有其他地方我增加pageCount变量。这是怎么回事?这是范围问题吗?
答案 0 :(得分:2)
PageFooterSection_Format
同时针对DoCmd.OpenReport reportName, acViewPreview
和DoCmd.OutputTo acOutputReport, reportName, "PDF"
运行,因为后者就像打印命令一样。
在这两种情况下,Report.CurrentView = acViewReport
(5)。我不知道该怎么做 - 感觉就像一个bug。 (注意:这是Access 2010)
无论如何,由于DoCmd.OutputTo
就像打印(因此就像打印预览一样),您只需省略此行:
DoCmd.OpenReport reportName, acViewPreview, , , acHidden, !ID
至少那对我有用。
注意:事件过程中的断点可能会误导何时发生。使用Debug.Print
电话更安全。
我的测试代码是:
Sub ReportTesting()
Const reportName = "Bericht4"
'before loop
pageCount = 1
'run in previewView first so page footer format function is called
Debug.Print pageCount, "acViewPreview"
DoCmd.OpenReport reportName, acViewPreview, , , acHidden
'then run this to open in report view, so onload event runs
Debug.Print pageCount, "acViewReport"
DoCmd.OpenReport reportName, acViewReport, , , acHidden
'save report as a pdf
Debug.Print pageCount, "acOutputReport"
DoCmd.OutputTo acOutputReport, reportName, "PDF", "C:\test.pdf"
'close report
DoCmd.Close acReport, reportName
Debug.Print pageCount, "done"
End Sub
和
Private Sub Report_Open(Cancel As Integer)
Debug.Print "Report View: " & Me.CurrentView
End Sub
Private Sub PageFooterSection_Format(Cancel As Integer, FormatCount As Integer)
If Me.CurrentView = acViewReport Then
Debug.Print "Footer - acViewReport"
'a textbox in the report
Me.pgNumber.Value = pageCount
pageCount = pageCount + 1
Else
Debug.Print "Footer - other view" ' <-- this never happens
End If
End Sub
立即窗口中的输出:
1 acViewPreview
Report View: 5
Footer - acViewReport
2 acViewReport
Report View: 6
2 acOutputReport
Footer - acViewReport
3 done