Access 2007 ReportEvents 类未触发事件

发布于 2024-11-28 06:07:33 字数 2132 浏览 0 评论 0原文

我在报告我没有报告的事件时遇到问题 以前在 Access 2007 之前的 Access 中遇到过。

我使用 Access 2007 作为 SQL 后端的前端。
我有一个用于报告的类 ReportEvents。 在报告的 Report_Open 事件中,我实例化然后使用此类 处理激活、关闭、NoData 等事件,我还放置了通用代码 例如将数据导出到 Excel 而不是报告。

这段代码在我之前使用的 Access 2003 应用程序 (mdb) 中运行良好, 但它在 2007 年并没有按预期工作(accdb)。在我的测试中,对非事件公共子的调用,ProvideXLOption 就像一个魅力,但没有任何事件被触发 来自 ReportEvents 类。我没有更改代码,我只是将其导入到 项目。我设置了断点,但它们没有被击中。我把它们全部改成了 公共事件,然后在测试报告事件中调用它们,它们工作得很好。

我在 Access 2007 中设置了另一个报表,结果相同。我查过 Access 中的启动设置都很好。我什至删除并重新添加 来自受信任位置的数据库位置没有任何运气。

Microsoft 是否修改了事件代码,或者这只是我没有看到的一个简单的代码错误?这一定是一些简单的事情。我的大脑很糟糕(我儿子在昨晚喂奶后决定保持清醒)。

类ReportEvents代码:

Option Compare Database
Option Explicit

Private WithEvents rpt As Access.Report
Const strEventKey As String = "[Event Procedure]"

Public Property Set Report(Rept As Access.Report)

    Set rpt = Rept
    With rpt
        .OnActivate = strEventKey        
        .OnClose = strEventKey
        If LenB(.OnNoData) = 0 Then
            .OnNoData = strEventKey
        End If
    End With
End Property

Public Sub Terminate()
    On Error Resume Next
    Set rpt = Nothing
End Sub

Private Sub rpt_Activate()
    LoadPrintRibbon
End Sub

Private Sub rpt_Close()
    Me.Terminate
End Sub

Private Sub rpt_NoData(Cancel As Integer)
    Dim strMsg  As String
    strMsg = "No Records were found that match your criteria."
    MsgBox strMsg, vbInformation, rpt.Name & _
         ": No Match. Report Cancelled"
    Cancel = True
End Sub

Private Sub LoadPrintRibbon()
 #If AccessVersion >= 12 Then
    If rpt.RibbonName <> "PrintPreview" Then
       rpt.RibbonName = "PrintPreview"
    End If
 #End If
End Sub

';;Provides user with option to send data to Excel instead of a report
Public Sub ProvideXLOption(Cancel As Integer)
 '... some XLOption Code
End Sub

在测试报告代码中:

Private Sub Report_Open(Cancel As Integer)
    Dim rptEvts   As ReportEvents
    Set rptEvts = New ReportEvents
    Set rptEvts.Report = Me 

    ';;Let User User Choose to Export Data to Excel or Display the report
    rptEvts.ProvideXLOption Cancel
End Sub

I'm having an issue with report events that I haven't
encountered before in Access prior to Access 2007.

I'm using using Access 2007 for a Front-end to a SQL Back-end.
I have a Class, ReportEvents, that I use for the reports.
In a report's Report_Open event I instantiate and then use this class to
handle events like activate, Close, NoData and I also put common code
such as exporting data to excel instead of a report.

This code was working fine in a previous Access 2003 application (mdb) I was using,
but it isn't working as expected in 2007 (accdb). In my tests the call to a non event public sub, ProvideXLOption works like a charm, but none of the events are being fired
from the ReportEvents class. I didn't change the code I just imported it into the
project. I set up break points but they aren't being hit. I changed all of them to
public events and then called them within the test reports event and they worked fine.

I set up another report in Access 2007 with the same results. I've checked
the startup settings in Access and they are fine. I even removed and re-added
the database location from the trusted locations without any luck.

Has Microsoft modified the Events Code or is this just a simple code error on my part that I'm not seeing? It's gotta be something simple. My brain is just toast (my son decided to stay awake after last night's feeding).

Class ReportEvents Code:

Option Compare Database
Option Explicit

Private WithEvents rpt As Access.Report
Const strEventKey As String = "[Event Procedure]"

Public Property Set Report(Rept As Access.Report)

    Set rpt = Rept
    With rpt
        .OnActivate = strEventKey        
        .OnClose = strEventKey
        If LenB(.OnNoData) = 0 Then
            .OnNoData = strEventKey
        End If
    End With
End Property

Public Sub Terminate()
    On Error Resume Next
    Set rpt = Nothing
End Sub

Private Sub rpt_Activate()
    LoadPrintRibbon
End Sub

Private Sub rpt_Close()
    Me.Terminate
End Sub

Private Sub rpt_NoData(Cancel As Integer)
    Dim strMsg  As String
    strMsg = "No Records were found that match your criteria."
    MsgBox strMsg, vbInformation, rpt.Name & _
         ": No Match. Report Cancelled"
    Cancel = True
End Sub

Private Sub LoadPrintRibbon()
 #If AccessVersion >= 12 Then
    If rpt.RibbonName <> "PrintPreview" Then
       rpt.RibbonName = "PrintPreview"
    End If
 #End If
End Sub

';;Provides user with option to send data to Excel instead of a report
Public Sub ProvideXLOption(Cancel As Integer)
 '... some XLOption Code
End Sub

In the Test Report Code:

Private Sub Report_Open(Cancel As Integer)
    Dim rptEvts   As ReportEvents
    Set rptEvts = New ReportEvents
    Set rptEvts.Report = Me 

    ';;Let User User Choose to Export Data to Excel or Display the report
    rptEvts.ProvideXLOption Cancel
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

壹場煙雨 2024-12-05 06:07:33

我想通了。这是一个范围问题 ReportEvents 类变量 rptEvts 位于 Report_Open 子项内。因此,当其他事件发生时,它就不会存在。它应该在模块级别而不是在过程内。

Dim rptEvts   As ReportEvents
Private Sub Report_Open(Cancel As Integer)     
   Set rptEvts = New ReportEvents     
   Set rptEvts.Report = Me       
   ';;Let User User Choose to Export Data to Excel or Display the report
   rptEvts.ProvideXLOption Cancel End Sub
End Sub

稍事休息对你的好处真是令人惊奇。

I figured it out. It was a scope issue The ReportEvents class variable rptEvts, was inside the Report_Open sub. Because of this it wouldn't exist when the other events happened. It should be at the module level and not within the procedure.

Dim rptEvts   As ReportEvents
Private Sub Report_Open(Cancel As Integer)     
   Set rptEvts = New ReportEvents     
   Set rptEvts.Report = Me       
   ';;Let User User Choose to Export Data to Excel or Display the report
   rptEvts.ProvideXLOption Cancel End Sub
End Sub

It's amazing what a little rest will do for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文