VB: Disable all controls on a form

I use this code to cycle through all controls on a form and disable them.  After each control has been disabled, you can then specify individual controls that you wish to be enabled.  This is a handy way to force users to click specific buttons or enter data in specific fields before the rest of the controls on a form become active (enabled).

Private Sub LockControls()
‘ Disable all the controls on the form
Dim
ctrl As Control

For Each ctrl In Me.Controls
ctrl.Enabled = False
Next

Me.txt_Log.Enabled = True ‘ re-enable the log box
Me.progBar.Enabled = True ‘ re-enable the progress bar
Me.lst_ItemGroups.Enabled = True ‘ re-enable the listbox
End Sub

To enable all controls on the form, use the following function:

Private Sub UnlockControls()
‘ Enable all the controls on the form
Dim ctrl As Control

For Each ctrl In Me.Controls
ctrl.Enabled = True
Next
End Sub

‘ Disable all the controls on the form

One comment on “VB: Disable all controls on a form

  1. Hi – I’ve tried to use your standard code here behind an “EDIT” command button in Access, but I keep getting Runtime error “438-Object doesn’t support property/method”.

    In other words, behind the EDIT button OnClick Event, I have:

    Dim ctrl As Control
    For Each ctrl In Me.Controls
    ctrl.Enabled = True
    Next

    In researching properties for a form’s “Control” and “Controls” objects, I don’t see “Enabled” as a valid property, yet I KNOW this is commonly-used code. I’m referencing all the appropriate Access/VB/ActiveX/OLE/DAO libraries in my Access application, so would you have ANY idea what I’m doing wrong? I’m desparate!

    Thanks very much!
    Carla

Leave a comment