访问 VBA。如何使用单个按钮执行多个按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1706963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access VBA. How to execute many buttons using a single button?
提问by tksy
I have a form with a few buttons. I want create a button which will execute all the buttons one after the other, after each function of the buttons has completed. Also, I would like to change the colour of the buttons to show which button is being executed. How do I do this?
我有一个带有几个按钮的表单。我想创建一个按钮,在按钮的每个功能完成后,它将一个接一个地执行所有按钮。另外,我想更改按钮的颜色以显示正在执行的按钮。我该怎么做呢?
回答by BenAlabaster
It's good programming practice to not put code inside the event of an object - such as the click event. Instead the code should be put into their own methods which are called by the click event. Then when you need to run a whole bunch of them, you don't need to call the click event of each button, but instead run each of the methods that the click events call directly.
不将代码放在对象的事件中是一种很好的编程习惯 - 例如点击事件。相反,代码应该放在它们自己的方法中,这些方法由 click 事件调用。那么当你需要运行一大堆的时候,你就不需要调用每个按钮的点击事件,而是运行点击事件直接调用的每个方法。
Private Sub DoSomething()
'Code to do something
End Sub
Private Sub DoSomethingElse()
'Code to do something else
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
DoSomething
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs)
DoSomethingElse
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs)
DoSomething
DoSomethingElse
End Sub
This also makes for much more testable code... not that code is particularly testable inside of a Microsoft Office application. But that's a commonly used industry best practice.
这也产生了更多可测试的代码......并不是说代码在 Microsoft Office 应用程序内部特别可测试。但这是常用的行业最佳实践。
回答by Anand Shah
All you have to do is call the Click event of the buttons inside the Click event of the one single button.
您所要做的就是在单个按钮的 Click 事件中调用按钮的 Click 事件。
Also command buttons in Access don't have a BackColor property, the work around would be to use third party tools or You can use a picture and use that as the command button's 'picture' property.
Access 中的命令按钮也没有 BackColor 属性,解决方法是使用第三方工具,或者您可以使用图片并将其用作命令按钮的“图片”属性。
回答by Atmocreations
Private Sub myUltraGroupingButton_Click()
Call cmdButton1_Click()
Call cmdButton2_Click()
Call cmdButton3_Click()
Call cmdButton4_Click()
End Sub
If you want to automate this and if it's a Form, you would need to iterate over Form.Controls
. If it's a worksheet, I think you would need to use Worksheet1.ListObjects
如果你想自动化这个,如果它是一个表单,你需要迭代Form.Controls
. 如果是工作表,我认为您需要使用Worksheet1.ListObjects
回答by tksy
I also got a way to change the color. though i am not changing the color of the button i am able to change at least the color of the caption on teh button using forecolor option.
我也有办法改变颜色。虽然我没有更改按钮的颜色,但我至少可以使用前景色选项更改按钮上标题的颜色。