Excel .Setfocus Jumping to wrong Control

64 Views Asked by At

I'm working with Excel 2016 and have an interesting problem with the cursor jumping to locations I didn't command.

enter image description here

The goal is to jump to the "Svc Date" (txt_svcDate) field if the value of the combo box "Inspection Type" = "Initial". But for some reason it is jumping to the "Date On" field. I should note that I am using the Tab key to leave cmb_insp field.

Private Sub cmb_insp_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If Me.cmb_insp.Value = "Initial" Then
    Me.txt_svcDate.SetFocus
End If

End Sub

Something similar is happening after leaving the "Tail #" field. In that code, the goal is to jump to the "Date Off" field (txt_dateOff) if the combo box "Inspection Type" = "Originial to".

Private Sub txt_tail_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If Me.cmb_insp.Value = "Original to" Then
    Me.txt_dateOff.SetFocus
End If

End Sub

In this second case it is jumping to the "Work Order On" (txt_woOn) control. Again, I am using the tab key to exit the control.

In both instances I have noticed very breif flashes of the cursor in the correct input before it suddenly appears in the wrong box.

A few things to note about this issue. 1. I put the set focus code into a command button and it works just fine.

Private Sub cmd_Test_Click()
Me.txt_dateOff.SetFocus
End Sub

So I know I have the correct name of the desired control that I want to set focus on. 2. I tried using my mouse to click into a different control instead of using the Tab Key, and in both cases, the above exit codes ran perfectly. 3. For testing, I installed a few message boxes in the Exit events to see if they were firing. In all cases, the message box appeared, the code ran, but the cursor still wound up in the wrong field. I even tried removing all other code in the module, leaving just these exit events and still got the same results.

I also tried debugging by putting a stop at each if statement and running one line at a time. The only abnormality I noted there was that it actually looped through the Sub twice before terminating, with or without the Cancel = True statement.

All the code looks right. So what could be causing these uncommanded jumps?

0

There are 0 best solutions below