Change font color on ALL form controls on "Got Focus" and "Lost Focus" (1 Viewer)

NigelShaw

Registered User.
Local time
Today, 00:34
Joined
Jan 11, 2008
Messages
1,573
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Hi Chris

just to point out really, the title of your post reflects the answers that you get. If you specifically look for answers relating to "lost focus" and "got focus", then they would generally be the answers you get. It would be quite obvious that asking for these 2 handlers for the 1 object would mean the exact thing which you say is not feasible due to the amount of text boxes.

Not to split hairs or anything lol :eek:

regs

Nidge
 

ChrisO

Registered User.
Local time
Today, 09:34
Joined
Apr 30, 2003
Messages
3,202
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

>>just to point out really, the title of your post reflects the answers that you get.<<

I must confess to not understanding that point. Which title do you mean?
 

NigelShaw

Registered User.
Local time
Today, 00:34
Joined
Jan 11, 2008
Messages
1,573
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Hi

the part that says-
Change font color on ALL form controls on "Got Focus" and "Lost Focus

regs

Nidge
 

ChrisO

Registered User.
Local time
Today, 09:34
Joined
Apr 30, 2003
Messages
3,202
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

That’s not my title, Nigel, that’s the OP’s title.
 

NigelShaw

Registered User.
Local time
Today, 00:34
Joined
Jan 11, 2008
Messages
1,573
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Hi chris

sorry, I picked this up from you eagerly awaiting my routine so assumed it was your post sorry mate

Nigel
 

JPaulo

Developer
Local time
Today, 00:34
Joined
Dec 21, 2009
Messages
185
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

ChrisO, brilliant example, congratulations.
 

ahmedjamalaboelez

Ahmed J. Aboelez
Local time
Yesterday, 16:34
Joined
Feb 25, 2015
Messages
79
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Something to try.

Behind each Form: -

Code:
Option Explicit
Option Compare Text


Private Sub Form_Open(ByRef intCancel As Integer)

    InitialiseEvents Me

End Sub


In a Standard Module: -

Code:
Option Explicit
Option Compare Text


Public Sub InitialiseEvents(ByRef frmThisForm As Form)
    Dim ctl            As Control
    Dim strControlName As String
    
    For Each ctl In frmThisForm
        On Error Resume Next
        ctl.OnGotFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Got')"
        ctl.OnLostFocus = "=HandleFocus('" & frmThisForm.Name & "', '" & ctl.Name & "', 'Lost')"
        Err.Clear
    Next ctl

End Sub


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

    With Forms(strFormName)(strControlName)
        Select Case strChange
            Case "Got"
                .ForeColor = vbBlue
                .FontBold = True
            
            Case "Lost"
                .ForeColor = vbBlack
                .FontBold = False
        End Select
    End With

End Function

Regards,
Chris.

plz Question the Public Sub InitialiseEvents(ByRef frmThisForm As Form)
How could i make for sub form controls >>>>>> in this main form
 

Mile-O

Back once again...
Local time
Today, 00:34
Joined
Dec 10, 2002
Messages
11,316
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

This is the method that I (would) use. I use a variation on it for changing the backcolor property of textboxes and comboboxes, so that the user can see what item the tab is at and needs data input.
 

Attachments

  • Commands.accdb
    428 KB · Views: 166

Zie

New member
Local time
Today, 01:34
Joined
Feb 8, 2018
Messages
3
Re: Change font color on ALL form controls on "Got Focus" and "Lost Focus"

Hello,

I'm new to this forum so I hope it's ok I bring this topic back up. I absolutely LOVE ChrisO's module, I've been looking for something like this for days, so thank you, it's almost exactly what I need.

So here's my question : would there be a way to slightly change the code so that, instead of the control on/off focus, it highlights the label associated with it ? Assuming I use some naming convention, like every label has the name of the control followed by "_lbl" for example, so [Textbox1] is associated with [Textbox1_lbl], [Combo4] with [Combo4_lbl], etc.
Basically, I think I would need to add something like :
Code:
lbl.Name = ctl.Name & "_lbl"
... and then make it so that the changes are applied to it.

I've been trying a few things with no success, I'm not very good with VBA. Can somebody help me please ?

Many thanks !

Also sorry for my poor english, I hope I'm clear enough.

Dave.

You should be able to just add to the list in Function HandleFocus: -

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
Small A2K3 demo attached.

Regards,
Chris.
 

Users who are viewing this thread

Top Bottom