Cycling though objects on an Access form

  • Thread starter Thread starter Carl Clarke
  • Start date Start date
C

Carl Clarke

I would like to go through all of the objects on an Access form and
lock/unlock them.
I tried the following (from a book)

Dim ctl As Control
For Each ctl In Me.Controls
ctl.Locked = True
Next ctl

but I get an error mesage that the object doesn't support the property or
method. I am sure this is possible, and would be grateful for a pointer in
the right direction.
Also I believe that I can check for the type of control ?

Thanks in anticipation

Carl Clarke
 
If you will take a look in Visual Basic Help, searching on the keyword
"Locked", you'll see a list of the objects that the property applies to. A
couple of commonly-used controls not on the list are the Label and
CommandButton controls; one of these may be causing the error.

While in VB Help, check out the ControlType property. It lists the
intrinsic constants for controls which can be used to refine your loop, as
in the following (untested) code shows.

Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType <> acLabel then
ctl.Locked = True
End If
Next ctl
 
Your 'cycling' is set up just fine, but you need (as you
mentioned) to check the control type first; you can't
lock such controls as labels, images, lines, etc.

Try changing your code to the following:

Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
ctl.Locked = True
End Select
Next

I've only included the acTextBox and acComboBox control
types; you can find the other legal ones in Help if
you'll look up the ControlType and Locked properties.

HTH
Sheldon Slade
 
Carl Clarke said:
I would like to go through all of the objects on an Access form and
lock/unlock them.
I tried the following (from a book)

Dim ctl As Control
For Each ctl In Me.Controls
ctl.Locked = True
Next ctl

but I get an error mesage that the object doesn't support the property or
method. I am sure this is possible, and would be grateful for a pointer in
the right direction.
Also I believe that I can check for the type of control ?

The error is because not all objects have a Locked property. You can test the object
type, but what I find is easier is to give all of the objects you want to manipulate
a common string in the Tag property and then test for that. This is more flexible
because you might not want to lock or unlock ALL of the objects that have such a
property in some circumstances.

Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Tag = "Lock" Then ctl.Locked = True
Next ctl
 
Thanks for all the help - I've just added the line to check the control type
and it worked !
Having spent over an hour trying to get this going on my own I am really
greteful for the input.

Best Regards

Carl Clarke
 
Back
Top