Determine Controls Index in a collection

B

Brad

Thanks for taking the time to read my question.

How do I determine a controls number?

For example you could do:
dim x as integer
x = 0
For each control in Me
debug.print me.controls(x).tag
x=x+1
Next

This would iterate through all controls on the form and print out each ones
Tag value. Say I wanted to skip a specific control by using it's number.

so

dim x as integer
x = 0
For each control in Me
If x <> 2 then
debug.print me.controls(x).tag
x=x+1
End If
Next

If I had this in a function and wanted to pass a variable instead of always
skipping 2 but rather skip the number assigned to the variable.

Function Test (SkipThisOne as Integer)
dim x as integer
x = 0
For each control in Me
If x <> SkipThisOne then
debug.print me.controls(x).tag
x=x+1
End If
Next
End Function

How do I get the Index number of a control to pass to the function?

Thanks,

Brad
 
M

Marshall Barton

Brad said:
How do I determine a controls number?

For example you could do:
dim x as integer
x = 0
For each control in Me
debug.print me.controls(x).tag
x=x+1
Next

This would iterate through all controls on the form and print out each ones
Tag value. Say I wanted to skip a specific control by using it's number.

so

dim x as integer
x = 0
For each control in Me
If x <> 2 then
debug.print me.controls(x).tag
x=x+1
End If
Next

If I had this in a function and wanted to pass a variable instead of always
skipping 2 but rather skip the number assigned to the variable.

Function Test (SkipThisOne as Integer)
dim x as integer
x = 0
For each control in Me
If x <> SkipThisOne then
debug.print me.controls(x).tag
x=x+1
End If
Next
End Function

How do I get the Index number of a control to pass to the function?


The index number of a control is an ethereal thing. In some
situations, it can change while you are modifying the form's
design.

Better to use its Name.

Function Test (SkipThisOne As String)
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Name <> SkipThisOne Then
debug.print ctl.Name, ctl.tag
End If
Next
End Function
 
O

OldPro

Thanks for taking the time to read my question.

How do I determine a controls number?

For example you could do:
dim x as integer
x = 0
For each control in Me
debug.print me.controls(x).tag
x=x+1
Next

This would iterate through all controls on the form and print out each ones
Tag value. Say I wanted to skip a specific control by using it's number.

so

dim x as integer
x = 0
For each control in Me
If x <> 2 then
debug.print me.controls(x).tag
x=x+1
End If
Next

If I had this in a function and wanted to pass a variable instead of always
skipping 2 but rather skip the number assigned to the variable.

Function Test (SkipThisOne as Integer)
dim x as integer
x = 0
For each control in Me
If x <> SkipThisOne then
debug.print me.controls(x).tag
x=x+1
End If
Next
End Function

How do I get the Index number of a control to pass to the function?

Thanks,

Brad

I'm not sure I understand what you are after, but let me take a shot.
If a user clicks on a control and you want to have the same set of
code for all of the controls, then put a function in the click event.
Pass the function the number of the control. Don't open up the code
window, just put the function right in the properties window. For
example: =Clicked(5) would go in the lblSomeField click property.
 

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