controling control properties

  • Thread starter Thread starter Andy G
  • Start date Start date
A

Andy G

Below is what I'm trying to do. I want to point to a certain control on a
form programatically. I know the code below is crap but it will give you a
good idea of what I'm trying to do. cnt will be a counter (1-20) that will
tell the code which control's back color to change.

Dim cnt As String
Dim box As String
Dim strControl As String

cnt = "1"
box = "Box"
strControl = box & cnt
strControl.BackColor = vbBlue

Thanks.
Andy
 
If you want to use the number of the control:
Dim cnt As Integer
Dim box As String

cnt = 1
box = "Box"
Me.Controls(cnt) = box & Cstr(cnt)
Me.Controls(cnt).BackColor = vbBlue

If you want to address the control by name:
Dim cnt As String
Dim box As String
Dim strControl as String

strControl = "MyControlName"
cnt = "1"
box = "Box"
Me.Controls(strControl) = box cnt
Me.Controls(strControl).BackColor = vbBlue
 
Change Me.Controls(strControl) = box cnt to strControl = box & cnt and it
works great.

Thanks Klatuu
 
Here is some code that will loop through all controls on a form and change
the BackColor. Just let me know if this is not exactly what you are looking
for.

Dim ctlAnycontrol As Control
For Each ctlAnycontrol In Me.Controls
ctlAnycontrol.Properties("BackColor") = 16711808
Next

Dim X As Integer
For X = 0 To Me.Controls.Count - 1
Me.Controls(X).Properties("BackColor") = 16711808
Next X
 
I almost have it working the way I want. One more quick question.

I have a big loop in the OnOpen event of a form. Inside this loop I want to
change the color of these boxes as the records are processed. It seems like
when the myControl.BackColor = vbBlue is ran inside the loop it doesn't have
an event to make it change. The color of the boxes change after a msgbox is
thrown at the end of the loop.

Any ideas?
 
Back
Top