Highlight Active Control

Pac-Man

Active member
Local time
Today, 11:57
Joined
Apr 14, 2020
Messages
429
Hello,

The attached db contain two class module used to highlight active control with colored border like forms on webpages. Idea to highlight active control originally came from Daniel Pineault (DevHut). I wrote the class modules to get the highlighting of active controls. To use, just declare a form level private variable as Private HAC as New clsHAC_HighlightActiveControl and use following code in form code module:

Code:
Private Sub Form_Close()
    Set HAC = Nothing
End Sub

Private Sub Form_Open(Cancel As Integer)
    HAC.Init Me
End Sub

Known Bugs:
-If a subform (single form view) is used on the form, last control that got focus on the subform retain the highlighting effects even though the focus set to other control on the main form. Sample form to show this effect is given in the the db.

Screenshots:
1688359328358.png
 

Attachments

the same with less code ...
Code:
Public Function FRMControlFocusInOut(strFormName As String, blnGotFocus As Boolean, Optional blnInSubForm As Boolean)
' Highlighting Active Control
' -------------------------------------------------------------------------------------------------/
' Usage:
'   OnGotFocus Control Property  : =FRMControlFocusInOut([Name];-1)
'   OnLostFocus Control Property : =FRMControlFocusInOut([Name];0)
' Usage in subform:
'   OnGotFocus Control Property  : =FRMControlFocusInOut([Name];-1;-1)
'   OnLostFocus Control Property : =FRMControlFocusInOut([Name];0;-1)
' -------------------------------------------------------------------------------------------------/
Const clGotFocusBorderColor = 1643706 ' Red
Const clGotFocusBackColor = 12975359  ' Light Yellow
Const clNormalBorderColor = 10921638  ' Background 1, Darker 35%
Const clNormalBackColor = 16777215    ' Background 1
Dim objCtrl As Control
' -------------------------------------------------------------------------------------------------/
On Error GoTo FRMControlFocusInOut_Err

    If blnInSubForm Then
        Set objCtrl = Screen.ActiveControl
    Else
        Set objCtrl = Forms(strFormName).ActiveControl
    End If
   
    With objCtrl
        Select Case blnGotFocus
            Case True    ' On GotFocus
                .BorderColor = clGotFocusBorderColor
                .BackColor = clGotFocusBackColor
            Case False   ' On LostFocus
                .BorderColor = clNormalBorderColor
                .BackColor = clNormalBackColor
        End Select
    End With
   
    Set objCtrl = Nothing
    Exit Function
' -------------------------------------------------------------------------------------------------/
FRMControlFocusInOut_Err:
    Err.Clear
End Function

FRMControlFocusInOut_2023-07-03_02.png
 

Attachments

OK Eugene I give up, what sort of wizardry have you done on that no code version? Also, what do you mean by almost the same? Update: OK, I see now that the border is not highlighted on the no code version. I thought you were using conditional formatting for the no code version, but I cannot see any defined rules. That's why I'm scratching my head on this. It has to be conditional formatting though.
 
Last edited:
@Eugene-LS, modifying each control seems time consuming and harder one as compared to adding a single line of code in the form OnOpen event. Secondly for using method 1 (the less code one), you can select all controls and set OnGotFocus and OnLostFocus properties to =FnName() to run the code but what if few of the controls already got some code there (like in one of my project whose GUI and data entry is local language, I have code to change keyboard layout on got and lost focus properties) then I have to set the property to "Event Procedure" and then add highlighting code in VBE. I find it easier to just run a single line code in the form OnOpen event which is why I preferred writing the class module.
 
Last edited:
Setting the field back color (Light Yellow)
Except in your example the back color is set to "no color". We're talking about the text box back color right? So I'm still not seeing where the yellow color is coming from. Plus, how do you set the back color to "No Color"? If there was a color setting and I try and go back to No Color, it always changes it to #FFFFFFFE automatically.

1688392749884.png
 
Last edited:
We're talking about the text box back color right?
Yes! - we do.

Step 01
Set BackColor (of selected fields) to #FFFDD1
Step01_2023-07-04_030829.png


Step 02
Set back style (of selected fields) = Transparent
Step02_2023-07-04_030829.png


... And "hocus pocus" is ready. :cool:
Good luck!
 
but what if few of the controls already got some code there
This won't be a problem, since you can also use the function in your code.
Code:
Private Sub ObjectName_GotFocus()
    FRMControlFocusInOut Me.Name, True 'Highlighting Control
    ' your code ...
End Sub

Private Sub ObjectName_LostFocus()
    FRMControlFocusInOut Me.Name, False 'Highlighting Control = Off
    ' your code ...
End Sub
I liked your way of highlighting.
Let everyone choose a solution to their liking ...
 
As explained in post #5, you set a backcolor whilst the back style is set to solid. Then change it to transparent.
When clicked the original back color is shown with no code needed.

Taking it one stage further, use that setting for a default text box control.
You only have to do it once!
See Default Control Properties (isladogs.co.uk)
Then every text box will automatically behave the same on your form (unless you choose to change any of them
 
Thanks for the extra explanation. I have now been able to reproduce the effect. It wasn't clear to me that toggling transparent to solid would reveal the yellow color setting. I always thought that the transparent selection simply overrides whatever color setting there was for backcolor at all times whether control has focus or not. It turns out transparent is only true when the control does not have the focus. Yet again, I learned something new.

What was and is still could be maddening is the fact that toggling transparent to solid in design view (although it does show the yellow color of the background) still says "No Color" in the property field for Back Color. You would think it would show the #FFFDD1 color that it was actually set to, but it does not. That's what was confusing the heck out of me. The Access Back Color property won't update to the true value until the form is cycled to form view and then back to design view. Anyways, I feel enlightened now.
 

Users who are viewing this thread

Back
Top Bottom