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
Forcing a Logout in Windows 2003 Small Business Server
Recommended backup for a Server 2008 R2 Terminal Server
Can I turn on logging for only 1 mail account in Exchange 2003 server?
Retrieve routing table via SNMP RO Access
primary ip address for a server?
How to show/hide Child gridview based on CheckBox Click in Parent Grid No PostBack
Query to sort by length of text data
Creating histogram in MINITAB 15
Archiving
How to control growing size of an MSSQL database transaction log file