Help with looping thru controls on a form

M

MechEngr

Greetings, I am looking for a little help with looping controls in VBA. I
have two loops below... the first one works and the second does not.

This loop in in a module attache to a form and does not use Option Explicit.
this works:

<Dim ctl As Control
Dim temp As String

For Each ctl In Forms!frmMembership_Manager!subfrmFilters.Controls
If ctl.Tag = "FilterCheck" Then ctl.Value = 0
Next ctl>


This loop is in an independent module and does use Option Explicit. This
does not work. I get an error message "Object does not support this Property
or Method". The error occurs in the body of the “For Loopâ€.

<Dim intI As Integer
Dim ctl As Control

For Each ctl In Forms!frmMembership_Manager!subfrmFilters.Controls
If ctl.Tag = "FilterCheck" And ctl.Value = -1 Then intI = intI + 1
Next ctl>

Any help offered is appreciated in advance.
 
D

Douglas J. Steele

For Each ctl In Forms!frmMembership_Manager!subfrmFilters.Form.Controls

Make sure that the name of the control that holds the subform on
frmMembership_Manager is actually named subfrmFilters. Depending on how you
added the subform, the control name may not be the same as the name of the
form being used as a subform. You need to use the name of the control.

Having said that, though, I don't understand why your first statement would
work.

In any case, the reason you're running into problems with the second one is
that not all controls have a Value property. For example, Labels and Lines
don't. You need to check what type of control it is before looking at its
Value property.

If ctl.Tag = "FilterCheck" Then
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox
If ctl.Value = -1 Then intI = intI + 1
End Select
End If
 
M

MechEngr

Works like a charm. I am sure that I will puzzle on why case one still works
(I have not changed it yet, but probbly will).

Thanks very much!
 

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