MS Access在VBA中以编程方式更改“ OnChange”事件

时间:2018-10-01 14:54:23

标签: vba access-vba

我正在一个项目中,我必须使用MS Access,并且必须使数据库尽可能地松散(这很奇怪,我不认为最佳实践,但就目前而言,这就是我所拥有的。) / p>

无论如何,我有50个组合框,但是经常必须对它们进行更改,这意味着我必须手动更改所有组合框。我宁愿花一个小时来找到编程解决方案,然后手动花5分钟来做。

我需要使用VBA更改“ OnChange”事件,但是我的代码会产生错误。

Private Function RunChangePropagate()
    Dim combo As ComboBox
    RevealGrid
    For Each combo In Me.Controls
        combo.OnChange = "=ComboBox_Change()"
    Next combo
    ClearGrid
End Function

错误: Error

我还尝试过将文本更改为Variant,然后将事件分配给所述Variant。

我该怎么做?

谢谢。

2 个答案:

答案 0 :(得分:1)

这是一个最小的示例,有效:

Public Sub ChangeEvent()

    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If ctrl.Name = "Combo5" Then
            Debug.Print ctrl.OnChange
            ctrl.OnChange = "SomeProcedure"
        End If
    Next ctrl

End Sub

在您的示例中,您只应删除分配中的=。不需要在已分配子的末尾加上括号。

答案 1 :(得分:1)

使用 WithEvents 。这样,您的表单就完全与控制表单的类(某些控件)“分离”了。

我发表了一篇文章,其中包含链接和供所有人学习的示例:

Create Windows Phone Colour Palette and Selector using WithEvents

主要代码(模块和类模块后面的代码)仅是:

Option Explicit

' Helper class for form Palette for event handling of textboxes.
' 2017-04-19. Gustav Brock, Cactus Data ApS, CPH.
' Version 1.0.0
' License: MIT.

' *

Private Const EventProcedure    As String = "[Event Procedure]"

Private WithEvents ClassTextBox As Access.TextBox


Public Sub Initialize(ByRef TextBox As Access.TextBox)

    Set ClassTextBox = TextBox

    ClassTextBox.OnClick = EventProcedure

End Sub


Public Sub Terminate()

    Set ClassTextBox = Nothing

End Sub


Private Sub ClassTextBox_Click()

    ' Select full content.
    ClassTextBox.SelStart = 0
    ClassTextBox.SelLength = Len(ClassTextBox.Value)
    ' Display the clicked value.
    ClassTextBox.Parent!CopyClicked.Value = ClassTextBox.Value
    ' Copy the clicked value to the clipboard.
    DoCmd.RunCommand acCmdCopy

End Sub

和:

Option Explicit

' Form to display the Windows Phone 7.5/8.0 colour theme.
' Also works as a basic example of implementing WithEvents for a form.
' 2017-04-19. Gustav Brock, Cactus Data ApS, CPH.
' Version 1.0.0
' License: MIT.

' *

Private ControlCollection   As Collection


Private Sub Form_Load()

    ' Load events for all colour value textboxes.

    Dim EventProcedure  As ClassTextboxSelect
    Dim Control         As Access.Control

    Set ControlCollection = New Collection

    For Each Control In Me.Controls
        If Control.ControlType = acTextBox Then
            Set EventProcedure = New ClassTextboxSelect
            EventProcedure.Initialize Control
            ControlCollection.Add EventProcedure, Control.Name
        End If
    Next

    Set EventProcedure = Nothing
    Set Control = Nothing

End Sub


Private Sub Form_Unload(Cancel As Integer)

    ' Unload events for all colour value textboxes.

    Dim EventProcedure  As ClassTextboxSelect

    For Each EventProcedure In ControlCollection
        EventProcedure.Terminate
    Next

    Set EventProcedure = Nothing
    Set ControlCollection = Nothing

End Sub

完整代码也位于 GitHub 上:VBA.ModernTheme