[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Passing control arrays

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

Thanks
0
Comment
Question by:Shaley
  • 2
4 Comments
 
LVL 1

Accepted Solution

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

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

IN MODULE:
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
0
 
LVL 4

Expert Comment

by:mcix
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
    Next
End Function
0
 

Expert Comment

by:jleinbach
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?
0
 
LVL 4

Expert Comment

by:mcix
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
    Next

End Function
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

834 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