【问题标题】:MS Access: Replace Many On Click Events with Generic On Click EventMS Access:用通用的点击事件替换许多点击事件
【发布时间】:2021-11-17 00:58:48
【问题描述】:

我在 MS Access 中有一个表单,顶部有 12 个按钮作为列标题。对于每个按钮,都有一个 On Click 事件,它调用相同的公共函数。此功能(如下所示)打开与单击的按钮对应的字段的过滤器菜单。

Public Function HeaderClick(HeaderName As String) 
    DoCmd.GoToControl "[" & HeaderName & "]"
    DoCmd.RunCommand acCmdFilterMenu
End Function

例如,我单击“名字”按钮,它会拉出“名字”的过滤器菜单:

我想知道是否有一种方法可以在单击任何这些按钮时调用HeaderClick。换句话说,我想要创建 12 个单独的 On Click 事件的替代方法。原因是这是我在许多数据库中的许多表单上使用的一种技术。只是看起来效率不高。

【问题讨论】:

  • 您可以使用事件下沉。搜索一下,我会尽快尝试发布一些内容。
  • 点击不总是将焦点设置在控件上吗?所以你不需要DoCmd.GoToControl,你的函数可以完全通用。只需选择所有按钮并在 OnClick 事件属性中写入 =HeaderClick()
  • @Andre 如果焦点在按钮上,那么我无法调出适当的过滤器菜单。我实际上希望焦点位于按钮下方的文本框上。但你对第二部分是正确的。
  • 啊,没错。但是如果可以自动从关联的按钮名称中获取文本框名称,可以使用ActiveControl获取名称,就不需要这个参数了。
  • 好点。我只需要为按钮使用标准命名约定或给每个按钮一个标签。

标签: vba ms-access


【解决方案1】:

当您单击 OnClick 文本框以添加新功能时,您通常会看到文本 [Event Procedure]。只需用函数的名称覆盖它(确保函数在全局模块中)。因此,您可以输入=HeaderClick("Name"),而不是[Event Procedure]。您仍然需要为函数指定参数。

【讨论】:

    【解决方案2】:

    你会创建一个类,我已经像这样调用了我的 clsCustomButton

    Option Explicit
    
    Private WithEvents cmdCustom As CommandButton
    
    Public Sub Initialise(cmdIn As CommandButton)
        Set cmdCustom = cmdIn
        cmdCustom.OnClick = "[Event Procedure]"
    End Sub
    
    Private Sub cmdCustom_Click()
        MsgBox "Hello"
    End Sub
    
    Private Sub Class_Terminate()
        Set cmdCustom = Nothing
    End Sub
    

    在标准代码模块中,您需要有一个集合/数组来保存这个“新”自定义按钮,我称之为 colButtons

    Option Explicit
    
    Public colButtons As Collection
    

    然后您需要通过传入要更改的按钮来添加到集合中,我刚刚完成了一个,但是您可以循环全部或指定使用标签/名称来执行此操作。像这样,从表单中,通常在打开时

    Dim clsCommandButton As clsCustomButton
    
    Set colButtons = New Collection
        
    Set clsCommandButton = New clsCustomButton
    
    clsCommandButton.Initialise Me.Command0
    
    colButtons.Add clsCommandButton, CStr(colButtons.Count)
    

    【讨论】:

    • 不幸的是,我对课程知之甚少,但你给了我很好的理由去研究它们。谢谢!
    【解决方案3】:

    另一个示例是我的 Windows Phone 主题颜色 选择器,其中捕获了对 文本框 的点击:

    表格

    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
    

    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
    Attribute ClassTextBox.VB_VarHelpID = -1
    
    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
    

    完整代码、供下载的 Access 应用程序以及VBA.ModernTheme 的文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 2014-06-03
      • 2023-03-27
      相关资源
      最近更新 更多