Consulting

Results 1 to 9 of 9

Thread: Userform textbox skipped in tab order after re-enabling - unwanted behaviour

  1. #1

    Userform textbox skipped in tab order after re-enabling - unwanted behaviour

    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.
    Attached Files Attached Files

  2. #2
    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.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    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
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Private Sub TextBox2_Change()
        TextBox3.Visible = TextBox2 <> "X"
    End Sub

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    @snb -- better
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Thank you both.
    Thanks snb for the new knowledge, I didn't know you could do that. Very succinct.

  7. #7
    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.

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by melpa View Post
    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
    Attached Files Attached Files
    Last edited by Paul_Hossler; 06-13-2020 at 09:21 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    Thanks for taking the time. Much appreciated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •