Easy way to lock all controls on a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All,

Is there an easy way to lock all the controls on a form. Or, alternatively,
is there an easy way to cycle through all the controls on the form (to, say,
set their locked property to true)?

TIA

Nick M.
 
If you really want them all locked (including command buttons), set the
form's AllowEdits property to false.

You can also use code like this:
On Error Resume Next
Dim ctl as Control
For each ctl in Me.Controls
ctl.Locked=true
next

(On Error Resume Next is to handle the errors which will arise because some
controls, e.g. labels, don't have a locked property.)
 
Nick M said:
Hello All,

Is there an easy way to lock all the controls on a form. Or,
alternatively, is there an easy way to cycle through all the controls
on the form (to, say, set their locked property to true)?

Not all controls have a Locked property, so you have to deal with the
error that will be raised if the property doesn't exist:

'----- start of example code ("air code") -----
Sub LockAllControls(frm As Access.Form)

On Error GoTo Err_Handler

Dim ctl As Access.Control

For Each ctl in frm.Controls
ctl.Locked = True
Next ctl

Exit_Point:
Exit Sub

Err_Handler:
If Err.Number = 438 Then
Resume Next
Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End If

End Sub
'----- end of example code -----

You would call this procedure from the form in question with a line like
this:

LockAllControls Me
 
Back
Top