Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2015
    Posts
    13

    Unanswered: Problem with function "Initialise Events"

    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?

    You can to see two attachments:

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

    Click image for larger version. 

Name:	Afbeelding1.png 
Views:	4 
Size:	155.5 KB 
ID:	16177 Click image for larger version. 

Name:	Afbeelding2.png 
Views:	2 
Size:	157.5 KB 
ID:	16178

    P.S. I've also placed this post at the other forum. See the link below.

    My post at the other forum

    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

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    I pasted Code1 (Form_Open) in the class module of a form, Code2 (InitialiseEvents) and Code3 (HandleFocus) in a standard module and everything works perfectly.

    What do you mean with "But the problem is that the function InitialiseEvents is not running in these comboboxes."?
    Have a nice day!

  3. #3
    Join Date
    Feb 2015
    Posts
    13
    Dear Sinndho,

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

    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.

    Click image for larger version. 

Name:	Naamloos.jpg 
Views:	3 
Size:	139.5 KB 
ID:	16209

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    1. I don't see why you could not execute the sub Form_Open() from a subform, just place the code in the subform module.

    2. The actual problem, as I can see it, comes from the Public Function HandleFocus():
    Code:
         With Forms(strFormName)(strControlName)
    a) If InitialiseEvents Me was called from the Parent form, strFormName is the name of the Parent. However the control is located on the Child (sub) form.

    b) If InitialiseEvents Me was called from the Child form, strFormName has the correct name. However The is no strFormName is the Forms() collection because strFormName is the name of a form open as a Child (sub) form and such a form is not added to the Forms() collection when it is open: only it's Parent form is.

    3. In such a case, the correct syntax in HandleFocus() should be:
    Code:
         With Forms(strFormName).Controls(strSubFormControlName).Form.Controls(strControlName)
    4. From there, we can conclude that there are at least two ways to properly initialize the Combobox controls:
    a) From the Parent form:
    Code:
    Private Sub Form_Open(Cancel As Integer)
        InitialiseEvents Me.Name, Me.SubFormControlName
    End Sub
    b) From the Child form:
    Code:
    Private Sub Form_Open(Cancel As Integer)
        InitialiseEvents Me.Parent.Name, Me.Name
    End Sub
    5. In both cases, the function Function InitialiseEvents() becomes:
    Code:
    Public Function InitialiseEvents(ParentName As String, ChildName as String)
    
        Dim ctl As Control
    
        For Each ctl In Forms(ParentName).Controls(ChildName).Form.Controls
            With ctl
                If .ControlType = acComboBox Then
                    .OnGotFocus = "=HandleFocus('" & ParentName & "', '" & ChildName & "','" & .Name & "', 'Got')"
                    .OnLostFocus = "=HandleFocus('" & ParentName & "', '" & ChildName & "','" & .Name & "', 'Lost')"
                End If
            End With
        Next ctl
    
    End Function
    And the HandleFocus() function becomes:
    Code:
    Public Function HandleFocus(ByVal strParentName As String, _
                                Byval strChildName 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(strParentName).Controls(strChildName).Form.Controls(strControlName)
    
    ...
    
    End Function
    Notes:
    1. Do not confuse the name of the Child form with the name of the SubForm/SubReport control that contains the Child form in the Parent form. See: http://access.mvps.org/access/forms/frm0031.htm

    2.I tested the proposed solution and it works.
    Have a nice day!

  5. #5
    Join Date
    Feb 2015
    Posts
    13
    Hi Sinndho,

    It works for me now! Thank you very much for your clear explanation which helps a lot. I've learned now how I do it to call the function in the correct way. This is an instructive experience for me. Once again, thank you very much.

    Greeting from Wappervliegje!

  6. #6
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    You're welcome!
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •