How to loop through controls and forms?

H

Henro Veijer

Heloo,

I have a form with 4 buttons on it.
They are called btn1....4
There are also 4 subforms.
The subform control is called sub1....4 and each has as sourceobject
frm1...4.
I would like to do something like this but I do not know how to loop
through the names of the forms/buttons. Probably just a matter of the
right declarations but which ones?
Who is able and willing to tell me how I can loop through forms/
controls and set their properties?
To be clear: I just need to know how to do the looping., the rest is
clear.........

<code>
x=4 'X = 'the number of buttons and corresponding
suborms
for i = i to x
copy frm1 to [frm & i]
frmmain.[sub & i].form.sourceobject = strSQL
frmmain.[frm & i].form.backcolor = frmmain.[btn & i].backcolor
next
</code>

TIA,

Henro
 
A

Arvin Meyer [MVP]

Henro Veijer said:
Heloo,

I have a form with 4 buttons on it.
They are called btn1....4
There are also 4 subforms.
The subform control is called sub1....4 and each has as sourceobject
frm1...4.
I would like to do something like this but I do not know how to loop
through the names of the forms/buttons. Probably just a matter of the
right declarations but which ones?
Who is able and willing to tell me how I can loop through forms/
controls and set their properties?
To be clear: I just need to know how to do the looping., the rest is
clear.........

<code>
x=4 'X = 'the number of buttons and corresponding
suborms
for i = i to x
copy frm1 to [frm & i]
frmmain.[sub & i].form.sourceobject = strSQL
frmmain.[frm & i].form.backcolor = frmmain.[btn & i].backcolor
next
</code>

TIA,

Henro
 
A

Arvin Meyer [MVP]

Several ways:

For i = 0 to Me.Controls.Count - 1
If TypeOf Me.Controls(I) Is acSubform Then
Me.Controls(i).SourceObject = strSQL 'or whatever else you want to do
End If
Next I

You can also do something like:

Dim frm As Form
Dim ctl as Control

Set frm = Me
For Each ctl In frm.Controls
If TypeOf ctl is acCommandButton Then
ctl.ForeColor = vbRed 'or whatever else you want to do
End If
Next ctl
 
D

Dirk Goldgar

Arvin Meyer said:
If TypeOf Me.Controls(I) Is acSubform Then
[...]
If TypeOf ctl is acCommandButton Then


I think that you've mixed two types of control-type identification. To use
the TypeOf operator, you must compare to a type name, like this:

If TypeOf Me.Controls(I) Is Access.SubForm Then

If TypeOf ctl is Access.CommandButton Then

Alternatively, you can use the control's ControlType property to compare to
one of the enumerated controltype constants, like this:

If Me.Controls(I).ControlType = acSubform Then

If ctl.ControlType = acCommandButton Then
 
H

Henro Veijer

<snip>

gentlemen,

baffled by your knowlegde, rejoiced by your willingness to help me and
utterly cocky showing off the thing that I created thanks to your
advice, I solemnly bow towards you, acknowledge my masters and from
now on will relentlessly avocate my opinion that all answers to all
questions can be found here! (I have only one answer and it is '42')

(but one publicised 'THANK YOU' will have to suffice otherwise my *rse
will be kicked for spamming)

However, what I am trying to say is: THANX!

Greetings form happy Henro
 
A

Arvin Meyer [MVP]

You're right, good catch
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com



Dirk Goldgar said:
Arvin Meyer said:
If TypeOf Me.Controls(I) Is acSubform Then
[...]
If TypeOf ctl is acCommandButton Then


I think that you've mixed two types of control-type identification. To
use the TypeOf operator, you must compare to a type name, like this:

If TypeOf Me.Controls(I) Is Access.SubForm Then

If TypeOf ctl is Access.CommandButton Then

Alternatively, you can use the control's ControlType property to compare
to one of the enumerated controltype constants, like this:

If Me.Controls(I).ControlType = acSubform Then

If ctl.ControlType = acCommandButton Then


--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)
 

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