【问题标题】:VBA Access strange behavior with global variablesVBA 使用全局变量访问奇怪的行为
【发布时间】:2016-04-02 16:19:33
【问题描述】:

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

【问题讨论】:

  • pagecount 变量显示什么?
  • @JapanDave 显示和文本框一样,计数为 2,4,6。这可能是因为我先在 acPreview 中打开报告,然后在 acReportView 中打开报告吗?即使仅在调用 acPreview 时触发 Format 事件?
  • 单步执行代码怎么样?好像是在函数里跑了两次???
  • 你能在表单模块中提供一个完整的代码块,以便我们可以看到这个循环吗?此外,报表的打印预览应该运行 OnLoad 事件,无需隐藏。

标签: ms-access vba ms-access-2007


【解决方案1】:

PageFooterSection_FormatDoCmd.OpenReport reportName, acViewPreviewDoCmd.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

【讨论】:

  • 你是对的。这太奇怪了,在 acViewReport 中事件仍然被调用,但断点不起作用。我想知道这会有什么其他影响?谢谢您的帮助! +1 进行实验以确认!
猜你喜欢
  • 2016-08-24
  • 2013-01-16
  • 2012-04-08
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
相关资源
最近更新 更多