ms-access - VBA 使用全局变量 Access 奇怪的行为

标签 ms-access vba ms-access-2007

我有一个表单,可以一个接一个地调用一系列报告。我还有一个用作计数器的全局变量。每次调用页面的 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 变量。这是怎么回事?这是范围问题吗?

最佳答案

PageFooterSection_Format 既适用于 DoCmd.OpenReport reportName, acViewPreview 也适用于 DoCmd.OutputTo acOutputReport, reportName, "PDF",因为后者就像一个打印命令。

在这两种情况下,Report.CurrentView = acViewReport (5)。我不知道该怎么做——感觉就像一个错误。 (注意:这是 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

关于ms-access - VBA 使用全局变量 Access 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36367679/

相关文章:

sql - 从多个表中查找

ms-access - 如何将饼图添加到我的 Access 报告中

vba - 使用公式获取 Excel 中范围的名称

sql - 微软 Access : setting table column Caption or Description in DDL?

ms-access - 查询输入必须至少包含一个表或查询

excel - Access 2013 中的类型转换失败

excel - XMLHTTP onTimeOut 时如何使用 VBA 回调函数?

excel - 正确处理 VBA (Excel) 中的错误

sql - 有没有办法通过 INNER JOIN 绕过 "The multi-valued field ' [Table X].[Field Y ]' is not valid in specified Join clause"?

vba - Access 2016 VBA运行时错误-如何捕获?