Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Passing control arrays

Posted on 1998-09-08
Last Modified: 2010-04-30
I want to pass a text box control array to a sub, how do you do it???

Question by:Shaley
  • 2

Accepted Solution

bharris1 earned 50 total points
ID: 1433582
Pass the control to the sub as a Variant.  ie:

Private Sub Form_Click()
    ChangeColor Text1    'Text1 is the name of a control array
End Sub

Public Sub ChangeColor(vSomething As Variant)
Dim i As Long

    For i = 0 To (vSomething.Count - 1)
        vSomething(i).BackColor = vbRed
    Next i
End Sub

Expert Comment

ID: 1433583
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
End Function

Expert Comment

ID: 2640670
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?

Expert Comment

ID: 2662108
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

End Function

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question