Solved

Change the Color of a Command Button with VBA Code

Posted on 2014-04-24
13
2,284 Views
Last Modified: 2014-04-28
I have a Combo Box [cboReportCateg] and when a user chooses an option from this combo box, I want a command button [cmdAssocReport] on that same form to change colors (preferably RED).

Also, is there anywhere that provides the codes for all the different colors?

How can I do this with VBA Code?

Thanks,

gdunn59
0
Comment
Question by:gdunn59
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40021440
what version of access are you using?

if you are using version < access 2010
you can use labels instead of command button
labels has click events too.
0
 
LVL 1

Author Comment

by:gdunn59
ID: 40021459
I'm using Access 2010.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 300 total points
ID: 40021480
try


me.cmdAssocReport.backcolor=rgb(255,0,0)

see this sample db
Database001.accdb
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:gdunn59
ID: 40022870
Rey Obrero:

I tried what you suggested, but nothing happened.

Here is the code that I have (I am using it on the AfterUpdate of another field):

cboReportCateg_AfterUpdate

If Me.cboReportCateg = "Associate" Then
    Me.cmdAssocRpt.Enabled = True
    
    Me.cmdAssocRpt.BackColor = RGB(255, 0, 0)

Open in new window



What am I doing wrong?

Thanks,
gdunn59
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40023179
did you download the sample db ?
0
 
LVL 1

Author Comment

by:gdunn59
ID: 40023230
I'm not able to download the database because of HIPAA laws.
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 300 total points
ID: 40023447
did you apply Themes when you created your form?
0
 
LVL 1

Author Comment

by:gdunn59
ID: 40024032
No I didn't apply themes.

Another strange thing.  Since I tried the code to change the color of the command button, now everytime I open that form my SetFocus command is giving me a 2105 run-time error wherein it wasn't doing that before.

Really strange.  I've checked everything and can't figure out why it started doing this.
0
 
LVL 1

Author Comment

by:gdunn59
ID: 40024046
Rey,

Ok.  Ignore my last posting ID: 40024032, for some reason it had put a record source in the Form.  I removed that, and the error went away.

But I'm still having the issue with the command button color not changing by using your code referenced:

me.cmdAssocReport.backcolor=rgb(255,0,0)

I noticed that your code is on the OnClick, mine is on the AfterUpdate of a combo box, and it reference's a command button on the same form, which is the command button that I want to change colors.

Thanks,
gdunn59
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40024064
<No I didn't apply themes>

you should apply themes to enable the command buttons to change its color.
0
 
LVL 1

Author Comment

by:gdunn59
ID: 40026709
Rey, how do you apply themes?

Thanks,

gdunn59
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40027005
in the design view of the form, select the command button then hit F4
in the Format tab, look for the "Use Theme" property and set it to YES
0
 
LVL 1

Author Comment

by:gdunn59
ID: 40027467
Hi Rey,

Thanks.  I figured it out.

Here is what I ended up doing (I actually ended up changing the color to GREEN).

I used the following code on the AfterUpdate of the cboReportCateg Combo Box (this is just part of the code.  I also used the same block of code for the other combo boxes I have in the OnEnter):

If Me.cboReportCateg = "Associate" Then
    Me.cmdAssocRpt.Enabled = True
    Me.cmdAssocRpt.UseTheme = True
    Me.cmdAssocRpt.BackColor = RGB(0, 255, 0)

Open in new window


I also, put the following code in the OnEnter of the cboReportCateg Combo Box):

Private Sub cboReportCateg_Enter()
    
    Me.cboCategSelect.RowSource = ""
    
    Me.cboReportCateg.Dropdown
    
    Me.cmdAssocRpt.Enabled = False
    Me.cmdProcessAvg_Emp.Enabled = False
    Me.cmdDeptReportSummary.Enabled = False
    Me.cmdEmpScorecardRptByDate.Enabled = False
    Me.cmdDetErrRpt.Enabled = False
    Me.cmdMgrErrorDetailRpt.Enabled = False
    Me.cmdMgrRpt.Enabled = False
    Me.cmdNumofAudits.Enabled = False
    Me.cmdTop10.Enabled = False
    
    
    Me.cmdAssocRpt.UseTheme = False
    Me.cmdProcessAvg_Emp.UseTheme = False
    Me.cmdDeptReportSummary.UseTheme = False
    Me.cmdEmpScorecardRptByDate.UseTheme = False
    Me.cmdDetErrRpt.UseTheme = False
    Me.cmdMgrErrorDetailRpt.UseTheme = False
    Me.cmdMgrRpt.UseTheme = False
    Me.cmdNumofAudits.UseTheme = False
    Me.cmdTop10.UseTheme = False
    
End Sub

Open in new window

Thanks for your assistance.  Works exactly like I want it to.

gdunn59
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question