How to hide button control in VBA

Is there anyone else who is programming VBA here?

I'm trying to get this code to work

Private Sub button3_click() 'hide main buttons button1.Visible = False button2.Visible = False button3.Visible = False 'show submenu buttons button4.Visible = True; button5.Visible = True; End Sub 

What I am trying to do basically is that I have a main form that has 5 main buttons. 2 of them are hidden at startup. Therefore, when I press button 3, I want to hide the first three main buttons and β€œshow” the other two. When I try to execute this event, I received an error message

" Runtime Error 2165 - You cannot hide the control with focus .

Has anyone encountered this aspect of programming before? I am sure this is doable. I just don’t understand what is wrong here ...

+8
access-vba ms-access button onclick
source share
2 answers

Change focus to one of the visible control before hiding the current

 Private Sub button3_click() 'show submenu buttons button4.Visible = True button5.Visible = True DoEvents 'execute any pending events, to make sure the button 4 and 5 are really visible button4.SetFocus 'change the focus to a now visible control DoEvents 'execute any pending events, to make sure that button4 really has the focus 'now you can hide the other buttons 'hide main buttons button1.Visible = False button2.Visible = False button3.Visible = False End Sub 

Perhaps you can skip the DoEvents command, you should try

+7
source share

I would have thought that the error was fairly obvious. Move the focus to some control that you are not trying to hide before running your code. Also, consider Me : http://msdn.microsoft.com/en-us/library/aa223099(v=office.11).aspx

+1
source share

All Articles