Userform Lock Objects

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Userform Lock Objects

Post by jstevens »

I have a userform containing option buttons, check boxes, list boxes and command buttons all within a frame. What I would like to do is prevent the user from accidentally selecting something while code is being run. As an example to execute code by selecting a command button which processes code based on a selection of option buttons, check boxes and list boxes.

Is it possible to lock the objects all in one sweep after the command button is clicked or is there a better solution?
Regards,
John

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Userform Lock Objects

Post by HansV »

For example:

Code: Select all

Private Sub CommandButton1_Click()
    Dim ctl As Control
    Dim i As Long
    On Error Resume Next
    For Each ctl In Me.Controls
        ctl.Enabled = False
    Next ctl
    On Error GoTo 0
    ' Your code goes here
    ' ...
    On Error Resume Next
    For Each ctl In Me.Controls
        ctl.Enabled = True
    Next ctl
    On Error GoTo 0
End Sub
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Userform Lock Objects

Post by jstevens »

Hans,

Excellent suggestion.

Thank you.
Regards,
John