Cycle through Controls on form

G

Guest

Thanks for taking the time to read my question.

Question 1: how do I determine the position or control number of a control
on a form. I can type in me.controls(1) and it will go somewhere, but I'd
like the code to put in the "1" for me. Something like me.controls.number???

Question 2: I'd like to cycle through all the controls on a form where the
Tag <> "Skip"

Code:
For Each c In Me
Debug.Print c.Tag
If c.Tag <> "Skip" Then
Debug.Print c
Me.Controls(c).BackStyle = 1
Debug.Print c
End If
Next
c starts off as 1, but then goes to -1 which is not on the form. What am I
doing wrong?

Thanks,

Brad
 
J

John W. Vinson

Thanks for taking the time to read my question.

Question 1: how do I determine the position or control number of a control
on a form. I can type in me.controls(1) and it will go somewhere, but I'd
like the code to put in the "1" for me. Something like me.controls.number???
Question 2: I'd like to cycle through all the controls on a form where the
Tag <> "Skip"

Code:
For Each c In Me
Debug.Print c.Tag
If c.Tag <> "Skip" Then
Debug.Print c
Me.Controls(c).BackStyle = 1
Debug.Print c
End If
Next
c starts off as 1, but then goes to -1 which is not on the form. What am I
doing wrong?

How (if at all) is c Dim'd? I'd suggest

Dim c As Control

and use

For Each c In Me.Controls

You can't print c (a Control is a complex object, not a value that can be
printed) but you can Debug.Print c.Name.

You can also loop using

Dim iPos As Integer
Dim c As Control
For iPos = 0 to Me.Controls.Count - 1
Set c = Me.Controls(iPos)
...
Next iPos

John W. Vinson [MVP]
 
G

Guest

Perfect! Thanks,

How do I tell a controls number? I'd rather not type in the number.

Thanks,

Brad
 
D

Douglas J. Steele

Why do you need its number?

Me.Controls("NameOfControl") will work just as well.
 

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