Userform tab order

ecchahine

New Member
Joined
Mar 31, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi. So I have a userform and let's say it has one textbox, a controlbutton1 and a controlbutton2.
controlbutton1.enabled = false
controlbutton2.enabled = true

at textbox_afterupdate (after pressing Enter or Tab), if the content of the textbox meets the requirements it sets controlbutton1.enabled to true.
The problem I'm having is every time the code gets executed it skips controlbutton1 and sets focus to controlbutton2.
I have tried setting focus (controlbutton1.setfocus) before exiting textbox_afterupdate, didn't work. And yes, the tab orders are correct: textbox (0), controbutton1 (1), controlbutton2 (2).

Any ideas? Thanks in advance.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
AfterUpdate event occurs when focus reach new control.
If it's not necessary to use this event you can use KeyDown event. Here is example.
VBA Code:
Private Sub UserForm_Initialize()
    
    CommandButton1.Enabled = False
    CommandButton2.Enabled = True
    
End Sub

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    
    If KeyCode = 13 Or KeyCode = 9 And _
        TextBox1.Text = "requirement" Then
        CommandButton1.Enabled = True
    End If

End Sub
 
Upvote 0
Solution
AfterUpdate event occurs when focus reach new control.
If it's not necessary to use this event you can use KeyDown event. Here is example.
VBA Code:
Private Sub UserForm_Initialize()
   
    CommandButton1.Enabled = False
    CommandButton2.Enabled = True
   
End Sub

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
   
    If KeyCode = 13 Or KeyCode = 9 And _
        TextBox1.Text = "requirement" Then
        CommandButton1.Enabled = True
    End If

End Sub
Genius, thank you.
 
Upvote 0

Forum statistics

Threads
1,224,541
Messages
6,179,418
Members
452,912
Latest member
alicemil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top