• Format controls with focus vba (2000/XP)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Format controls with focus vba (2000/XP)

    • This topic has 8 replies, 2 voices, and was last updated 21 years ago.
    Author
    Topic
    #403310

    I need to format about 20 check boxes on a form so the user can see easily when each has the focus, I’ve applied (and removed) a bold drop shadow using the got focus/lost focus events but was wondering if I could wrap the code in a separate sub so I can re-use it for all the check boxes (and no-doubt easily change the color when someone doesn’t like it).

    Here’s the simple code so far

    Private Sub chkQuestion1_GotFocus()
    With chkQuestion1
    .SpecialEffect = 4
    .BorderWidth = 3
    .BorderColor = RGB(176, 255, 184)
    End With
    End Sub

    Private Sub chkQuestion1_LostFocus()
    With chkQuestion1
    .SpecialEffect = 0
    .BorderWidth = 0
    .BorderColor = RGB(176, 255, 184)
    End With
    End Sub

    Instead, I wanted to pass the control name to another sub and use after the ‘With’ word but couldn’t get it to work. Any ideas?

    Viewing 1 reply thread
    Author
    Replies
    • #810472

      Try this:

      Private Function HandleGotFocus()
      With Me.ActiveControl
      .SpecialEffect = 4
      .BorderWidth = 3
      .BorderColor = RGB(176, 255, 184)
      End With
      End Function

      Private Function HandleLostFocus()
      With Me.ActiveControl
      .SpecialEffect = 0
      .BorderWidth = 0
      .BorderColor = RGB(176, 255, 184)
      End With
      End Function

      You can handle the events in two different ways:

      1. Select all check boxes on the form. Enter =HandleGotFocus() directly in the On Got Focus event in the Event tab of the Properties window (instead of [Event Procedure]), and =HandleLostFocus() directly in the On Lost Focus event.

      – or –

      2. Create event procedures for each of the check boxes:

      Private Sub chkQuestion1_GotFocus()
      HandleGotFocus
      End Sub

      Private Sub chkQuestion1_LostFocus()
      HandleLostFocus
      End Sub

      and repeat for chkQuestion2 … chkQuestion20. It is more work, but the code is more self-documenting. (It is easy to overlook a function directly entered in the Properties window, so someone else looking at the design might be confused for a while.)

      • #810482

        As always, your suggestion was exactly what I was after groovin thankyou.

        I modified the code to apply a highlight to text and combo boxes, does this code seem reasonable?

        Private Function HandleGotFocus()

        Dim ctl As Control
        Set ctl = Me.ActiveControl
        With ctl
        If ctl.ControlType = acCheckBox Then
        .SpecialEffect = 4
        .BorderWidth = 3
        .BorderColor = RGB(176, 255, 184)
        ElseIf ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
        .BackColor = RGB(176, 255, 184)
        Else
        End If
        End With

        Set ctl = Nothing

        End Function

        Private Function HandleLostFocus()

        Dim ctl As Control
        Set ctl = Me.ActiveControl

        With ctl
        If ctl.ControlType = acCheckBox Then
        .SpecialEffect = 0
        .BorderWidth = 0
        .BorderColor = RGB(176, 255, 184)
        ElseIf ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
        .BackColor = RGB(255, 255, 255)
        Else
        End If
        End With

        Set ctl = Nothing

        End Function

        • #810490

          That’s fine.

          Note: within the With ctl … End With blocks, you don’t need to use ctl.ControlType, you can use .ControlType instead.

        • #810491

          That’s fine.

          Note: within the With ctl … End With blocks, you don’t need to use ctl.ControlType, you can use .ControlType instead.

      • #810483

        As always, your suggestion was exactly what I was after groovin thankyou.

        I modified the code to apply a highlight to text and combo boxes, does this code seem reasonable?

        Private Function HandleGotFocus()

        Dim ctl As Control
        Set ctl = Me.ActiveControl
        With ctl
        If ctl.ControlType = acCheckBox Then
        .SpecialEffect = 4
        .BorderWidth = 3
        .BorderColor = RGB(176, 255, 184)
        ElseIf ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
        .BackColor = RGB(176, 255, 184)
        Else
        End If
        End With

        Set ctl = Nothing

        End Function

        Private Function HandleLostFocus()

        Dim ctl As Control
        Set ctl = Me.ActiveControl

        With ctl
        If ctl.ControlType = acCheckBox Then
        .SpecialEffect = 0
        .BorderWidth = 0
        .BorderColor = RGB(176, 255, 184)
        ElseIf ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
        .BackColor = RGB(255, 255, 255)
        Else
        End If
        End With

        Set ctl = Nothing

        End Function

    • #810473

      Try this:

      Private Function HandleGotFocus()
      With Me.ActiveControl
      .SpecialEffect = 4
      .BorderWidth = 3
      .BorderColor = RGB(176, 255, 184)
      End With
      End Function

      Private Function HandleLostFocus()
      With Me.ActiveControl
      .SpecialEffect = 0
      .BorderWidth = 0
      .BorderColor = RGB(176, 255, 184)
      End With
      End Function

      You can handle the events in two different ways:

      1. Select all check boxes on the form. Enter =HandleGotFocus() directly in the On Got Focus event in the Event tab of the Properties window (instead of [Event Procedure]), and =HandleLostFocus() directly in the On Lost Focus event.

      – or –

      2. Create event procedures for each of the check boxes:

      Private Sub chkQuestion1_GotFocus()
      HandleGotFocus
      End Sub

      Private Sub chkQuestion1_LostFocus()
      HandleLostFocus
      End Sub

      and repeat for chkQuestion2 … chkQuestion20. It is more work, but the code is more self-documenting. (It is easy to overlook a function directly entered in the Properties window, so someone else looking at the design might be confused for a while.)

    Viewing 1 reply thread
    Reply To: Format controls with focus vba (2000/XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: