Lock/Unlock form controls

J

James

Searched group & found no previous post using this simple method.

Trying to resolve a code problem, I'm trying to lock all controls on a form
On Load, and unlock all for an edit, with a check box.
My below first step is giving errors in AC2003.

Private Sub Form_Load()
'Lock All Controls
Dim ctl As Control
For Each ctl In Me.Controls
ctl.Enabled = True <---- Object doesn't support this property or
method.
ctl.Locked = True <---- Object doesn't support this property or
method.
Next
End Sub

I'll also use this method(with False) to lock controls after edit.
Any suggestions appreciated.
 
D

Douglas J. Steele

The Controls collection contains ALL controls, including those like Label
and Line which cannot be enabled.

One approach is to check the type of each control:

Private Sub Form_Load()
'Lock All Controls
Dim ctl As Control

For Each ctl In Me.Controls
If TypeOf ctl Is Textbox Or TypeOf ctl Is Combobox Or _
TypeOf ctl Is Listbox Or TypeOf Is Checkbox Or ... Then
ctl.Enabled = False
ctl.Locked = True
Next ctl

End Sub

Another is to set the Tag property of those controls which you really care
about. Let's say you put "toggle" as the Tag property for those controls.
You'd then use

Private Sub Form_Load()
'Lock All Controls
Dim ctl As Control

For Each ctl In Me.Controls
If ctl.Tag = "toggle" Then
ctl.Enabled = False
ctl.Locked = True
Next ctl

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top