Search code examples
excellockedvba

Toggling locked and unlocked toolbox elements in VBA


I'm currently trying to toggle a 'Locked' element so that it is no longer locked once a check is performed. In the properties of the button, I have set the default value to be 'Locked' so that the user cannot use it's functionality.

When another button is clicked, my macro runs a process and if the process returns true then then the other 'Locked' button should be 'Unlocked' and a textbox containing a name should be 'Locked' to prevent alteration. The processing is done in a module called 'PNC_Module2' and the form that I wish to lock and unlock elements on is called 'folder_creator_window'.

I'm currently using the following lines to try and achieve the 'Lock and Unlock' but it doesn't seem to be doing anything:

    'This is supposed to unlock the create_folders_button
    folder_creator_window.create_folders_button.Locked = False  

    'This is supposed to lock the p_name_textbox textbox
    folder_creator_window.p_name_textbox.Locked = True

I haven't included the rest of the code as it has no real baring on what occurs here.


Solution

  • Seeing the rest of your code may reveal the problem as your syntax looks ok

    But on your quest as asked The normal method of toggling is to reverse the current condition using NOT. ie to toggle Locked on CommandButton1 on UserForm1 you would use:

    UserForm1.CommandButton1.Locked = Not UserForm1.CommandButton1.Locked