Microsoft
Software
Hardware
Network
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
Refreshing Worksheets
How to monitor ReadyBoost usage?
array to table?
VOIP Solution for Home Phone
Copying graphs from Excel 2007 to word 2007 using VBA
What is wrong with this code?
SQL using InStr
how to vba programatically audit trail changes from capital to lower case alphabet letters
C# app.config file, can we set a subkey? I mean a key under another key hierarchically?
php apply different class to last result...