PDA

View Full Version : [SOLVED:] Userform textbox skipped in tab order after re-enabling - unwanted behaviour



melpa
06-09-2020, 11:25 AM
Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

If TextBox2.Value = "X" Then
TextBox3.Enabled = False
Else
TextBox3.Enabled = True
End If

End Sub


If "X" is entered into TextBox2, TextBox3.Enabled is set to false.
The first time the tab key is pressed it stays in TextBox2. The second time it jumps to TextBox4.
After that, if anything else is entered into TextBox2, TextBox3 is re-enabled.
But now when the tab key is pressed it jumps immediately to TextBox4.

I've tried resetting tabstop to true, adding 'TextBox3.SetFocus', but it doesn't help.

Does anyone know how can I fix this?
I want to tab smoothly through all enabled textboxes.

Workbook attached.

melpa
06-09-2020, 11:41 AM
I figured it out. Mostly.
I moved TextBox3.Enable to the change event and now it works.

It still takes two key strokes to leave textbox2 if I enter "X" though.

Paul_Hossler
06-09-2020, 01:40 PM
I'd do something like this



Option Explicit


Private Sub TextBox2_Change()


If TextBox2.Value = "X" Then
TextBox3.Visible = False
Else
TextBox3.Visible = True
End If


End Sub

snb
06-10-2020, 07:26 AM
Private Sub TextBox2_Change()
TextBox3.Visible = TextBox2 <> "X"
End Sub

Paul_Hossler
06-10-2020, 07:34 AM
@snb -- better

melpa
06-13-2020, 04:25 AM
Thank you both.
Thanks snb for the new knowledge, I didn't know you could do that. Very succinct.

melpa
06-13-2020, 07:38 AM
Could you please explain the advantages of using .Visible in preference to .Enabled ?
Both of the solutions given appear to work equally well with either choice.

Paul_Hossler
06-13-2020, 09:10 AM
Could you please explain the advantages of using .Visible in preference to .Enabled ?
Both of the solutions given appear to work equally well with either choice.

.Visible = False hides the control

.Enabled = False dims the control but you can still see it, but can't use it or tab to it

FWIW if a control is not applicable based on settings, then I hide it. If it's applicable but it's not ready to use I usually dim it until all required fields are entered


Demo



Option Explicit


Private Sub TextBox1_Change()
CommandButton1.Enabled = Len(TextBox1.Text) > 0 And Len(TextBox2.Text) > 0 And Len(TextBox4.Text) > 0
End Sub


Private Sub TextBox2_Change()
TextBox3.Visible = (LCase(TextBox2.Value) <> "x")
CommandButton1.Enabled = Len(TextBox1.Text) > 0 And Len(TextBox2.Text) > 0 And Len(TextBox4.Text) > 0
End Sub
Private Sub TextBox3_Change()
CommandButton1.Enabled = Len(TextBox1.Text) > 0 And Len(TextBox2.Text) > 0 And Len(TextBox4.Text) > 0
End Sub
Private Sub TextBox4_Change()
CommandButton1.Enabled = Len(TextBox1.Text) > 0 And Len(TextBox2.Text) > 0 And Len(TextBox4.Text) > 0
End Sub




Private Sub UserForm_Initialize()
CommandButton1.Enabled = False
End Sub

melpa
06-19-2020, 10:12 AM
Thanks for taking the time. Much appreciated.