Question : excel userform textbox cannot setfocus to itself

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

Answer : excel userform textbox cannot setfocus to itself

Can't you just use the Exit event and skip the AfterUpdate (which is IMO fairly pointless in VBA userforms)?
Random Solutions  
 
programming4us programming4us