Set Enabled On All Controls in a Form?

H

HumanJHawkins

I am trying to set all controls visible, unlocked, and enabled, as the
baseline for disabling or locking down some fields based on other
criteria. Here's what I tried, which gives me an "Invalid or
unqualified reference" error:

Function SetEnabledFieldsBase()
Dim ctl As Control
For Each ctl In Me.Controls
.Enabled = True
.Visible = True
.Locked = False
Next ctl
End Function

I based this on several examples that were intended to do something to
all text boxes on a form. (I have never done this before, so sorry if
this is just really bad syntax.)

Thanks!
 
M

Marshall Barton

HumanJHawkins said:
I am trying to set all controls visible, unlocked, and enabled, as the
baseline for disabling or locking down some fields based on other
criteria. Here's what I tried, which gives me an "Invalid or
unqualified reference" error:

Function SetEnabledFieldsBase()
Dim ctl As Control
For Each ctl In Me.Controls
.Enabled = True
.Visible = True
.Locked = False
Next ctl
End Function

I based this on several examples that were intended to do something to
all text boxes on a form. (I have never done this before, so sorry if
this is just really bad syntax.)


That vode is nice. One thing though, not all controls have
all those properties (e.g. labels) so you need some error
handling to deal with those situations. In a procedure this
simple, I think just adding:
On Error ResumeNext
before the For statement would be sufficient.

It would be better if you could arrange things to avoid this
kind of blanket preperty settings. One way is to use the
Tag property of each control you want to set or reset. E.g.
if the controls you want to disable have D in their Tag
property, then you could modify the procedure to disable
them in the same loop.

Function SetResetControls(grp As String)
Dim ctl As Control
Dim OnOff As Boolean
On Error ResumeNext
For Each ctl In Me.Controls
OnOff = (ctl.Tag = grp)
.Enabled = OnOff
.Visible = OnOff
.Locked = Not OnOff
Next ctl
End Function

You can then have several different groups of controls to
enable/disable just be using a different tag. Using Like
and wildcards instead of = you could eve have overlapping
groups.

Note that if your form has a lot of controls, it would be
faster to use the form's load event to populate a collection
with the controls you want to manipulate. This way the
procedure won't waste time with controls that are never
set/reset.
 
L

Linq Adams via AccessMonster.com

Marsh's suggestion about using the Tag Properties to selectively manipulate
control properties is bang on! The Tag Property is propbably one of the least
known/most under utilized ones out there, and it can be powerful!

If you know what kind of controls you want to manipulate, you can also
stipulate that in your code, like the following, which includes the most
common controls:

Dim ctr As Control
For Each ctr in Me.Controls
Select Case ctr.ControlType
Case acTextBox, acComboBox,acCommandButton, acLabel, acListBox,acTabCtl
'Do your thing here
End Select
Next

Obviously you can include in Line #4 of the code only those type of controls
you want to deal with.
 
H

HumanJHawkins

Thanks to both of you for two good solutions. I used the Select/Case
suggestion and it works beautifully.
 

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