We help IT Professionals succeed at work.
Get Started

excel userform textbox cannot setfocus to itself

rberke
rberke asked
on
1,195 Views
Last Modified: 2012-05-09
simple form with only two textboxes.  Textbox1 accepts data, processes it and then tries to setfocus to itself so it can accept more data.  Textbox2 has other unrelated uses.


The following code did not work.  After data was entered, textbox2 would always get focus.

Private Sub Textbox1_AfterUpdate()
    debug.print textbox1.text ' actual processing program is irrelevant to today's post
    TextBox1.text = ""
    textbox1.setfocus
End Sub

The following code DID work fairly well.  Once the user starts entering data, they cannot exit the textbox until the data has been processed

The only problem is it is a little bit comlicated. If somebody has a better solution, please post and you will get the points.

Otherwise, I will PAQ this question tomorrow

------- my best solution so far -------------------

Dim CPUMadeChange As Boolean
Dim stopExit As Boolean
Private Sub Textbox1_AfterUpdate()
CPUMadeChange = True
TextBox1.text = ""  ' this fires the _change event. I use CPUMadeChange to keep it from
CPUMadeChange = False
End Sub
Private Sub Textbox1_Change()
If CPUMadeChange Then stopExit = True
End Sub

Private Sub Textbox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If stopExit Then
    Cancel = True
    stopExit = False
End If
End Sub

Private Sub TextBox3_Enter()
Stop
End Sub
Comment
Watch Question
Grand Poobah
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2011
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE