Access to VBA. How to execute many buttons with one button?

I have a form with several buttons. I want to create a button that will execute all the buttons one by one, after the completion of each function of the buttons. In addition, I would like to change the color of the buttons to show which button is running. How to do it?

+3
source share
4 answers

It is good programming practice not to put code inside an object event - for example, a click event. Instead, you should put the code in your own methods that are called by the click event. Then, when you need to start a whole bunch, you do not need to trigger a click event for each button, but instead run each of the methods that will trigger direct call events.

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

It also makes much more verifiable code ... not the code that is especially tested inside a Microsoft Office application. But this is a common industry practice.

+5
source

All you have to do is trigger the Click event for the buttons inside the Click event of a single button.

, Access BackColor, "".

+1
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 is a form, you will need to iterate Form.Controls. If this is a worksheet, I think you will need to useWorksheet1.ListObjects

0
source

I also have a way to change the color. although I do not change the color of the button, I can at least change the color of the label on the button using the forecolor option.

0
source

Source: https://habr.com/ru/post/1722425/


All Articles