Show error if no match then clear textbox

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
172
Hello All,

I have a userform with 2 texboxes where I scan a barcode in both and if they match the textboxes turn green and red if no match, I am trying to show an error message if there if no match
then clear both textboxes but I can not figure out how to do it as I am very new to VBA, could anyone help.
Below is what I have at the minute.

Private sub TextBox2_Change ()
If TextBox5.Text = TextBox2.Text then
TextBox2.Backcolor = RGB(51, 255, 51)
TextBox5.Backcolor = RGB(51, 255, 51)
Else
TextBox2.Backcolor = RGB(255, 0, 0)
TextBox5.Backcolor = RGB(255, 0, 0)
End If
End Sub

Regards

 
It looks like the change event is being triggered after the first character is there a way around this?
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
It looks like the change event is being triggered after the first character is there a way around this?

Another test.

Create a commandbutton.
1. Select Combo.
2. Scan into textbox5
3. Press commandbutton

Code:
[COLOR=#ff0000]'Delete these lines[/COLOR]
[COLOR=#ff0000]'Private Sub TextBox5_Change()[/COLOR]
[COLOR=#ff0000]'    Call matchText[/COLOR]
[COLOR=#ff0000]'End Sub[/COLOR]
[COLOR=#ff0000]'Private Sub TextBox2_Change()[/COLOR]
[COLOR=#ff0000] '   Call matchText[/COLOR]
[COLOR=#ff0000]'End Sub

[/COLOR][COLOR=#0000ff]'Add this to the command[/COLOR][COLOR=#ff0000]
[/COLOR]Private Sub CommandButton5_Click()
   Call matchText
End Sub



Sub matchText()
        If TextBox2.Value = "" Or TextBox5.Value = "" Then Exit Sub
        If TextBox5.Value = TextBox2.Value Then
        TextBox2.BackColor = RGB(51, 255, 51)
        TextBox5.BackColor = RGB(51, 255, 51)
    Else
        TextBox2.BackColor = RGB(255, 0, 0)
        TextBox5.BackColor = RGB(255, 0, 0)
        MsgBox "no match"
        'TextBox2.Value = ""
        'TextBox5.Value = ""
        'TextBox2.BackColor = RGB(255, 255, 255)
        'TextBox5.BackColor = RGB(255, 255, 255)
        TextBox2.SetFocus
    End If
End Sub
 
Upvote 0
It works doing it with the command button.
Is there a way without them having to press a command button?
 
Upvote 0
It works doing it with the command button.
Is there a way without them having to press a command button?

Let's make another test.

Remove the code that I sent you.
Scan into textbox2.
What's going on? Does the cursor remain inside the textbox2 or does it pass to another control?
 
Upvote 0
After scanning in to textbox2 cursor jumps to the next box.

Try the exit event. It works in this way, you capture the data and when you exit the textbox2 the event is activated.
Remove the Change events and the commandbutton.

Code:
Private Sub [COLOR=#0000ff]TextBox2_Exit[/COLOR](ByVal Cancel As MSForms.ReturnBoolean)
    Call matchText
End Sub
Sub matchText()
        If TextBox2.Value = "" Or TextBox5.Value = "" Then Exit Sub
        If TextBox5.Value = TextBox2.Value Then
        TextBox2.BackColor = RGB(51, 255, 51)
        TextBox5.BackColor = RGB(51, 255, 51)
    Else
        TextBox2.BackColor = RGB(255, 0, 0)
        TextBox5.BackColor = RGB(255, 0, 0)
        MsgBox "no match"
        'TextBox2.Value = ""
        'TextBox5.Value = ""
        'TextBox2.BackColor = RGB(255, 255, 255)
        'TextBox5.BackColor = RGB(255, 255, 255)
        TextBox2.SetFocus
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,619
Members
452,661
Latest member
Nonhle

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