Access 2007中的用户定义事件无法正常工作

时间:2018-01-31 14:20:21

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

我在Access 2007中遇到用户定义事件的问题:

事件在类模块中声明 - 让我们将此类命名为Controller - 如下所示(简化代码示例):

Public Event EventA()

[...]

Public Property Let PropertyA(RHS As String)
    mPropertyA = RHS
    RaiseEvent EventA
End Property

[...]

该类在模块中实例化为"self-healing" object,如下所示:

Public Property Get objController() As Controller
    Static c As Controller

    If c Is Nothing Then _
        Set c = New Controller

    Set objController = c
End Property

在表单中声明Controller类并在子Form_Load()中设置如下:

Private WithEvents mController  As Controller

[...]

Private Sub Form_Load()
    [...]
    Set mController = objController
    [...]
End Sub

在同一表格中我实施了事件动作:

Private Sub mController_EventA()
    [...]
    Me!PropertyA = mController.PropertyA
    [...]
End Sub

单击表单上的按钮后,将打开带有树视图的对话框表单。单击PropertyA对象中树视图Controller中的节点后,将更改:

Private Sub tvwRDS_NodeClick(ByVal node As Object)
    [...]
    objController.PropertyA = node.key
    [...]
End Sub

我的意图是:

  1. 您点击一个节点。
  2. 设置了实例化类PropertyA
  3. Controller,并引发了事件EventA()
  4. 主要表格是处理事件;表单中的控件已更新。
  5. 第一次一切按预期工作。在使用“压缩和修复”功能以及编译和创建ACCDE文件之后,子mController_EventA()似乎在空间中丢失:事件触发后没有任何反应。为什么?!

    WithEvents子句仅在对象模块中允许。但我需要从普通模块中实例化自我修复对象。

    提前多多感谢!

    D.C。

1 个答案:

答案 0 :(得分:1)

似乎Property本身必须声明为Static

Public Static Property Get objController() As Controller
    Static c As Controller

    If c Is Nothing Then _
        Set c = New Controller

    Set objController = c
End Property

此后它运作良好。

相关问题