Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changing a list or array of controls property in a module?

Status
Not open for further replies.

STPMB

Programmer
Sep 13, 2002
28
US
Is it possible to create a list or array of various types of controls in a subprocedure of a form and pass that list or array to a module that will change certain properties of those controls.
I don't want to read through the whole controls collection if possible.
Appreciate any comments.
 
You could create your own collection and just add the controls you want to act on, and then pass this collection to the desired routine(s). The following example was run on a form with 3 text boxes and 3 command buttons. Only the command buttons are added to the custom collection, and therefore only their caption is changed.

Private Sub Form_Load()

Dim colButtons As Collection

Set colButtons = New Collection
colButtons.Add cmdFirst
colButtons.Add cmdSecond
colButtons.Add cmdThird
Call SetButtons(colButtons)

End Sub

Private Sub SetButtons(colCmdButtons As Collection)

Dim ctlButton As CommandButton

For Each ctlButton In colCmdButtons
ctlButton.Caption = "The Same"
Next

End Sub
 
Surely I do not understand.

You want to pass a subset of controls to a procedure -as opposed to itterating through the controls collection?

What gives you the impression / idea that passing the subset of actual controls is 'better' than iterating through the collection? And what is 'better' in this situation?

It appears (to me) that the subset of controls of interest must be dynamically selected, otherwise there is no need to identify them in a calling argument, so their identification must already be in some programatic / procedureal process. Likewise, the property and it's setting must be known, otherwise there is no reason to set it. So, it appears (to me) that when/where the control is identified as a canidate for having a property set, you could quite easily just go ahead and set it.

Alternatively, you mention iterating through the controls collection, as if the controls (or some existing property) include the necessary and sufficient identification for your purpose, if that is true, what is the issue with the iteration approach?



MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
I am trying to do something like this but keep getting a message saying object required. Is there a problem using a variable identified as Control in this manner?

Private Sub Form_Load()
Dim colx As Collection
Set colx = New Collection
colx.Add Combo1
colx.Add Text1
colx.Add DataList1
colx.Add DataGrid
Call changecolor(colx)
End Sub


Public Sub changecolor(x As Collection)
Dim y As Control
For Each y In x
y.BackColor = vbRed
Next
End Sub

 
The various controls are not 'fully qualified', thus the procedure doesn't 'know' what combo1 (etc.) refers to. Something like 'MyFrm.Combo1" or (more easily Me.combo1). But this doesn't really make any more sense than to just pass the control names (and depending on the details of the overall process, the name of the form). Let the second procedure just use the names. It is MY impression that passing the 'control' just passes a reference, so passing the name is not significantly different.

Code:
Private Sub Form_Load()

    Dim colx As Collection

    Set colx = New Collection

    colx.Add "Combo1"
    colx.Add "Text1"
    colx.Add "DataList1"
    colx.Add "DataGrid"

    Call basChgClr(colx)

End Sub
Public Sub basChgClr(x As Collection)

Dim y As Control

    For Each y In x
        Me(Chr(34) & y & Chr(34)).BackColor = vbRed
    Next

End Sub

Although I'm not certain that the quotes (Chr(34) around "y" are required MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top