Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Format controls with focus vba (2000/XP)

    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?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Format controls with focus vba (2000/XP)

    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.)

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Format controls with focus vba (2000/XP)

    As always, your suggestion was exactly what I was after <img src=/S/groovin.gif border=0 alt=groovin width=21 height=21> 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

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Format controls with focus vba (2000/XP)

    That's fine.

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

  5. #5
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Manchester, Gtr Manchester, England
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Format controls with focus vba (2000/XP)

    I really should have noticed that, thanks again <img src=/S/bash.gif border=0 alt=bash width=35 height=39>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •