Link to home
Start Free TrialLog in
Avatar of DPPro
DPPro

asked on

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.
Avatar of ameba
ameba
Flag of Croatia image

' 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
Avatar of Kurvy
Kurvy

Control.Lbound
Control.Ubound

These two properties should give you the information
ASKER CERTIFIED SOLUTION
Avatar of caraf_g
caraf_g

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
set caption:
    Me.Controls("Command1", "42").Caption = "Hello"
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

  objControl.Caption = "A C B B A A B A C C"  ' :(
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.

;-)
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.