Solved Form Controls (Got & Lost Focus) Events (1 Viewer)

ahmedjamalaboelez

Ahmed J. Aboelez
Local time
Yesterday, 23:48
Joined
Feb 25, 2015
Messages
79
Hi , Good day everyone
I'm Calling following code in Form Open Event So Code set Got and Lost Focus Event For Each Control in the form ,So Each Control On Got Focus Got Bold Font And Reg Border Color , This Code Run in Main Form with no issues, But its not Run in Sub Form Open Event, So Please Any Advice to Run This In Sub Form Too


This Code I Found Many Years Before , While I'm Searching Change Format of form Controls
Click Here To See Original Thread

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"
                ' Save current configuration.
                lngForeColour = .ForeColor
                lngFontWeight = .FontWeight
                lngBorderStyle = .BorderStyle
                lngBorderColour = .BorderColor
                lngBackStyle = .BackStyle
                lngBackColour = .BackColor
     ' Set required configuration.
                .ForeColor = vbBlue
                .FontWeight = 700
                .BorderStyle = 1
                .BorderColor = vbRed
                .BackStyle = 1
                .BackColor = vbYellow
            Case "Lost"
                ' Restore saved configuration.
                .ForeColor = lngForeColour
                .FontWeight = lngFontWeight
                .BorderStyle = lngBorderStyle
                .BorderColor = lngBorderColour
                .BackStyle = lngBackStyle
                .BackColor = lngBackColour
        End Select
    End With
    Err.Clear
End Function
Thank You So Much ,
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:48
Joined
Oct 29, 2018
Messages
21,473
Looks like that code can only handle standalone forms. If you want it to work with subforms too, you would probably want to change the code to use a form object instead of the form's name.
 

ahmedjamalaboelez

Ahmed J. Aboelez
Local time
Yesterday, 23:48
Joined
Feb 25, 2015
Messages
79
Looks like that code can only handle standalone forms. If you want it to work with subforms too, you would probably want to change the code to use a form object instead of the form's name.
I tried to refer to Sub Form then Control , But Still Not Work!
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:48
Joined
Oct 29, 2018
Messages
21,473
I tried to refer to Sub Form then Control , But Still Not Work!
Not sure what you meant by that, but it probably won't work that way in the Open event. The simplest approach, I think, is to do it the way I said. Try changing your code, so it doesn't use the form's name and just use the form as an object instead.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:48
Joined
Feb 28, 2001
Messages
27,186
You might try a couple of changes. I can't change colors in this forum in things with code tags so I will use UPPERCASE to highlight my suggestions:

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(ME, '" & ctl.Name & "', 'Got')"
        ctl.OnLostFocus = "=HandleFocus(ME, '" & ctl.Name & "', 'Lost')"
    Next ctl
    Err.Clear
End Sub

Public SUB HandleFocus(BYREF FX As Access.Form, _
                            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 FX.Controls(strControlName)
...

End Sub

Using object variable frmX as the formal argument for your sub, you just pass the string ME for the actual argument - but it isn't evaluated in the initialization code because you don't want that reference to work until you make the call. Then when you DO make the call, "ME" evaluates to the class object from which the call was made and becomes a short-cut for that object. Inside the sub, you treat it like it was just another form object. This is similar to what you were doing already with your InitializeEvents() call - using a form object and you would probably call that with

Code:
InitializeEvents(Me)

somewhere in the Form_Load routine, right? So to make this work in the sub-form, in the sub-form's Form_Load call your InitializeEvents from there, too.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:48
Joined
May 7, 2009
Messages
19,243
you can make it simpler by using Conditional Format on your Form/Subform:
Code:
Public Sub subFormatCondition(ByRef f As Form)
Dim ctl As Control
With f
    For Each ctl In .Controls
        With ctl
            Select Case TypeName(ctl)
            Case "Textbox", "Combobox", "Listbox"
                With .FormatConditions
                    .Delete
                    With .Add(2)                'field has focus
                        .ForeColor = 16711680   ' blue font
                        .BackColor = 62207      ' on yellow background
                    End With
                End With
            End Select
        End With
    Next
End With
End Sub

call the sub on the Load Event of your Form and Subform:

Private Sub Form_Load()
Call subFormatCondition(Me)
End Sub
 

ahmedjamalaboelez

Ahmed J. Aboelez
Local time
Yesterday, 23:48
Joined
Feb 25, 2015
Messages
79
Thank You Mr. arnelgp
I tried this it works fine But It not Possible to change border style or border Color !!
.BorderColor = 16711680 !!!
you can make it simpler by using Conditional Format on your Form/Subform:
Code:
Public Sub subFormatCondition(ByRef f As Form)
Dim ctl As Control
With f
    For Each ctl In .Controls
        With ctl
            Select Case TypeName(ctl)
            Case "Textbox", "Combobox", "Listbox"
                With .FormatConditions
                    .Delete
                    With .Add(2)                'field has focus
                        .ForeColor = 16711680   ' blue font
                        .BackColor = 62207      ' on yellow background
                    End With
                End With
            End Select
        End With
    Next
End With
End Sub

call the sub on the Load Event of your Form and Subform:

Private Sub Form_Load()
Call subFormatCondition(Me)
End Sub
A.J
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:48
Joined
May 7, 2009
Messages
19,243
Conditional format cannot do that.
what it can do though is even what type
of form you use (single, datasheet, continuous form, split), it
will highlight the current textbox.

unlike your previous code, that will not work on datasheet view.
 

ahmedjamalaboelez

Ahmed J. Aboelez
Local time
Yesterday, 23:48
Joined
Feb 25, 2015
Messages
79
Conditional format cannot do that.
what it can do though is even what type
of form you use (single, datasheet, continuous form, split), it
will highlight the current textbox.

unlike your previous code, that will not work on datasheet view.
yes I really agree with that , I will use first code for main form textboxes , and your code for continuous sub forms and datasheet .
Thank you so much sir 🌹 🌹 🌹 🌹
AJ
 

Users who are viewing this thread

Top Bottom