Solved

Change the Color of a Command Button with VBA Code

Posted on 2014-04-24
13
2,041 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
  • 7
  • 6
13 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
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
 

Author Comment

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

Accepted Solution

by:
Rey Obrero earned 300 total points
ID: 40021480
try


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

see this sample db
Database001.accdb
0
 

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 119

Expert Comment

by:Rey Obrero
ID: 40023179
did you download the sample db ?
0
 

Author Comment

by:gdunn59
ID: 40023230
I'm not able to download the database because of HIPAA laws.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 119

Assisted Solution

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

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
 

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 119

Expert Comment

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

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

Author Comment

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

Thanks,

gdunn59
0
 
LVL 119

Expert Comment

by:Rey Obrero
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
 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now