Access VBA. How to execute many buttons using a single button?
I have a form with a few开发者_JAVA技巧 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?
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.
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.
All you have to do is call the Click event of the buttons inside the Click event of the one single button.
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.
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
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.
精彩评论