访问 VBA:许多命令按钮执行相同的代码 [英] Access VBA: many CommandButton execute same code

查看:56
本文介绍了访问 VBA:许多命令按钮执行相同的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有许多按钮的表单.这些按钮中的大多数都有一个 .tag 字段,我可以在其中标识它们的功能(数字字段)

I have a form with many buttons. Most of these buttons have a .tag field where I identify their function (numeric field)

每个按钮在点击时都应该执行相同的代码,将 .tag 值传递给一个函数:

Every button, on click, should execute same code, passing the .tag value to a function:

Private Sub CtlNameA_Click()
   Call WriteTimbraIN(CInt(CtlNameA.tag))
   DoCmd.Close
End Sub

是否有更聪明的方法来做到这一点,而无需在每个按钮中编写完全相同的代码(控件名称除外)?

Is there a smarter way to do it without writing the very same code (except for control name) in every button?

谢谢

推荐答案

可以直接从按钮点击事件中调用表单模块中的函数,并使用Screen.ActiveControl来确定调用按钮.

You can call a function from the form module directly from the button click events, and use Screen.ActiveControl to determine the calling button.

Private Function GenericButton()

    Debug.Print "Called from " & Screen.ActiveControl.Name

    Call WriteTimbraIN(CInt(Screen.ActiveControl.Tag))
    DoCmd.Close

End Function

然后将 =GenericButton() 指定为 OnClick 事件.
您可以通过多选所有按钮然后设置属性来一次性完成此操作.

And then assign =GenericButton() as OnClick event.
You can do this in one go by multi-selecting all buttons and then setting the property.

这篇关于访问 VBA:许多命令按钮执行相同的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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