2011-08-08 81 views
1

我在与报告事件的一个问题,我没有 在访问之前遇到Access 2007中Access 2007年ReportEvents类不触发事件

之前我使用的Access 2007年的正投结束于SQL后端。
我有一个类,ReportEvents,我用于报告。 在报告的Report_Open事件中,我实例化,然后使用这个类来处理事件,如激活,关闭,NoData,并且我还将常用代码 (如将数据导出为ex​​cel而不是报告)发送到 。

此代码在我以前使用的Access 2003应用程序(mdb)中正常工作, 但它在2007年(accdb)中按预期工作。在我的测试中,对非事件公共子集的调用ProvideXLOption像魅力一样工作,但ReportEvents类中没有任何事件正在触发 。我没有更改我刚刚将它导入 项目的代码。我设置了破发点但他们没有被击中。我将他们全部更改为 公共活动,然后在测试报告事件中调用它们,并且它们工作正常。

我在Access 2007中创建了另一个报告,结果相同。我已经检查了 Access中的启动设置,它们都很好。我甚至删除并重新添加 来自受信任位置的数据库位置,但没有任何运气。

微软修改了事件代码吗?或者这只是我没有看到的简单代码错误?这很简单。我的大脑只是烤面包(我的儿子在昨晚喂食后决定保持清醒)。

类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 

回答

2

我想通了。这是范围问题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 

这是一个令人惊讶的是一点休息会为你做。