Locking all controls on a form

D

Duane Hookom

I usually add the word "lock" in the tag property of all the controls that I
want to lock. Then use code like:

Public Sub FormLock(frmForm as Form, booLock as Boolean)
Dim ctrl as Control
For Each ctrl in frmForm
If ctrl.Tag = "lock" Then
ctrl.Locked = booLock
End If
Next
End Sub

You can then call this function from the On Open event of your form like
FormLock Me , True
 
A

Ayo

Thanks Duane,
I need to lock down the whole form. So what I did was I set the
AllowAdditions, AllowDeletions and AllowEdits property for the form to No in
the form's propety window and now I want to use a check box on the form to
reset these.
I want these properties to be set to Yes when the check box is checked, but
My problem is I can't check the box because it is on the form and it seems
like the form has locked it. Is it possible to use your code to unlock just
the check box so that I can then use the check box to unlock the whole form?
 
D

Duane Hookom

Why not just use the method I suggested? You can select multiple controls at
one time and set the tag property simultaneously.
 
A

Ayo

I just did and I get an error on :
ctrl.Locked=booLock
"Object doesn't support this property or method"
 
A

Allen Browne

The error suggests you are trying to set the Locked property for a control
that does not have a Locked property (such as a label, or line.)

Here's a more involved version of what Duane is suggesting:
Locking bound controls on a form and subforms
at:
http://allenbrowne.com/ser-56.html

Instead of using the Tag property, the code loops through the controls on
your form, identifying those that have a ControlSource (e.g. text boxes and
combos.) It then looks at the Control Source, and figures out which ones are
bound to a table field (excluding unbound, bound to expressions, or bound to
calculated fields.) It then locks/unlocks those controls.

If it finds a subform, the code calls itself recursively, so as to
lock/unlock any controls in the subform as well, and so on to any depth of
nested subforms.

Implementing it is copy'n'paste stuff. You don't need to modify the code to
suit your form; just set paste in the code, and set a couple of properties.

HTH.
 
Top