【发布时间】:2011-10-22 12:55:22
【问题描述】:
我遇到了我没有报告事件的问题 在 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
【问题讨论】:
标签: ms-access ms-access-2007 vba