Range of control arrays

What is the most efficient way to loop through a set of control arrays to find out the range of index values?

I want to set the caption on each button on the form from a database. The database is indexed on the control arrays index. I need to know if the controls on a particullar form ranges from lets say 42 through 77 and only look up those values.
DPProAsked:
Who is Participating?
 
caraf_gConnect With a Mentor Commented:
Dim objControl As Control

For Each objControl In Controls
    If objControl.Name = "YourControlArraysName"
        If (objControl.Index >= 42) And (objControl.Index >= 77) Then
            'do something
        End If
    End If
Next
0
 
amebaCommented:
' add control array of 2 command buttons, paste this code
Option Explicit

Private Sub Form_Click()
    Dim ctl As Control
    For Each ctl In Command1
        Print ctl.Index
    Next
End Sub
0
 
KurvyCommented:
Control.Lbound
Control.Ubound

These two properties should give you the information
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
James_stillmanCommented:
If i understand correctly. You might want to try the control array base name (without the (index)) and then use

..ubound = upper bound
..lbound = lower bound
..count = total number of coontrols in array

Hope this helps

Cheers

James
0
 
amebaCommented:
set caption:
    Me.Controls("Command1", "42").Caption = "Hello"
0
 
caraf_gCommented:
Buggy code... sorry

+ another way to set caption.

Dim objControl As Control

For Each objControl In Controls
    If objControl.Name = "YourControlArraysName" Then
        If (objControl.Index >= 42) And (objControl.Index <= 77) Then
            'do something
            'for example
            objControl.Caption = "Hello"
        End If
    End If
Next

0
 
amebaCommented:
  objControl.Caption = "A C B B A A B A C C"  ' :(
0
 
caraf_gCommented:
objControl.Text = "13/14"
>:-(

This is where I have to comment:
DPPro. If you're in ANY way dissatisfied with my answer, even if you think it's too obvious or something... just reject it. PLEASE just reject it.

;-)
0
 
caraf_gCommented:
DPPro, didn't I just ask you to please REJECT my answer if you were in any way dissatisfied with it?

If my answer is not clear or needs more explanation, I would have been happy to explain it further. Instead, you give me a "B".

I think I'll be putting you on my blacklist.
0
All Courses

From novice to tech pro — start learning today.