Change font and background on form controls, got focus and lost focus events (1 Viewer)

Hello1

Registered User.
Local time
Today, 02:45
Joined
May 17, 2015
Messages
271
Hi everyone,

I found this older thread Change font color on ALL form controls on "Got Focus" and "Lost Focus" where ChrisO provided a pretty good solution.
The only downsides I came across:
1) This procedure overwrites your current code on Got Focus and Lost Focus events if you have any
2) I didn't manage to make it work on subforms

Regarding the first downside I was wondering if there is a way to capture the code on Got Focus and Lost Focus before entering into Chris' procedure?
Meaning to keep the current code I have there and just add the Got Focus and Lost Focus code from Chirs' procedure. I wonder if there is a way, with the research I did I wasn't quite successful.

As for the second downside I could't make it work on the subforms when I run the main form with them. If I open the subform alone it works.

Hope there are some ideas.

Thanks!
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 20:45
Joined
Feb 19, 2002
Messages
42,981
Since we can't see Chris' code, you will need to post it. Make sure to post the modified version if you made any changes.
 

Hello1

Registered User.
Local time
Today, 02:45
Joined
May 17, 2015
Messages
271
There is also a database sample provided by Chris in the thread I linked above.
InitialiseEvents is being called on form open.

Code:
Option Explicit
Option Compare Text


Public Sub InitialiseEvents(ByRef frmThisForm As Form)
    Dim ctl As Control
    
    On Error Resume Next

    For Each ctl In frmThisForm
        ctl.OnGotFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Got')"
        ctl.OnLostFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Lost')"
    Next ctl
    
    Err.Clear

End Sub


Public Function HandleFocus(ByVal strFormName As String, _
                            ByVal strControlName As String, _
                            ByVal strChange As String)

    Static lngForeColour   As Long
    Static lngFontWeight   As Long
    Static lngBorderStyle  As Long
    Static lngBorderColour As Long
    Static lngBackStyle    As Long
    Static lngBackColour   As Long
    
    On Error Resume Next

    With Forms(strFormName)(strControlName)
        Select Case strChange
            Case "Got"
                [color=green]' Save current configuration.[/color]
                lngForeColour = .ForeColor
                lngFontWeight = .FontWeight
                lngBorderStyle = .BorderStyle
                lngBorderColour = .BorderColor
                lngBackStyle = .BackStyle
                lngBackColour = .BackColor
                
                [color=green]' Set required configuration.[/color]
                .ForeColor = vbBlue
                .FontWeight = 700
                .BorderStyle = 1
                .BorderColor = vbRed
                .BackStyle = 1
                .BackColor = vbYellow
            
            Case "Lost"
                [color=green]' Restore saved configuration.[/color]
                .ForeColor = lngForeColour
                .FontWeight = lngFontWeight
                .BorderStyle = lngBorderStyle
                .BorderColor = lngBorderColour
                .BackStyle = lngBackStyle
                .BackColor = lngBackColour
                
        End Select
    End With
    
    Err.Clear

End Function
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 08:45
Joined
May 7, 2009
Messages
19,169
you just use Conditional formatting, no need any code.
 

Users who are viewing this thread

Top Bottom