创建一个类来处理访问表单控件事件 [英] Creating a Class to Handle Access Form Control Events

查看:154
本文介绍了创建一个类来处理访问表单控件事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个将在Access中处理多个控制事件的类。这是为了节省重复打印出许多相同代码的行。

I'm trying to create a Class which will handle multiple Control Events in Access. This is to save the repetition of typing out many lines of identical code.

我已经按照下面的页面上的答案,但是通过一些调整来定制它访问rahter而不是Excel。

I've followed the answer located on the following page, but with a few adjustments to tailor it to Access rahter than Excel.

如何分配多个按钮的常用过程?

我的类代码如下:

Option Compare Database

Public WithEvents ct As Access.CommandButton 'Changed object type to something recognised by Access

Public Sub ct_Click()
    MsgBox ct.Name & " clicked!"
End Sub

我的表单代码如下:

Option Compare Database
Private listenerCollection As New Collection
Private Sub Form_Load()
Dim ctItem
Dim listener As clListener

For Each ctItem In Me.Controls
    If ctItem.ControlType = acCommandButton Then 'Changed to test whether control is a Command Button
        Set listener = New clListener
        Set listener.ct = ctItem
        listenerCollection.Add listener
    End If
Next

End Sub

我已经注意到我已经对(工作)Excel代码进行了更改。我认为问题来自Class中的对象声明。注意:在此过程中不会抛出任何错误;它不会触发事件。

I have noted with comments where I have made changes to the (working) Excel code. I think the problem comes with the object declaration in the Class. Note: no errors are thrown during this procedure; it simply doesn't trigger the event.

提前感谢!

编辑:

我已经将问题缩小到点击事件中没有[事件过程]。如果我手动添加,则类可按预期方式工作。显然,我不想手动添加这些 - 它会击败对象。任何想法我会怎么做?

I've since narrowed the problem down to there being no '[Event Procedure]' in the 'On Click' Event. If I add it manually, the Class works as expected. Obviously, I don't want to have to add these manually - it defeats the object. Any ideas how I would go about this?

推荐答案

在您的OnLoad事件中,您可以添加此行

In your OnLoad event you can add this line

Dim ctItem
Dim listener As clListener

For Each ctItem In Me.Controls
    If ctItem.ControlType = acCommandButton Then 'Changed to test whether control is a Command Button
        Set listener = New clListener
        Set listener.ct = ctItem
        listener.ct.OnClick = "[Event Procedure]"  '<------- Assigned the event handler
        listenerCollection.Add listener
    End If
Next

虽然我不知道这是否比更少的代码,而不仅仅是双击设计器中的OnClick并粘贴方法调用。无论如何,都很酷。

Although I'm not sure if this is more is less code than just double clicking in the OnClick in the designer and pasting in a method call. It's cool regardless.

编辑:
你可以这样改变你的班级

You could change your class like this

Public WithEvents ct As Access.CommandButton 'Changed object type to something recognised by Access

Public Function AddControl(ctrl as Access.CommandButton) as Access.CommandButton
    set ct = ctrl
    ct.OnClick = "[Event Procedure]"
    Set AddControl = ct
End Function

Public Sub ct_Click()
    MsgBox ct.Name & " clicked!"
End Sub

然后在您的表单中,您可以添加像这样的ct

Then in your form you can add a ct like this

For Each ctItem In Me.Controls
    If ctItem.ControlType = acCommandButton Then 'Changed to test whether control is a Command Button
        Set listener = New clListener
        listener.AddControl ctItem
        listenerCollection.Add listener
    End If
Next

现在在类中添加事件处理程序。

Now the event handler is added in the class.

这篇关于创建一个类来处理访问表单控件事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆