Order of Events Challenge

dgreen

Member
Local time
Today, 12:26
Joined
Sep 30, 2018
Messages
397
I have a form, that according to my new friend "F8", is going from the form load below into a specific field LostFocus event to do a spell check. I don't want this to happen. Thoughts on what might be causing it and how to stop it? What happens next is even more bizarre is that it then goes to another form (Navigation) and opens it up.

Code:
Private Sub Form_Load()
'Resequence the data to pull the newest events to the top of the form.
    Me.OrderBy = "[Start_Date] DESC, [start_time] DESC"
    Me.Requery
'Put cursor in search box vice a data row.
    textsearch.SetFocus
End Sub

'somehow it thinks it needs to then run this vba????

Private Sub Event_LostFocus()
'http://eileenslounge.com/viewtopic.php?f=29&t=10183
'Skips warning message if no spelling error
    If Not IsNull(Me.Event) Then
        With Me.Event
            .SetFocus
            .SelStart = 0
            .SelLength = Len(Me.Event)
        End With
        DoCmd.SetWarnings False
        RunCommand acCmdSpelling
        DoCmd.SetWarnings True
    End If
End Sub

Somehow it then jumps over to the Navigation form and starts running this code

Code:
Private Sub Form_Open(Cancel As Integer)
'Log Opening of form
'http://allenbrowne.com/AppLogDocUse.html
    Call LogDocOpen(Me)
End Sub
 
Hi. Your Load event fires and in it you do a Requery, which means the Current event will fire. What code do you have in the Current event? Do you have anything in there that sets the focus to a different control? Also, is the control losing the focus not the first control in the Tab Order?
 
Here's the current event

Code:
Private Sub Form_Current()
    textCurrentRecordID = Event_ID
        'https://answers.microsoft.com/en-us/msoffice/forum/all/ms-access-2010-how-can-i-lock-individual-records/cef4f2b7-bb49-4062-95f1-12f4afac6c64
    If Me.NewRecord Then
        Call LockBoundControls([Form], False)
    Else
        Call LockBoundControls([Form], True)
        Me.cmdLock.caption = "Un&lock"
    End If
End Sub

Hi. Your Load event fires and in it you do a Requery, which means the Current event will fire. What code do you have in the Current event? Do you have anything in there that sets the focus to a different control? Also, is the control losing the focus not the first control in the Tab Order?
 
Based on the previous, I would say no. I moved the cmdLock button to the first position in the tab control just to see if that lead to something different but it didn't change anything.

Do you have anything in there that sets the focus to a different control? Also, is the control losing the focus not the first control in the Tab Order?
 
When I do close the Navigation form again, the cursor is blinking in the textsearch box as requested in the form load.
 
When I do close the Navigation form again, the cursor is blinking in the textsearch box as requested in the form load.
Right. Form events fire in a certain order, unless the code it's executing causes other events to fire first before the event finishes its job and hands over code execution to the next event. So, your Load event fires, causes other events to fire, then finishes itself. That's what I think is happening.
 
I'd love to see your Public Function

As you have discovered, the form load event does not behave in the way you would expect.

A solution to the strange behaviour of the form load event is not to use it.

Once you realise that this is possible, then you stop trying to get something to work with the form load event that just won't, and take control yourself with your own load event.

To do this, I add a public function to most of my forms called fSetUp.

Now instead of putting code in the form load event I put the code in the function fSetUp, and now I know exactly what is going to happen, when, and why.
 
You have this line of code:

Code:
    textsearch.SetFocus

You say your app fires the Event_LostFocus() event but you don't want it to do so. To me it means that before the Form_Current event fired, the control named Event must have had focus.

The key to understanding what happened is that focus events come in pairs. ALWAYS ALWAYS ALWAYS. When something gains focus, something else HAS to lose focus, because focus can only be in one place at a time and Access will take drastic measures to assure that focus is somewhere. In fact, it is so strong a requirement that even if you disable EVERY CONTROL on a form, Access will assert focus on the form itself - the ONLY time that a form rather than a control may have focus. So if your control named textsearch didn't have focus at the time of the Form_Current event, then that line of code I called out will trigger the LostFocus event for whatever control HAD focus. This is not optional behavior.

When I wanted some control to NOT take the code associated with the focus event, I equipped the application with a public flag that the event's procedure could test to decide whether certain effects would be activated. Yes, it added a little extra layer of complexity, but it did the job. Part of the technical problem is that focus events don't have a Cancel option. So if you have event code for focus changes, that code WILL be executed.
 

Users who are viewing this thread

Back
Top Bottom