setFocus multiple times (1 Viewer)

stefanoB

New member
Local time
Yesterday, 23:30
Joined
Feb 20, 2019
Messages
2
Hi,
I need to have the focus back to a listbox that on click hilight a row in a second listbox, so:


from listbox1 on click I set focus to listbox2 (after got the param to find)
cycle with for each row in listbox2 and if param is found I set listIndex to the row


until here everything ok, but then I need to setFocus back to listbox1, but give me error: Unable to setfocus to a controllistbox2 (or something similar, I dont have access in english language)


here my example code:


Private Sub listBox1_Click()

Me.itemSel = listBox1.Column(1)

Dim i As Long
Me.listBox2.SetFocus
Me.listBox2.ListIndex = -1
For i = 0 To listBox2.ListCount - 1
If listBox2.Column(7, i + 1) = Me.itemSel Then
Me.listBox2.ListIndex = i
Me.Repaint
Exit For
End If
Next

Me.listBox1.SetFocus
End Sub


Any clues if it possible to move the focus many time in the same sub?
I tryed also to call a secondo sub with the setfocus back, but same result.
Thank you


Stefano
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:30
Joined
Feb 28, 2001
Messages
27,140
For what it is worth, you are not the only person reporting this error, which is #2110. In English, the text is "Microsoft Access can't move the focus to the control xxxx". For future reference, always include the error number in your complaint as well as the text message. But since you are on a non-English version, thanks for translating!

OK, the problem with .SetFocus is that under certain circumstances, Access is already in the process of setting focus elsewhere, and this sounds like one of them. Let me try to explain it mechanistically.

If you got to that control by hitting the tab key, the focus is already there when you click on it. In that circumstance, you might get away with doing what you are doing. BUT if you got there when the focus was elsewhere and you clicked the control, THREE events occurred - a .LostFocus of the control that was in focus when you clicked this one, a .GotFocus of this control, and the .Click event for this control.

ALL THREE will fire, and I believe I have the order correct on that one. Access fires the click event on detection of the click, but the other events fire just BEFORE the actual action occurs. Which means that the click doesn't fire until the other two are handled.

Edit: For some reason, Access wants focus on the control that just got clicked, which is why the other two events have to occur when your click occurs.

You get a .LostFocus because you are STILL in focus - but are about to no longer be in focus. You get a .GotFocus because you are NOT in focus yet - but are about to be. You get to the Click event because it was pending - but events cannot interrupt each other so they enter an "event queue" that holds them to fire in the correct order. This means that the Click event has been pending while the other two fired, and it will fire before returning back to whatever else was going on. BUT what was going on was that you are about to change focus but since the events PRECEDE the action, the actual change of focus will occur next - and you have a .SetFocus in the middle of that code.

Edit: It DOES NOT MATTER that you might have NO code for the .LostFocus and .GotFocus events. They occur whether or not you have anything to say about the matter!

So that means that you are trying to do a .SetFocus at a time when Access itself is already doing a .SetFocus elsewhere. This conflict raises the error.
 
Last edited:

stefanoB

New member
Local time
Yesterday, 23:30
Joined
Feb 20, 2019
Messages
2
Thank you The_Doc_Man,
your explanation was really clear.
I supposed something like that, but I've found not clues to how avoid the error, if there is a way. :(
 

Users who are viewing this thread

Top Bottom