Iterating controls?

Iterating controls?

Anonymous
Not applicable
590 Views
10 Replies
Message 1 of 11

Iterating controls?

Anonymous
Not applicable
Hi guys,
I have a form with about 20 checkboxes on it and I need to perform a
similar action for each box that is checked. Rather than monotonously
using :
if check1.value = true then,,,,,if check2.value=true then
ect., ect,
I'm trying to access them in a loop such as:
dim cbox as checkbox
for each cbox in form1 or for each cbox in form1.controls
or
for I = 0 to form1.controls.count, ect.

The problem is that I cannot access the controls collection in the form
to get a count and a variable defined as a checkbox stays set to nothing
when it hits the loop. I did not create a controls array when I added
these to the form, they're all individuals. Did I mess up? How can I
loop through the checkboxes on a form?
-Josh
0 Likes
591 Views
10 Replies
Replies (10)
Message 2 of 11

Anonymous
Not applicable
Are you using vb or vba? Vba does not use control arrays on forms, so you have to handle it differently. Ed
0 Likes
Message 3 of 11

Anonymous
Not applicable
Josh,

Perhaps this will help.

Dim ctl As Control
Dim chk As CheckBox

For Each ctl In UserForm1.Controls
If TypeOf ctl Is CheckBox Then
Set chk = ctl
Debug.Print chk.Value
End If
Next ctl

Joe

"Minkwitz Design" wrote in message news:3C484C67.5000302@ameritech.net...
> Hi guys,
> I have a form with about 20 checkboxes on it and I need to perform a
> similar action for each box that is checked. Rather than monotonously
> using :
> if check1.value = true then,,,,,if check2.value=true then
> ect., ect,
> I'm trying to access them in a loop such as:
> dim cbox as checkbox
> for each cbox in form1 or for each cbox in form1.controls
> or
> for I = 0 to form1.controls.count, ect.
>
> The problem is that I cannot access the controls collection in the form
> to get a count and a variable defined as a checkbox stays set to nothing
> when it hits the loop. I did not create a controls array when I added
> these to the form, they're all individuals. Did I mess up? How can I
> loop through the checkboxes on a form?
> -Josh
>
0 Likes
Message 4 of 11

Anonymous
Not applicable
Dan,
Use something like the following to iterate all the controls in the form -
select case to deal with the ones you need:
Dim varControl As Variant
Dim objControl As Control
For Each varControl In Me.Controls
Set objControl = varControl
Debug.Print objControl.Name
Next varControl

Mike Weaver

"Minkwitz Design" wrote in message
news:3C484C67.5000302@ameritech.net...
> Hi guys,
> I have a form with about 20 checkboxes on it and I need to perform a
> similar action for each box that is checked. Rather than monotonously
> using :
> if check1.value = true then,,,,,if check2.value=true then
> ect., ect,
> I'm trying to access them in a loop such as:
> dim cbox as checkbox
> for each cbox in form1 or for each cbox in form1.controls
> or
> for I = 0 to form1.controls.count, ect.
>
> The problem is that I cannot access the controls collection in the form
> to get a count and a variable defined as a checkbox stays set to nothing
> when it hits the loop. I did not create a controls array when I added
> these to the form, they're all individuals. Did I mess up? How can I
> loop through the checkboxes on a form?
> -Josh
>
0 Likes
Message 5 of 11

Anonymous
Not applicable
Thanks guys,
That did it. To address the question, I'm using vb 6. Your posts made me
recognize the error of my ways 🙂 Addressing the objects in the form as
by a specific type rather than as a contol (as object) first, then
checking type. I used the method Joe specified since I'm more familiar
with it, with the minor exception that (for future ref) I didn't have to
address the controls collection. ie -

for each ctl in form1
if typeof ctl = checkbox then

Thanks again for the help guys,
-Josh



Joe Sutphin wrote:

> Josh,
>
> Perhaps this will help.
>
> Dim ctl As Control
> Dim chk As CheckBox
>
> For Each ctl In UserForm1.Controls
> If TypeOf ctl Is CheckBox Then
> Set chk = ctl
> Debug.Print chk.Value
> End If
> Next ctl
>
> Joe
>
> "Minkwitz Design" wrote in message news:3C484C67.5000302@ameritech.net...
>
>>Hi guys,
>>I have a form with about 20 checkboxes on it and I need to perform a
>>similar action for each box that is checked. Rather than monotonously
>>using :
>>if check1.value = true then,,,,,if check2.value=true then
>>ect., ect,
>>I'm trying to access them in a loop such as:
>>dim cbox as checkbox
>>for each cbox in form1 or for each cbox in form1.controls
>>or
>>for I = 0 to form1.controls.count, ect.
>>
>>The problem is that I cannot access the controls collection in the form
>>to get a count and a variable defined as a checkbox stays set to nothing
>>when it hits the loop. I did not create a controls array when I added
>>these to the form, they're all individuals. Did I mess up? How can I
>>loop through the checkboxes on a form?
>>-Josh
>>
>>
>
>
0 Likes
Message 6 of 11

Anonymous
Not applicable
You should consider switching to a control array; that will allow you
to use a single routine to handle all the click events.

--
Good judgment comes from experience.
Experience comes from bad judgment.

http://www.acadx.com


"Minkwitz Design" wrote in message
news:3C485632.9030109@ameritech.net...
> Thanks guys,
0 Likes
Message 7 of 11

Anonymous
Not applicable
Josh,

Your choice is identical, it's just implicit using
the Controls collection instead of explicit.

Incidentally, that will not work in VBA, you
must explicitly use the Controls collection.

Joe

"Minkwitz Design" wrote in message news:3C485632.9030109@ameritech.net...
> Thanks guys,
> That did it. To address the question, I'm using vb 6. Your posts made me
> recognize the error of my ways 🙂 Addressing the objects in the form as
> by a specific type rather than as a contol (as object) first, then
> checking type. I used the method Joe specified since I'm more familiar
> with it, with the minor exception that (for future ref) I didn't have to
> address the controls collection. ie -
>
> for each ctl in form1
> if typeof ctl = checkbox then
>
> Thanks again for the help guys,
> -Josh
>
>
>
> Joe Sutphin wrote:
>
> > Josh,
> >
> > Perhaps this will help.
> >
> > Dim ctl As Control
> > Dim chk As CheckBox
> >
> > For Each ctl In UserForm1.Controls
> > If TypeOf ctl Is CheckBox Then
> > Set chk = ctl
> > Debug.Print chk.Value
> > End If
> > Next ctl
> >
> > Joe
> >
> > "Minkwitz Design" wrote in message news:3C484C67.5000302@ameritech.net...
> >
> >>Hi guys,
> >>I have a form with about 20 checkboxes on it and I need to perform a
> >>similar action for each box that is checked. Rather than monotonously
> >>using :
> >>if check1.value = true then,,,,,if check2.value=true then
> >>ect., ect,
> >>I'm trying to access them in a loop such as:
> >>dim cbox as checkbox
> >>for each cbox in form1 or for each cbox in form1.controls
> >>or
> >>for I = 0 to form1.controls.count, ect.
> >>
> >>The problem is that I cannot access the controls collection in the form
> >>to get a count and a variable defined as a checkbox stays set to nothing
> >>when it hits the loop. I did not create a controls array when I added
> >>these to the form, they're all individuals. Did I mess up? How can I
> >>loop through the checkboxes on a form?
> >>-Josh
> >>
> >>
> >
> >
>
0 Likes
Message 8 of 11

Anonymous
Not applicable
Hi Frank,
That's a good point and I'll have to try and remember that for future
reference. But in this instance, I'm not even looking at the events. I
simply have a list of checkboxes and a couple of command buttons. I
check the boxes I want and hit run, then vb accesses a database and runs
a sql query relevant to each box that was checked and fills out word
docs accordingly with the info retrieved.

But that brings up another question. How do you insert the autotext for
"page x of y" in the footer of a word doc from vb/vba?
-Josh

Frank Oquendo wrote:

> You should consider switching to a control array; that will allow you
> to use a single routine to handle all the click events.
>
> --
> Good judgment comes from experience.
> Experience comes from bad judgment.
>
> http://www.acadx.com
>
>
> "Minkwitz Design" wrote in message
> news:3C485632.9030109@ameritech.net...
>
>>Thanks guys,
>>
>
>
>
0 Likes
Message 9 of 11

Anonymous
Not applicable
Hi Joe,
That would explain it. While debugging, I tried it both ways and it
worked in either case. But again that was vb not vba. For the sake of
consistency, I may start adding that so I don't stub my toe attempting
the same thing in vba. Thanks for the clarification.
-Josh

Joe Sutphin wrote:

> Josh,
>
> Your choice is identical, it's just implicit using
> the Controls collection instead of explicit.
>
> Incidentally, that will not work in VBA, you
> must explicitly use the Controls collection.
>
> Joe
>
> "Minkwitz Design" wrote in message news:3C485632.9030109@ameritech.net...
>
>>Thanks guys,
>>That did it. To address the question, I'm using vb 6. Your posts made me
>>recognize the error of my ways 🙂 Addressing the objects in the form as
>>by a specific type rather than as a contol (as object) first, then
>>checking type. I used the method Joe specified since I'm more familiar
>>with it, with the minor exception that (for future ref) I didn't have to
>>address the controls collection. ie -
>>
>>for each ctl in form1
>>if typeof ctl = checkbox then
>>
>>Thanks again for the help guys,
>>-Josh
>>
>>
>>
>>Joe Sutphin wrote:
>>
>>
>>>Josh,
>>>
>>>Perhaps this will help.
>>>
>>> Dim ctl As Control
>>> Dim chk As CheckBox
>>>
>>> For Each ctl In UserForm1.Controls
>>> If TypeOf ctl Is CheckBox Then
>>> Set chk = ctl
>>> Debug.Print chk.Value
>>> End If
>>> Next ctl
>>>
>>>Joe
>>>
>>>"Minkwitz Design" wrote in message news:3C484C67.5000302@ameritech.net...
>>>
>>>
>>>>Hi guys,
>>>>I have a form with about 20 checkboxes on it and I need to perform a
>>>>similar action for each box that is checked. Rather than monotonously
>>>>using :
>>>>if check1.value = true then,,,,,if check2.value=true then
>>>>ect., ect,
>>>>I'm trying to access them in a loop such as:
>>>>dim cbox as checkbox
>>>>for each cbox in form1 or for each cbox in form1.controls
>>>>or
>>>>for I = 0 to form1.controls.count, ect.
>>>>
>>>>The problem is that I cannot access the controls collection in the form
>>>>to get a count and a variable defined as a checkbox stays set to nothing
>>>>when it hits the loop. I did not create a controls array when I added
>>>>these to the form, they're all individuals. Did I mess up? How can I
>>>>loop through the checkboxes on a form?
>>>>-Josh
>>>>
>>>>
>>>>
>>>
>
>
0 Likes
Message 10 of 11

Anonymous
Not applicable
One way would be to just set up a template that you could use when you open a new doc. Ed
0 Likes
Message 11 of 11

Anonymous
Not applicable
I used a similar construction to iterate over labels on a form...I wrote a
mouse over that would underline, and change the color of the label as well
as swap in the "hand" as the mouse cursor (ala internet explorer)...to
differentiate between labels as controls and labels not being used as
controls, I made use of the "tag" property...


"Joe Sutphin" wrote in message
news:23BCAD429EF4520BF82171378672A5B5@in.WebX.maYIadrTaRb...
> Josh,
>
> Your choice is identical, it's just implicit using
> the Controls collection instead of explicit.
>
> Incidentally, that will not work in VBA, you
> must explicitly use the Controls collection.
>
> Joe
>
> "Minkwitz Design" wrote in message
news:3C485632.9030109@ameritech.net...
> > Thanks guys,
> > That did it. To address the question, I'm using vb 6. Your posts made me
> > recognize the error of my ways 🙂 Addressing the objects in the form as
> > by a specific type rather than as a contol (as object) first, then
> > checking type. I used the method Joe specified since I'm more familiar
> > with it, with the minor exception that (for future ref) I didn't have to
> > address the controls collection. ie -
> >
> > for each ctl in form1
> > if typeof ctl = checkbox then
> >
> > Thanks again for the help guys,
> > -Josh
> >
> >
> >
> > Joe Sutphin wrote:
> >
> > > Josh,
> > >
> > > Perhaps this will help.
> > >
> > > Dim ctl As Control
> > > Dim chk As CheckBox
> > >
> > > For Each ctl In UserForm1.Controls
> > > If TypeOf ctl Is CheckBox Then
> > > Set chk = ctl
> > > Debug.Print chk.Value
> > > End If
> > > Next ctl
> > >
> > > Joe
> > >
> > > "Minkwitz Design" wrote in message
news:3C484C67.5000302@ameritech.net...
> > >
> > >>Hi guys,
> > >>I have a form with about 20 checkboxes on it and I need to perform a
> > >>similar action for each box that is checked. Rather than monotonously
> > >>using :
> > >>if check1.value = true then,,,,,if check2.value=true then
> > >>ect., ect,
> > >>I'm trying to access them in a loop such as:
> > >>dim cbox as checkbox
> > >>for each cbox in form1 or for each cbox in form1.controls
> > >>or
> > >>for I = 0 to form1.controls.count, ect.
> > >>
> > >>The problem is that I cannot access the controls collection in the
form
> > >>to get a count and a variable defined as a checkbox stays set to
nothing
> > >>when it hits the loop. I did not create a controls array when I added
> > >>these to the form, they're all individuals. Did I mess up? How can I
> > >>loop through the checkboxes on a form?
> > >>-Josh
> > >>
> > >>
> > >
> > >
> >
>
>
0 Likes