Link to home
Start Free TrialLog in
Avatar of Shaley
Shaley

asked on

Passing control arrays

I want to pass a text box control array to a sub, how do you do it???

Thanks
ASKER CERTIFIED SOLUTION
Avatar of bharris1
bharris1

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
Avatar of mcix
mcix

Something like this should work

Private Sub Command1_Click()
    PassATexBoxArray Text1
End Sub

Private Function PassATexBoxArray(vvarTextBoxArray As Variant) As Boolean
    Dim mlngMaxTextBoxes As Long
    Dim mlngCurrentTextBox As Long
    mlngMaxTextBoxes = vvarTextBoxArray.Count - 1
    For mlngCurrentTextBox = 0 To mlngMaxTextBoxes
        MsgBox vvarTextBoxArray(mlngCurrentTextBox).Text
    Next
End Function
Please be aware this solution is only valid for control arrays that happen to have sequential indexes starting at 0 (i.e 0, 1, 2, 3...)

If you have a control array with non-sequential indexes (i.e 3, 7, 9, 15) which is completely valid in VB (and often useful), this code will crash!

I am searching for a solution to *that* problem.  It is really a problem of how to loop through non-sequential elements in an array represented by a variant.  With a plain old array you can use LBound and UBound and ignore the invalid index errors with an On Error statement (perhaps there is a better way).  But with the Variant LBound and UBound don't work.

any ideas?
What about this?

Private Sub Command1_Click()
    PassATexBoxArray Text1
   
End Sub

Private Function PassATexBoxArray(ByVal vvarTextBoxArray As Variant) As Boolean
   
    Dim moTextBox  As TextBox
   
    For Each moTextBox In vvarTextBoxArray
        MsgBox moTextBox.Text
    Next

End Function