Problem with function "Initialise Events" (1 Viewer)

Wappervliegje

Registered User.
Local time
Today, 15:50
Joined
Nov 11, 2014
Messages
23
Dear people,

I'm trying to find out how I can get GotFocus and LostFocus of all comboboxes. See below the three codes (code 1, 2 and 3) that I've change from an example from the internet. But the problem is that the function "InitialiseEvents" is not running in these comboboxes. If I try the code 4 as an example, the function "InitialiseEvents" is running perfectly. I don't understand why I can't get GotFocus and LostFocus? I think I'm really forgotten one thing, but I don't know anymore... Please, can you help me? :confused:

You can to see two attachments:

Afbeelding1: The codes 1, 2 and 3 are running.
Afbeelding2: The codes 1, 2 and 4 are running.

Code 1:
Code:
Private Sub Form_Open(Cancel As Integer)
    InitialiseEvents Me
End Sub
Code 2:
Code:
 Public Function InitialiseEvents(frm As Access.Form)
    Dim ctl As Control
        For Each ctl In frm.Controls
            With ctl
                If .ControlType = acComboBox Then
                    .OnGotFocus = "=HandleFocus('" & frm.Name & "', '" & .Name & "', 'Got')"
                    .OnLostFocus = "=HandleFocus('" & frm.Name & "', '" & .Name & "', 'Lost')"
                End If
            End With
        Next ctl
End Function
Code 3:
Code:
 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
Code 4:
Code:
 Public Function InitialiseEvents(frm As Access.Form)
Dim ctl As Control
    For Each ctl In frm.Controls
        With ctl
            If .ControlType = acComboBox Then
                .Enabled = False
            End If
        End With
    Next ctl
End Function
 

Attachments

  • Afbeelding1.jpg
    Afbeelding1.jpg
    92.5 KB · Views: 117
  • Afbeelding2.jpg
    Afbeelding2.jpg
    86.7 KB · Views: 127

CJ_London

Super Moderator
Staff member
Local time
Today, 14:50
Joined
Feb 19, 2013
Messages
16,629
the string you are assigning is incorrect - you need to use double quotes, not single quotes - try manually writing the call and see what happens

.OnGotFocus = "=HandleFocus('" & frm.Name & "','" & .Name & "', 'Got')"
 

Wappervliegje

Registered User.
Local time
Today, 15:50
Joined
Nov 11, 2014
Messages
23
Dear CJ_London,

First I want thank you for your response. I'm sorry for my late reaction, but I'm still sick. :eek:

I see now that I was finally forgotten to let know you that I also want to run the function "InitialiseEvents" in a subform. If the function "InitialiseEvents" is running in a form, it works perfectly. But not in a subform. Do you maybe know what I make wrong with this function? You can see the attachment "Naamloos" about form with a subform. :)
 

Attachments

  • Naamloos.jpg
    Naamloos.jpg
    77.9 KB · Views: 145

CJ_London

Super Moderator
Staff member
Local time
Today, 14:50
Joined
Feb 19, 2013
Messages
16,629
you need to put initialiseevents in the subform as well
 

Wappervliegje

Registered User.
Local time
Today, 15:50
Joined
Nov 11, 2014
Messages
23
I've doing this, but that problem is still same. This is the code I've placed. :confused:

Code:
Private Sub Form_Open(Cancel As Integer)
    InitialiseEvents Me
End Sub
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:50
Joined
Feb 19, 2013
Messages
16,629
I presume the subform has comboboxes and you have fixed the issue per post #2
 

Wappervliegje

Registered User.
Local time
Today, 15:50
Joined
Nov 11, 2014
Messages
23
Dear CJ_London,

I've trying your proposed solution and it doesn't work unfortunately. :( But I've now get a good solution for my problem. See below the link to my post at the other forum. And I want thank you for your help! :D

Have a nice day! :)

My post at the other forum
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:50
Joined
Feb 19, 2013
Messages
16,629
It is very poor etiquette to double post without advising you have done so. Would you like it if someone asked you to research something and after you have done it, then say 'oh - I've got the information somewhere else'. Nobody likes to have their time wasted.
 

Users who are viewing this thread

Top Bottom