Solved

Passing control arrays

Posted on 1998-09-08
4
156 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 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now