Solved

Change the Color of a Command Button with VBA Code

Posted on 2014-04-24
13
2,093 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

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

Suggested Solutions

Title # Comments Views Activity
Help writing a query 6 71
Reset Active Directory Password via MS Access 9 52
Two list boxes - best structure 3 31
can't find file error on web browser 1 15
Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

910 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

18 Experts available now in Live!

Get 1:1 Help Now