1

In a UserForm in Excel I have a TextBox called ScanInput_Fx which is used repetitively to enter data. Each time the data is processed ScanInput_Fx is emptied and the focus returns to it. Or better: it stays on ScanInput_Fx since I use the Cancel = True option of the _BeforeUpdate event. But, for whatever reason sometimes the cursor does not display. I have tested to make sure that the focus is actually on ScanInput_Fx (by calling the name of the ActiveControl in a msgbox and by pushing text from the VBA code into the ActiveControl) and it really is.

The only thing the user can do is to manually click ScanInput_Fx again. But, that is not really an option as the user is entering the data with a scanner and he is not near the computer.

I have read lots of threads about this, but have not found a working solution. I have tried moving focus to another Control and the back to ScanInput_Fx, but that does not work neither. Strange thing is also that when I ommit the Cancel = True option (of the _BeforeUpdate event) the focus does not jump to the next Tabposition on the form, even not when I hardcode it in VBA. Not sure these two things are related, but maybe it does ring a bell to someone.

Some additional context (as I cannot display all the code here). This is what the code of the _BeforeUpdate Event does:

  • it checks whether the value entered in ScanInput_Fx is valid against a look up in an array
  • it adds a counter of 1 to another Control on the form that corresponds with the data entered in ScanInput_Fx
  • it puts the value of ScanInput_Fx in cell on the underlying excel sheet and saves that sheet as a PDF

So, to my opinion nothing very spectacular. The only thing that may be special is that most of the controls on the form are dynamically created by a function. But, ScanInput_Fx that is showing this erratic behaviour is fixed on the screen (so not dynamically created)

Would there be anyone with some advice on what I can do or where I can start to look for an answer?

Thanks in advance for your help.

Renaat

3
  • 1
    A shot in the dark (Since I cannot see your code). How are you showing your userform? I believe you are using .Show (modal). Try frm.Show vbmodeless. Does that bring the focus back? Commented Oct 16, 2023 at 17:24
  • I would consider using the TextBox's _Enter event to set the cursor position and ensure the TextBox has focus. Like below. Private Sub ScanInput_Fx_Enter() Me.ScanInput_Fx.SelStart = 0 End Sub Commented Oct 16, 2023 at 17:31
  • (Partially) helpful VBA Userform - unexpected behaviour after using setfocus on a textbox ? Commented Oct 16, 2023 at 17:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.