Form Open Event moving automatically to Field SpellCheck LostFocus

dgreen

Member
Local time
Today, 05:52
Joined
Sep 30, 2018
Messages
397
Can anyone explain why my code is triggering a LostFocus event next after the Form_Open event? All of this is happening before the user can even see the form. The first time they see the form it's doing a spell check, which isn't the behavior I'm looking for. I just want the form to open and have the cursor in the textsearch box. Below I show you my F8 next step.

Please advise on how to fix or stop this.

1584369420110.png
 
Hi. Without seeing your form, my guess is the first tab on your form is assigned to the control called Event. If so, when the form opens and you set the focus to textsearch, then the Lost Focus event of the Event control fires because you just forcibly took the focus from it.
 
Good guess. Textsearch is in the Form Header and Event field is in the detail section.
1584371672774.png
 
Nope. I'm still confused. You mentioned before that SetFocus and LostFocus events happen in pairs. In my mind the sequence that I have makes sense but obviously that isn't the case.
 
Nope. I'm still confused. You mentioned before that SetFocus and LostFocus events happen in pairs. In my mind the sequence that I have makes sense but obviously that isn't the case.
Hi. The question you posted was why the lost focus event was firing, which I said was because of you forcing the focus to go somewhere else. You will only get an effect from an event firing if you had an event procedure tied to that event. So, to see the focus event fire, do you have an event procedure in there? If you don't, the event could be firing, but you won't get any notification of it.
 
I think I'm answering your question.

The textsearch box has no Events tied to it.
The Event Field has 3 Events within it. AfterUpdate, On Lost Focus and On Key Down.

Picture2.png


Code:
Private Sub Event_AfterUpdate()
    'refresh the current record to obtain a primary key
    Me.Requery
    'Resort list with null date at top, then most current date/time.
    'https://www.access-programmers.co.uk/forums/threads/null-date-sort-to-bottom-query-problem.173051/
    Me.OrderBy = "[NullDateSort] DESC, [start_date] desc, [start_time] desc"
    'Set focus on start date to be populated.
    Me.Start_Date.SetFocus
    'Unlock the new record that was sorted to the top
    Call LockBoundControls([Form], False)
End Sub

Private Sub Event_KeyDown(KeyCode As Integer, Shift As Integer)
    Call NavigateRecords(KeyCode)
End Sub

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
 
I think I'm answering your question.

The textsearch box has no Events tied to it.
The Event Field has 3 Events within it. AfterUpdate, On Lost Focus and On Key Down.

View attachment 79903

Code:
Private Sub Event_AfterUpdate()
    'refresh the current record to obtain a primary key
    Me.Requery
    'Resort list with null date at top, then most current date/time.
    'https://www.access-programmers.co.uk/forums/threads/null-date-sort-to-bottom-query-problem.173051/
    Me.OrderBy = "[NullDateSort] DESC, [start_date] desc, [start_time] desc"
    'Set focus on start date to be populated.
    Me.Start_Date.SetFocus
    'Unlock the new record that was sorted to the top
    Call LockBoundControls([Form], False)
End Sub

Private Sub Event_KeyDown(KeyCode As Integer, Shift As Integer)
    Call NavigateRecords(KeyCode)
End Sub

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
Hi. Okay, so, if the question was "how come my lost focus event is firing," then the answer is "because you are making it lose the focus." If the question is "how come the focus event is firing," then the answer is "it is, but you don't see it because you don't have an event procedure for it." Does that help?
 
Where would I create this event procedure to see it firing?
I'm still guessing me way forward with your guidance.
 
Where would I create this event procedure to see it firing?
I'm still guessing me way forward with your guidance.
Can you tell us your primary concern? Did you want to make sure the Focus event of the Event control was firing? If it does, what would you like to do with it?
 
Can you tell us your primary concern? Did you want to make sure the Focus event of the Event control was firing? If it does, what would you like to do with it?
Also, can you please let me know if I was able to answer your original question? Sorry if I'm a bit confused right now.
 
Here's my end goal. Stop the Event field from running the Spell Check when the Form Opens. When the form opens, put the cursor in the textsearch and STOP.
 
Here's my end goal. Stop the Event field from running the Spell Check when the Form Opens. When the form opens, put the cursor in the textsearch and STOP.
Ah, I see, so the lost focus event is running a spell check (sorry I missed that earlier). One way to avoid that is to move the spell check code to the AfterUpdate event instead. Is there any reason to perform a spell check if the value in the control was never changed/updated?
 
I've changed all of the Spelling checks to a BeforeUpdate event. I feel that the code still isn't acting right and further troubleshooting is needed.

If the sequence of a form's code is supposed to be as below, …..

When you first open a form, the following events occur in this order:
Open → Load → Resize → Activate → Current
(source: https://docs.microsoft.com/en-us/office/vba/api/access.form.activate)

why would the code be stopping first on the Current event? I put a stop point on the Current and Load to see if I could understand what was happening AFTER to textsearch.setfocus event. Apparently the Form_Current() is running first? Does this make any sense?

1584379765274.png
 
@theDBguy I'm going to build a mock db with the navigation and events form and all the VBA code and post it. Maybe with a demo example someone can figure out a better way to set up the code and isolate this issue.
 
@theDBguy I'm going to build a mock db with the navigation and events form and all the VBA code and post it. Maybe with a demo example someone can figure out a better way to set up the code and isolate this issue.
Hi. That's probably best. Thanks!
 

Users who are viewing this thread

Back
Top Bottom