• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2468
  • Last Modified:

Change the Color of a Command Button with VBA Code

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
gdunn59
Asked:
gdunn59
  • 7
  • 6
2 Solutions
 
Rey Obrero (Capricorn1)Commented:
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
 
gdunn59Author Commented:
I'm using Access 2010.
0
 
Rey Obrero (Capricorn1)Commented:
try


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

see this sample db
Database001.accdb
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
gdunn59Author Commented:
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
 
Rey Obrero (Capricorn1)Commented:
did you download the sample db ?
0
 
gdunn59Author Commented:
I'm not able to download the database because of HIPAA laws.
0
 
Rey Obrero (Capricorn1)Commented:
did you apply Themes when you created your form?
0
 
gdunn59Author Commented:
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
 
gdunn59Author Commented:
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
 
Rey Obrero (Capricorn1)Commented:
<No I didn't apply themes>

you should apply themes to enable the command buttons to change its color.
0
 
gdunn59Author Commented:
Rey, how do you apply themes?

Thanks,

gdunn59
0
 
Rey Obrero (Capricorn1)Commented:
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
 
gdunn59Author Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now