View Full Version : [SOLVED:] Remove Focus From Text Box Once Something Has Been Typed In It
HTSCF Fareha
11-06-2022, 12:47 PM
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
Chas Kenyon
11-06-2022, 04:57 PM
If you remove the focus, where will the remainder of their typing go?
HTSCF Fareha
11-07-2022, 12:28 AM
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.
Aussiebear
11-07-2022, 01:13 AM
Have you set the tab order on your text boxes?
HTSCF Fareha
11-07-2022, 02:14 AM
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.
gmaxey
11-07-2022, 05:52 AM
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
HTSCF Fareha
11-09-2022, 02:19 AM
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
gmaxey
11-09-2022, 05:59 AM
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.
HTSCF Fareha
11-09-2022, 02:16 PM
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
gmaxey
11-09-2022, 04:26 PM
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
HTSCF Fareha
11-10-2022, 03:24 AM
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
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.