PDA

View Full Version : [SOLVED:] Userform: Change Textbox color in loop



BrI
11-19-2017, 12:19 PM
I want to loop through several textboxes on a userform and change the color of each, box names end with 1,2, 3...etc. I can change values of these boxes as follows:


Me.Controls("TextBox_" & i).Value '<--- value works

But variations of the above for backcolor are not working when use a counter, for example not working:


Me.("TextBox_" & i).BackColor = vbYellow '<--- backcolor not working

I have seen some examples using "Got Focus" but need separate Event entry for each textbox and was hoping to avoid this if possible.

Thanks

Paul_Hossler
11-19-2017, 02:55 PM
Just use the same code that you use for the working .Value piece



Me.Controls("TextBox_" & i).BackColor = vbYellow

BrI
11-19-2017, 03:08 PM
Tried that but .Backcolor , among other options are not available

BrI
11-19-2017, 04:58 PM
I believe the problem was related to"For Each" loop used to loop through all controls. In any case does work when use a "For" loop as below which resolves for now.


For i = 1 To 6
Me.Controls("TextBox_" & i).BackColor = vbYellow
Next

Paul_Hossler
11-20-2017, 07:25 AM
If you were using a For Each ... loop, but trying to set"TextBox_" & i then it wouldn't work since i would have what ever was left the last time you used it