Results 1 to 5 of 5
  1. #1
    ezybusy is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2015
    Posts
    119

    How to change the back color of all Command Buttons except some few?

    Hi Guys,


    So i was able to come up with the following code to set the back color of all command button on a form except one. And it works.
    Now my problem is, i want the exception to hit more than one button command. I actually have 5 commands which i don't want to be affected by the change in the back color. But i have no clue as to how to go about it.

    Any help???


    Code:
    Const BtBackColor As Long = 3355443 
    
    Dim ctl As Control
    
    If IsNull(Me.SomeTxtField) Then  For Each ctl In Me.Controls
            If ctl.ControlType = acCommandButton Then
               If ctl.Name <> "Cmd_Button1_ToBeExcluded" Then 'I need to add 4 more command buttons here
                  With ctl
                    .BackColor = BtBackColor
                    .ForeColor = vbWhite
                  End With
               End If
            End If
       Next ctl
    End If
    Last edited by ezybusy; 11-15-2017 at 12:31 PM.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Along the lines of:

    Code:
    If ctl.Name <> "Cmd_Button1_ToBeExcluded" AND ctl.Name <> "Cmd_Button2_ToBeExcluded" AND ctl.Name <> "Cmd_Button3_ToBeExcluded" Then
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ezybusy is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2015
    Posts
    119
    @Pbaldy.
    Yeah it did it. Thank you.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Another option would be to set the TAG property of the command buttons to "Exclude" for the buttons you do not want the back color to change.
    Then use this code:
    Code:
        Const BtBackColor As Long = 3355443
    
        Dim ctl As Control
    
        If IsNull(Me.SomeTxtField) Then
            For Each ctl In Me.Controls
                If ctl.ControlType = acCommandButton Then
                    If ctl.Tag <> "Exclude" Then
                        With ctl
                            .BackColor = BtBackColor
                            .ForeColor = vbWhite
                        End With
                    End If
                End If
            Next ctl
        End If
    If at some point you want to change which command buttons get the back color changed, all you have to do is change the TAG property; no need to edit the code.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 03-07-2017, 02:42 AM
  2. Replies: 2
    Last Post: 11-09-2016, 05:54 PM
  3. change color of a buttons
    By azhar2006 in forum Forms
    Replies: 1
    Last Post: 10-29-2014, 06:15 AM
  4. Replies: 8
    Last Post: 04-26-2012, 10:13 AM
  5. Replies: 2
    Last Post: 03-10-2009, 05:14 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums