Share via

Highlighting a command button while hovering mouse on another

Anonymous
2016-06-28T20:10:21+00:00

Hello,

I have these command buttons with these captions:

  1. Products
  2. Customers
  3. Sales Invoive records.

I also have another set of command buttons with these captions:

  1. New Products
  2. New Customers and
  3. New Sales Invoies.

the first set is used to view their respective records according to their captions 

while the second set is also used to enter new records according their captions.

they are working very fine but I want the first set to be highlighted with say red color while I hover on the second set. For example, I want "Products" to change its back color to red while I hover on "new products" and so on.

Please help me on how to get this done

Thanks for all your assistance

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Duane Hookom 26,825 Reputation points Volunteer Moderator
2016-06-30T11:49:13+00:00

You can try something like the following. I don't like the duplicated code but it should work.

Private Sub cmdNewCustomers_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim ctl As CommandButton

    Set ctl = Me(cmdNewCustomers.Tag)

    If ctl.ForeColor <> vbRed Then

        ctl.ForeColor = vbRed

    End If

End Sub

Private Sub cmdNewProducts_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim ctl As CommandButton

    Set ctl = Me(cmdNewProducts.Tag)

    If ctl.ForeColor <> vbRed Then

        ctl.ForeColor = vbRed

    End If

End Sub

Sub ResetButtonFore()

    Dim ctl As Control

    For Each ctl In Me.Controls

        If ctl.controlType = acCommandButton Then

            If ctl.ForeColor <> vbBlack Then

                ctl.ForeColor = vbBlack

            End If

        End If

    Next

End Sub

Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    ResetButtonFore

End Sub

Was this answer helpful?

0 comments No comments

13 additional answers

Sort by: Most helpful
  1. Anonymous
    2016-07-04T20:41:50+00:00

    Hello,

    the code is almost the same with the one you sent to me.

    see it below:

    Dim Ctl as CommandButton

      Set Ctl = Me.CustomersRecods 'name of the command button to highlight its color

            If Ctl.BackColor <> VbRedThen

                  Ctl.BackColor = VbRed

            End If

      Set Ctl = Nothing

      Sub resetButtonBackColor  ' function to reset the back color

      Dim Ctl As Control

      For Each Ctl In Me.Controls

           If Ctl.ControlType = AcCommandButton Then

                 If Ctl.BackColor<>VbBlue Then

                        Ctl.BackColor = VbBlue ' original back color of the Command Buttons

                 End If

            End If

      Next Ctl

    I repeat the first code on the mouse event of the respective "Enter New Records" Command Buttons according to the names.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-07-04T19:44:05+00:00

    Hello,

    your suggestion is ok, but I still prefer one express click to enter new records.

    I already have success on this but only hooked up with the issue of highlighting the command buttons to indicate the particular record for which new ones I want to enter.

    Was this answer helpful?

    0 comments No comments
  3. Duane Hookom 26,825 Reputation points Volunteer Moderator
    2016-07-03T17:44:45+00:00

    Please post all of your code to allow us to see exactly what you have.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2016-07-03T15:49:09+00:00

    Rather than having 2 sets of buttons, I'd consider constructing some code that asks the user if they intend to view existing records, or add new one(s), and react accordingly. Does each of these buttons open a corresponding form?

    Just a thought ...

    Something along these lines:

    ============================

    Private Sub cmdOpenCustomersForm_Click()

    Dim Msg As String

    Msg = "" 'Clear the variable

    Dim CR As String

    CR = vbCrLf

    Dim strDocName As String

    strDocName = "frmCustomers"

    Msg = "Would you like to view existing records?" & CR

    Msg = Msg & "OR" & CR

    Msg = Msg & "Enter new Records?" & CR & CR

    Msg = Msg & "[Yes] to view existing, [No] to add new."

    If MsgBox(Msg) = vbYes Then

    DoCmd.OpenForm stDocName

    Else

    DoCmd.OpenForm stDocName, , , , acAdd, acDialog

    End If

    End Sub

    Was this answer helpful?

    0 comments No comments