change color of the command buttons (1 Viewer)

wire_jp

Registered User.
Local time
Yesterday, 16:25
Joined
Jun 23, 2014
Messages
64
Hello Everyone,

I am using Microsoft Access 2010 software. How do you change the color of the command buttons (fore color/back color) and the navigation tabs of a navigation menu in an entire database on a fly (i.e. automatically)?

Thank you in advance.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:25
Joined
May 7, 2009
Messages
19,247
i think you can use the Theme button on the Ribbon.
 

Mark_

Longboard on the internet
Local time
Yesterday, 16:25
Joined
Sep 12, 2017
Messages
2,111
Can you please explain what you are trying to do?

i.e. change colour for buttons that are not available or change colour if certain user(s) log in?
 

isladogs

MVP / VIP
Local time
Today, 00:25
Joined
Jan 14, 2017
Messages
18,246
Please do explain what the purpose of this request?

Anyway, as Arnelgp said, you could change the theme using code when your trigger event occurs.
Or you could assign the same tag value (e.g. C) to all buttons you want to change, then use code similar to this (air code)

Code:
Dim ctl as Control
For each ctl in Me.Controls
      If ctl.Tag ="C" Then Me.ctl.Forecolor = vbBlue
Next
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:25
Joined
Feb 28, 2001
Messages
27,216
A command button is just like any other control except it does not have an associated value (i.e. it is NOT a text box, list box, check box, or combo box). While it is true that command buttons have special visual handling due to the special effects (i.e. sunken or raised that changes when pressed/clicked), they are otherwise just like a fancy unbound label. They have a .Caption (to which .Forecolor applies). They can have a .Backcolor and a .Border color, so you can diddle with those properties as well. They have shading and style options for those features as well. HOWEVER, they have a property that is often forgotten, a Boolean called .UseTheme, which acts as an override for the more normal foreground, background, and border settings.
 

wire_jp

Registered User.
Local time
Yesterday, 16:25
Joined
Jun 23, 2014
Messages
64
Hello Everyone,

Thank you for your responses. I have developed a Microsoft Access 2010 application which uses a switchboard with some command buttons on it e.g. Enter Here, Contact Us, Address Book. If the user clicks on the Enter Here command button, it opens a navigation form which has a main menu and submenus: e.g. if an user click on the (i) Report Tab in the main menu: it would contain the submenu listing of all of the reports: Purchase List Report, Sales List Report and Wish List Report or (ii) click on the Find Item Tab in the main menu: it would contain the LookUp form or the Search form.

So I would like the user to click on a command button on the Switchboard board to change the color of the Switchboard command buttons, the ForeColor/BackColor of the command buttons, change the color of the navigation tab controls and the color of the text in the labels on the forms. of the navigation menu.

Thanks
 

wire_jp

Registered User.
Local time
Yesterday, 16:25
Joined
Jun 23, 2014
Messages
64
I forgot to mention that the Microsoft Access navigation ribbon is disabled so that users cannot use it (the users can only use the customized navigation controls on the Switchboard).
 

isladogs

MVP / VIP
Local time
Today, 00:25
Joined
Jan 14, 2017
Messages
18,246
So either use themes or try my suggestion using tag property from post #4
 

wire_jp

Registered User.
Local time
Yesterday, 16:25
Joined
Jun 23, 2014
Messages
64
Thank you for the feedback and the answers
 

wire_jp

Registered User.
Local time
Yesterday, 16:25
Joined
Jun 23, 2014
Messages
64
Hi Everyone,

I appreciate all of your help. I have a standard module called modTextColorChange which allows the user to change the colour of the text labels. I just make my changes to the colour code (i.e. decimal color code) in the module and run the module to update the chosen color in the entire database. However, the idea is for any user to customize their colours to suit their own liking (and I do not want users to tinker with the colour selection in the module). Rather, I would like to have a command button on the switch board which an user can click, and then a colour picker dialog box appears and the user will change the text colour. The code for the ModTextColorChange:

Code:
Option Compare Database
Option Explicit


Sub ReformatAllForms()
    Dim strControl As String, lngColor As Long
    Dim frm As Object
            
'   Change these two values as needed:
    strControl = "label" '(Can also be "textbox")
    lngColor = 65450 'Or any valid color value (not Hex)
        
'   Loop through every form in the database
    For Each frm In CurrentProject.AllForms
        
'       Change all of the controls that are labels to have red text
'       (or whatever you've specified above)
        SetControlColor frm.Name, strControl, lngColor
    Next
End Sub

Sub SetControlColor(strFormName As String, strControlType As String, lngColor As Long)
    Dim ctl As Control
    
'   Open the target form in design mode, and hidden
    DoCmd.OpenForm strFormName, acDesign, , , , acHidden
'   Loop through all of the controls on the form
    For Each ctl In Forms(strFormName).Controls
        

        If strControlType = "label" And ctl.ControlType = acLabel Or _
           strControlType = "textbox" And ctl.ControlType = acTextBox Then
'           Change the label color
            ctl.ForeColor = lngColor
'           This could be made into an Argument like the color/control type, if necessary
            ctl.FontName = "Calibri (Detail)"
        End If
    
    Next
    
'   Close & Save form
    DoCmd.Close acForm, strFormName, acSaveYes
    
'   List the reformatted forms in the Immediate Window
    Debug.Print "Form " & strFormName & " design has been updated."
    
End Sub

Similarly I would like to use a command button to activate a colour picker to change the colour of the command buttons: -

Code:
Option Compare Database

Sub SetCtlProps(frm As Form)
    Dim ctl     As Control
        
    For Each ctl In frm.Controls
        Select Case ctl.ControlType
            Case acCommandButton, acToggleButton
                ctl.ForeColor = 0
                ctl.BackColor = 16777215
                ctl.UseTheme = False
            Case acTabCtl
                ctl.BackColor = 15777815
        End Select
    Next ctl
End Sub

Lastly, I have a module which allows a user to change the background image and I would like to add a command button on the switchboard, which will allow the user to open the image from a folder in the computer and insert it into the background. Right now, I just change the image directory path in the module and run the module to update the background image in the database. The only problem is that once the image has been updated, the switchboard form do not reload (I normally have to Compact & Repair the Database (my way of "refreshing" the database), to reload the switchboard form after the changes. Ideally, I would like to have a command button on the switchboard so that the user can click on it, select a background image and load the background image. The code for the modBackgroundImageChange is: -

Code:
Option Compare Database
Option Explicit


Function BackgroundPictureChange() As Boolean

Dim intNumOfForms As Integer
Dim intFrmCtr As Integer
Dim strFormName As String
 
intNumOfForms = CurrentDb.Containers("Forms").Documents.Count
 
For intFrmCtr = 0 To intNumOfForms - 1
  strFormName = CurrentDb.Containers("Forms").Documents(intFrmCtr).Name
    DoCmd.OpenForm strFormName, acDesign, , , acFormEdit, acHidden
      Forms(strFormName).Picture = "C:\My Documents\Image Filename.png"
      Forms(strFormName).PictureSizeMode = 1    'Stretch
        DoCmd.Close acForm, strFormName, acSaveYes
Next

BackgroundPictureChange = True

End Function
 

isladogs

MVP / VIP
Local time
Today, 00:25
Joined
Jan 14, 2017
Messages
18,246
Suggest you do a thorough search of this and other forums as I've tried an example database which does almost exactly what you want.

Unfortunately I didn't keep it as I thought the idea was better than the implementation of it.

Personally I'd stick with themes
 

wire_jp

Registered User.
Local time
Yesterday, 16:25
Joined
Jun 23, 2014
Messages
64
Hi Colin,

Thank you for the information and advice.
 

wire_jp

Registered User.
Local time
Yesterday, 16:25
Joined
Jun 23, 2014
Messages
64
Hi,

On a follow-up note, I found this sample database by Stephen Lebans which changes the backcolor of the command buttons: http://www.lebans.com/cmdbutton.htm. It works fine, as a colour picker pops-up and I can change the colour of the command buttons. The only problem occurs when I close the form and I reopen the form, the backcolor of the command buttons reverts back to their original color.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:25
Joined
Feb 28, 2001
Messages
27,216
That behavior (reverting to original color) occurs because you probably didn't SAVE the form while it had the updated colors. So the controls revert to their last saved colors. That is a technically correct situation according to the published behavior of Access forms.
 

wire_jp

Registered User.
Local time
Yesterday, 16:25
Joined
Jun 23, 2014
Messages
64
Hi Doc Man,

I did save my database form and the colours still revert back to the original colours (I also saved the colour change in the sample database, and when I reopened the form, the colour reverted back to the original colour).
 

JHB

Have been here a while
Local time
Today, 01:25
Joined
Jun 17, 2012
Messages
7,732
How do you save it?
Do you've any code running, (Events: OnLoad, OnOpen, OnCurrent), that set the colour back.
If you can't get it post your database with some sample data, + description how to reproduce what you describe!
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:25
Joined
Feb 28, 2001
Messages
27,216
Interesting. I've not seen that behavior. But generally speaking, that can't happen unless the color to which it reverts is the Windows "Theme" color. By any chance is the color to which it reverts consistent with the Windows Theme you have chosen for your other windows? Knowing that much might help us figure out whether your problem is a theme issue or something else. Command buttons are notorious regarding retention of themes.
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 19:25
Joined
May 21, 2018
Messages
8,552
To persist the changes you have to open in design view. The following works for me changing every control on every form

Code:
Private Sub cmdOk_Click()
  Dim frmObj As Access.AccessObject
  Dim frm As Access.Form
  Dim frmName As String
  For Each frmObj In CurrentProject.AllForms
     frmName = frmObj.Name
     If Not frmName = "FrmChangeColor" Then
        DoCmd.OpenForm frmName, acDesign, , , , acHidden
        Set frm = Forms(frmName)
        ChangeControls frm, Me.TxtForeColor, Me.TxtBackColor
        DoCmd.Close acForm, frmName, acSaveYes
     End If
  Next frmObj
  DoCmd.Close acForm, Me.Name
  DoCmd.OpenForm "Home", , , , , acDialog
  
End Sub

Public Sub ChangeControls(frm As Access.Form, TheForeColor, TheBackColor)
  'Not all controls support the property
  On Error Resume Next
  Dim ctrl As Access.Control
  For Each ctrl In frm.Controls
    ctrl.ForeColor = TheForeColor
    ctrl.BackColor = TheBackColor
  Next ctrl
 
End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 19:25
Joined
May 21, 2018
Messages
8,552
Let me caveat. That is only for demonstration you would never do it that way. You would make a table and save those values to a table.

Code:
TblSettings
  PropertyName
  PropertyValue

Then you would make wrapper functions

Code:
public Function getForecolor ()
  dlookup to return forecolor
end function

Code:
Public Function getBackColor()
  dlookup to get backcolor
end function

or simply

Code:
Public function getPropertySetting(TheSettingName as string, TheValue as string) as string 
  dlookup to get the value
end function

Then in the on load of each form you do something like

Code:
dim fc as long
dim bc as long
fc = clng(getForecolor)
bc = clng(getBackColor
changeControls me,fc,bc

So you persist the values in a table, but change them dynamically. Also you would realistically tag the controls you want changed and check for the tag.
 

wire_jp

Registered User.
Local time
Yesterday, 16:25
Joined
Jun 23, 2014
Messages
64
Hi Everyone,

Thank you for the feedback and the help. I have attached a sample database. The switchboard contains the command buttons which are giving me trouble.

@ MajP: I was trying to implement the code which you provided, but I am getting an error for the Me.txtForeColor as it is showing as an Invalid use of Me keyword.


Thanks,

wire_jp
 
Last edited:

Users who are viewing this thread

Top Bottom