Consulting

Results 1 to 11 of 11

Thread: Remove Focus From Text Box Once Something Has Been Typed In It

  1. #1
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location

    Remove Focus From Text Box Once Something Has Been Typed In It

    I have a userform set up so that when the "Enter" button is pressed, if any text box is empty, it will set focus and change its background colour to highlight the missing entry.

    This works fine as per my excerpt from my code below. But I was wondering if it would be possible to in essence remove the focus event once someone starts to type something in the box that previously had nothing in it?

    Private Sub EnterBut_Click()
    
        If cbThreat.ListIndex < 1 Then
            MsgBox "Select threat level!", vbExclamation + vbOKOnly, "Triage Hub"
            cbThreat.SetFocus
            cbThreat.BackColor = RGB(102, 255, 255)
            GoTo lbl_Exit
        End If
        If cbHarm.ListIndex < 1 Then
            MsgBox "Select harm level!", vbExclamation + vbOKOnly, "Triage Hub"
            cbHarm.SetFocus
            cbHarm.BackColor = RGB(102, 255, 255)
            GoTo lbl_Exit
        End If
        If cbOpportunity.ListIndex < 1 Then
            MsgBox "Select opportunity level!", vbExclamation + vbOKOnly, "Triage Hub"
            cbOpportunity.SetFocus
            cbOpportunity.BackColor = RGB(102, 255, 255)
            GoTo lbl_Exit
        End If
        If cbRisk.ListIndex < 1 Then
            MsgBox "Select risk level!", vbExclamation + vbOKOnly, "Triage Hub"
            cbRisk.SetFocus
            cbRisk.BackColor = RGB(102, 255, 255)
            GoTo lbl_Exit
        End If
        If cbDepartment.ListIndex < 1 Then
            MsgBox "Select department to follow up!", vbExclamation + vbOKOnly, "Triage Hub"
            cbDepartment.SetFocus
            cbDepartment.BackColor = RGB(102, 255, 255)
            GoTo lbl_Exit
        End If
        If txtRationale.Text = vbNullString Then
            MsgBox "Enter rationale!", vbExclamation + vbOKOnly, "Triage Hub"
            txtRationale.SetFocus
            txtRationale.BackColor = RGB(255, 255, 204)
            GoTo lbl_Exit
        End If
        Tag = 1
        Hide
    lbl_Exit:
        Exit Sub
    End Sub

  2. #2
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    123
    Location
    If you remove the focus, where will the remainder of their typing go?

  3. #3
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location
    At the moment when someone presses the "Enter" button, the background colour of the first TextBox on the UserForm that does not have any text in it will have its background colour changed to highlight it. What I was wondering is if then the user starts to type in that highlighted TextBox, as soon as they begin to type, the background colour returns to its default state rather than the highlighted one.

    With my limited knowledge of VBA, I'm not even sure if such a "live" event is achievable.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,053
    Location
    Have you set the tab order on your text boxes?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location
    Yes, the tab order is set so if the user uses the tab key it will move to the next TextBox / ComboBox. There is, however, one TextBox that is optional, although it is still included on the tab order.

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    You can use the control click or change event to evaluate content. If a textbox text len is greater than zero then set backcolor to the normal color. If memory serves, you can't change the backcolor of a listbox if it has an item selected. In the past, I think I have changed the border color to red to flag invalidation. Again you use the click or change event if ListIndex <> -1
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location
    Thanks, Greg.

    I was wondering if I am on the right lines with something like this (taken a few options out until I achieve something that works)? I like the idea of highlighting the missing TextBox / ComboBox with a border colour.

    Private Sub EnterBut_Click()
    
        If cbThreat.ListIndex < 1 Then
            MsgBox "Select threat level!", vbExclamation + vbOKOnly, "Triage Hub"
            cbThreat.SetFocus
            cbThreat.BorderColor = RGB(255, 0, 0)
            GoTo lbl_Exit
            
        ElseIf cbThreat.ListIndex < 1 Then
            cbThreat.BorderColor = RGB(0, 0, 0)
            GoTo lbl_Exit
        End If
        
        If Trim(txtResearch.Text & "") = " " Then
            MsgBox "Enter Research", vbExclamation + vbOKOnly, "Triage Hub"
            txtResearch.SetFocus
            txtResearch.BackColor = RGB(255, 0, 0)
            GoTo lbl_Exit
        ElseIf Trim(txtResearch.Text & "") <> " " Then
            txtResearch.BackColor = RGB(0, 0, 0)
            GoTo lbl_Exit
        End If
    
        Tag = 1
        Hide
    lbl_Exit:
        Exit Sub
    End Sub

  8. #8
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Well, not sure I understand your ElseIf statements (the cbThreat is one color if it is < 1 and another color if it is < 1, which is it?). If a listbox item is selected the .ListIndex will return 0 to .ListCount -1. If nothing is selected it returns -1.

    I would use the textbox change event and the listbox change or click event to restore the normal colors and use the command but to flag the invalid fields.
    Greg

    Visit my website: http://gregmaxey.com

  9. #9
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location
    Thank you, Greg. You were quite right and I really shouldn't post without checking which version of code I have been testing I add to the thread.

    I've hopefully taken on board your suggestions and have arrived at the following, but this still doesn't achieve my goal of the highlighted missing focus detail being removed as soon as text is input or selection is made.

    Private Sub txtResearch_Change()
    
        If txtResearch.Text = vbNullString Then
            MsgBox "Enter Research!", vbExclamation + vbOKOnly, "Triage Hub"
            txtResearch.SetFocus
            txtResearch.BackColor = RGB(255, 255, 204)
            GoTo lbl_Exit
        End If
        
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub cbThreat_Change()
    
        If cbThreat.ListIndex < 1 Then
            MsgBox "Select threat level!", vbExclamation + vbOKOnly, "Triage Hub"
            cbThreat.SetFocus
            cbThreat.BackColor = RGB(102, 255, 255)
            GoTo lbl_Exit
        End If
        
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub EnterBut_Click()
    
        txtResearch_Change
        
        If txtResearch.Text < " " Then
            txtResearch.BackColor = RGB(255, 255, 204)
            GoTo lbl_Exit
        End If
        
        cbThreat_Change
        
        If cbThreat.ListIndex < 1 Then
            cbThreat.BackColor = RGB(255, 255, 204)
            GoTo lbl_Exit
        End If
        
        Tag = 1
        Hide
    lbl_Exit:
        Exit Sub
    End Sub

  10. #10
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Private Sub txtResearch_Change()
      If Len(txtResearch.Text) > 0 Then txtResearch.BackColor = RGB(whatever the normal valid color is)
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub cbThreat_Change()
      If cbThreat.ListIndex <> -1 Then cbThreat.BackColor = RGB(whatever then normal color is)
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub EnterBut_Click()
      If Len(txtResearch.Text) = Then
        txtResearch.BackColor = RGB(whatever the invalid color is)
        txtResearch.SetFocus
        Exit Sub
      End If
     If cbThreat.ListIndex = -1 Then
       cbThreat.BackColor = RGB(whatever the invalid color is)
       cbThreat.SetFocus
       Exit Sub
     End If
     Tag = 1
     Hide
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  11. #11
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location
    My thanks to you Greg, this is now working brilliantly!

    Private Sub cbThreat_Change()
    
        If cbThreat.ListIndex <> -1 Then cbThreat.BackColor = RGB(255, 255, 255)
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub txtThreat_Change()
    
        If Len(txtThreat.Text) > 0 Then txtThreat.BackColor = RGB(255, 255, 255)
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub cbHarm_Change()
    
        If cbHarm.ListIndex <> -1 Then cbHarm.BackColor = RGB(255, 255, 255)
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub txtHarm_Change()
    
        If Len(txtHarm.Text) > 0 Then txtHarm.BackColor = RGB(255, 255, 255)
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub cbOpportunity_Change()
    
        If cbOpportunity.ListIndex <> -1 Then cbOpportunity.BackColor = RGB(255, 255, 255)
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub txtOpportunity_Change()
    
        If Len(txtOpportunity.Text) > 0 Then txtOpportunity.BackColor = RGB(255, 255, 255)
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub cbRisk_Change()
    
        If cbRisk.ListIndex <> -1 Then cbRisk.BackColor = RGB(255, 255, 255)
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub txtRisk_Change()
    
        If Len(txtRisk.Text) > 0 Then txtRisk.BackColor = RGB(255, 255, 255)
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub txtRationale_Change()
    
        If Len(txtRationale.Text) > 0 Then txtRationale.BackColor = RGB(255, 255, 255)
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub EnterBut_Click()
    
        If cbThreat.ListIndex < 1 Then
            cbThreat.BackColor = RGB(102, 255, 255)
            cbThreat.SetFocus
            MsgBox "Select threat level!", vbExclamation + vbOKOnly, "Triage Hub"
            Exit Sub
        End If
        
        If Len(txtThreat.Text) = 0 Then
            txtThreat.BackColor = RGB(255, 255, 204)
            txtThreat.SetFocus
            MsgBox "Enter threat text!", vbExclamation + vbOKOnly, "Triage Hub"
            Exit Sub
        End If
        
        If cbHarm.ListIndex < 1 Then
            cbHarm.BackColor = RGB(102, 255, 255)
            cbHarm.SetFocus
            MsgBox "Select harm level!", vbExclamation + vbOKOnly, "Triage Hub"
            Exit Sub
        End If
        
        If Len(txtHarm.Text) = 0 Then
            txtHarm.BackColor = RGB(255, 255, 204)
            txtHarm.SetFocus
            MsgBox "Enter harm text!", vbExclamation + vbOKOnly, "Triage Hub"
            Exit Sub
        End If
        
        If cbOpportunity.ListIndex < 1 Then
            cbOpportunity.BackColor = RGB(102, 255, 255)
            cbOpportunity.SetFocus
            MsgBox "Select opportunity level!", vbExclamation + vbOKOnly, "Triage Hub"
            Exit Sub
        End If
        
        If Len(txtOpportunity.Text) = 0 Then
            txtOpportunity.BackColor = RGB(255, 255, 204)
            txtOpportunity.SetFocus
            MsgBox "Enter opportunity text!", vbExclamation + vbOKOnly, "Triage Hub"
            Exit Sub
        End If
        
        If cbRisk.ListIndex < 1 Then
            cbRisk.BackColor = RGB(102, 255, 255)
            cbRisk.SetFocus
            MsgBox "Select risk level!", vbExclamation + vbOKOnly, "Triage Hub"
            Exit Sub
        End If
        
        If Len(txtRisk.Text) = 0 Then
            txtRisk.BackColor = RGB(255, 255, 204)
            txtRisk.SetFocus
            MsgBox "Enter risk text!", vbExclamation + vbOKOnly, "Triage Hub"
            Exit Sub
        End If
        
        If Len(txtRationale.Text) = 0 Then
            txtRationale.BackColor = RGB(255, 255, 204)
            txtRationale.SetFocus
            MsgBox "Enter rationale!", vbExclamation + vbOKOnly, "Triage Hub"
            Exit Sub
        End If
        
        Tag = 1
        Hide
    lbl_Exit:
        Exit Sub
    End Sub

Posting Permissions

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