The Controls collection is not a member of the Visual Basic Collection class. It has a smaller set of properties and methods than a Collection object ...
These are not my words: they come straight from the source: Microsoft VB6 Language Reference pp192-193. In normal speak: The For ...Each loop may not provide you the expected results when used in connection with the Controls collection. Here's how to solve your problem:
Create a form frmOne with a textbox array (two textboxes), a label and a command button. Add a module to your project, and paste the following code into the module:
Dim frm1 As frmOne, frm2 As frmOne, frm3 As frmOne
Set frm1 = New frmOne
Set frm2 = New frmOne
Set frm3 = New frmOne
Load frm1
frm1.Show
Load frm2
frm2.Show
Load frm3
frm3.Show
ChangeFormColor
Unload frm1
Set frm1 = Nothing
Unload frm2
Set frm2 = Nothing
Unload frm3
Set frm3 = Nothing
End Sub
Private Sub ChangeFormColor()
Dim frm As Form
For Each frm In Forms
frm.BackColor = vbBlue
Dim ctrl As Control
Dim intIndex As Integer
For intIndex = 0 To frm.Controls.Count - 1
Set ctrl = frm.Controls(intIndex)
Select Case (TypeOf ctrl Is TextBox) Or (TypeOf ctrl Is Label)
Case True
ctrl.BackColor = vbRed
Case Else
'do nothing
End Select
Next intIndex
frm.Refresh
Next
'Flush object variable
Set ctrl = Nothing
End Sub
Make sure that the Project Properties set the Public Main() module as Startup Module.
The Main module loads and shows three forms: all are "copies" of the frmOne form. The ChangeFormColor procedure changes the Background color of the forms to blue and the Background color of the TextBoxes and Labels to red. The Background of the Command button remains unchanged
The colors are hardcoded into the procedure, but they can easily be selected through e.g. the Common Dialog Box or other means. The procedure is just meant to show the general outline.
WARNING:
This procedure works to my general satisfaction. However it should not ????? Use it at your discretion???
The same Microsoft Language Reference pp502 hints as a tip: the TypeOf objectname Is objecttype clause can't be used with the Select Case statement.
As you may have guessed, English (neither UK nor US) is NOT my native language and therefore I surely have missed something: the above sample code works perfectly!
Perhaps somebody out there can provide a sensible (and surely) simple explanation.
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]