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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Controls' BackColor

Status
Not open for further replies.

GoPens

Technical User
Oct 6, 2009
8
US
Hi All,

Attempting to change a control's backcolor via a variable. Calling sub changeBackColors and setting strControlName to current control. Problem is when I attempt to execute Me!strControlName.BackColor = vbYellow, I get a syntax error. I know my syntax at Me!strControlName is wrong, but can't remember how to set this up.

Public Sub changeBackColors(strAction As String)

Dim ctlCurrentControl As Control
Dim strControlName As String

Set ctlCurrentControl = Screen.ActiveControl

strControlName = ctlCurrentControl.Name

MsgBox "strControlName is ... " & strControlName

If strAction = "OnClick" Then
Me!strControlName.BackColor = vbYellow
Else
'Me!strControlName.BackColor = vbWhite
End If

End Sub

Any help would be appreciated.

Thanks,

Tim
 
You use the controls collection
me.controls(strControlName)

but you could simply do

Public Sub changeBackColors(strAction As String)
If strAction = "OnClick" Then
activeControl.BackColor = vbYellow
Else
activeControl.BackColor = vbWhite
End If
End Sub

or

ctlCurrentCtl = activecontrol
ctlCurrentCtl.backcolor
 

0r ...
Code:
[blue]        Me[purple][b]([/b][/purple]strControlName[purple][b])[/b][/purple].BackColor = vbYellow[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Your suggestions were great. Thanks MajP and theAceMan1 for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top