Reputation: 14383
I have a userform with a number of frames in it, each frame with a number of text boxes. Some of the Tbxs have event procedures causing autofill on Enter provided the Tbx is blank. They all work. But if I delete the autofilled content, move to another control and then click the first control again the event doesn't fire. It doesn't matter what or where I click, nothing happens until I click another control in the same frame and then the one I am testing. Only in that sequence the event will fire again.
Click, delete, click in another control in the same frame, and click again in the first control does autofill every time. It's just when I delete, click a control in another frame and then return that the expected event doesn't occur.
I know that the sequencing of events between frames and controls is complicated. I believe that the Exit event of the tested control doesn't occur when I click a control in another frame, that the Exit event, in fact, only occurs when I click another control in the same frame, and that the Enter event, simply, doesn't occur because the Exit event hasn't preceded it, irrespective of which other frames I have physically clicked on in the meantime.
This logic, while apparently plausible, is weird, however, when considering that I have another auto-filling Tbx in another frame and, coming from the Tbx I am testing, that other on Enter event does fire. So, presumably the second frame's on Enter event did occur, enabling the control's on Enter event to autofill it. How can all those Enter events occur without the Exit event for the Tbx under testing having occurred? Well, not everything about Word is logical.
But perhaps there is someone who has experience with this and can either tell me how to force the Exit event for the first control or its frame, or what other event might be more suitable for this particular purpose.
Thank you.
Upvotes: 0
Views: 913
Reputation: 14383
The reluctant TextBox's Exit event can be replaced with a similar procedure which is called by other events. The most suitable one is the frame's Enter event which fires when any control in the frame is activated and can be made to call a procedure like the following.
Dim PrevFrm As MSForms.Frame
Private Sub SetPrevControl()
' 02 May 2017
If Not PrevFrm Is Nothing Then
If Not PrevFrm.ActiveControl Is Nothing Then
' call the replacement Exit procedure here
MsgMe "Trigger Exit for " & PrevFrm.ActiveControl.Name
End If
End If
Set PrevFrm = ActiveControl
End Sub
Upvotes: 1
Reputation: 53623
Create a UserForm with 2 frames, each containing 2 TextBox controls, and use the following code to test/debug.
Private Sub Frame1_Enter()
MsgMe "frm1 enter"
End Sub
Private Sub Frame1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
MsgMe "frm1 exit"
End Sub
Private Sub TextBox1_Enter()
MsgMe "tb1 enter"
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
MsgMe "tb1 exit"
End Sub
Private Sub TextBox2_Enter()
MsgMe "tb2 enter"
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
MsgMe "tb2 exit"
End Sub
Private Sub Frame2_Enter()
MsgMe "frm2 enter"
End Sub
Private Sub Frame2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
MsgMe "frm2 exit"
End Sub
Private Sub TextBox3_Enter()
MsgMe "tb3 enter"
End Sub
Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
MsgMe "tb3 exit"
End Sub
Private Sub TextBox4_Enter()
MsgMe "tb4 enter"
End Sub
Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
MsgMe "tb4 exit"
End Sub
Sub MsgMe(str$)
Debug.Print str
End Sub
Your suspicion seems correct.
I believe that the Exit event of the tested control doesn't occur when I click a control in another frame, that the Exit event, in fact, only occurs when I click another control in the same frame, and that the Enter event, simply, doesn't occur because the Exit event hasn't preceded it, irrespective of which other frames I have physically clicked on in the meantime
I believe this is why:
Frame1_Enter
event and the TextBox1_Enter
event.Frame1_Exit
, then Frame2_Enter
and finally the TextBox3_Enter
event but not the TextBox1_Exit
event. Frame1_Enter
event has raised. The TextBox1_Exit
event hasn't happened, because you haven't exited this control -- when you left the Frame in Step 2, this control still had focus, and now you're returning to it.Now, do things a little differently:
Frame1_Enter
event and the TextBox1_Enter
event.Frame1_Exit
, then Frame2_Enter
and finally the TextBox3_Enter
event but not the TextBox1_Exit
event. Form1_Enter1
, TextBox1_Exit
, and finally TextBox2_Enter
.If you add a MouseDown
event handler to your textboxes, you'll be able to capture that scenario where the control has not yet Exit
ed, but you're returning focus to it/parent frame.
Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
MsgMe "tb1 mousedown"
End Sub
Upvotes: 1