Looping through controls

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

Guest

I am lookping through all of the controls on my form and disabling them with the following code

Set frm = M
For Each ctl In fr
ctl.Enabled = Fals
Next ct

This works fine with the exception that I get a 438 error when it hits a control that doesn't have this property. I can trap the 438 error and resume. However, I was wondering if there is a way to just loop through the fields and not the labels or other items that might be causing this error. Thanks in advance.
 
SHIPP said:
I am lookping through all of the controls on my form and disabling them with the following code:

Set frm = Me
For Each ctl In frm
ctl.Enabled = False
Next ctl

This works fine with the exception that I get a 438 error when it hits a control that doesn't have this property. I can trap the 438 error and resume. However, I was wondering if there is a way to just loop through the fields and not the labels or other items that might be causing this error. Thanks in advance.


The cheap way is to use an On Error Resume Next before the
loop.

On Error Resum Next
For Each ctl In Me
ctl.Enabled = False
Next ctl
On Error GoTo ErrHandler

Another way is to test the ControlType property:

For Each ctl In Me
Select Case ctl.ControlType
Case acTextbox, acComboBox, ...
ctl.Enabled = False
End Select
Next ctl

Because I almost never want to disable absolutely every
enabled control on a form, the techique I prefer is to put a
key string (e.g. GrpA) in the Tag property of those controls
I do want to disable:

For Each ctl In Me
If ctl.Tag = "GrpA" Then
ctl.Enabled = False
End If
Next ctl
 
One way (in addition to the other suggestions) would be to perform an
ordinary loop once, adding the names of the relevant controls to a
Collection. Then next time(s), loop through the Collection (which is now
known to contain only the controls that you want).

HTH,
TC


SHIPP said:
I am lookping through all of the controls on my form and disabling them with the following code:

Set frm = Me
For Each ctl In frm
ctl.Enabled = False
Next ctl

This works fine with the exception that I get a 438 error when it hits a
control that doesn't have this property. I can trap the 438 error and
resume. However, I was wondering if there is a way to just loop through the
fields and not the labels or other items that might be causing this error.
Thanks in advance.
 
Another way....

Do a search in HELP for ControlType .

There is an example of looping thru controls and setting properties. You could use a Select Case ...End select to set the controls have the .Enabled property


Stev
-------------------------------
"Veni, Vidi, Velcro
(I came; I saw; I stuck around.
----- SHIPP wrote: ----

I am lookping through all of the controls on my form and disabling them with the following code

Set frm = M
For Each ctl In fr
ctl.Enabled = Fals
Next ct

This works fine with the exception that I get a 438 error when it hits a control that doesn't have this property. I can trap the 438 error and resume. However, I was wondering if there is a way to just loop through the fields and not the labels or other items that might be causing this error. Thanks in advance.
 
Back
Top