Solved

Passing control arrays

Posted on 1998-09-08
4
161 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
[X]
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
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
bharris1 earned 50 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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

733 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