Easy way to lock all controls on a form

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.
 
M

MacDermott

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.)
 
D

Dirk Goldgar

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
 

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

Similar Threads


Top