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

 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
DanteAmor now it will work

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]https://www.dropbox.com/s/7upnc0pg7s6xeau/TEST.xlsm?dl=0[/FONT]
 
Upvote 0
DanteAmor now it will work

https://www.dropbox.com/s/7upnc0pg7s6xeau/TEST.xlsm?dl=0


Try this
Code:
Private Sub TextBox5_Change()
    Call matchText
End Sub
Private Sub TextBox2_Change()
    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

1. Select an item from the combo, the textbox5 is filled.
2. Scan in the textbox2, if they are the same they are painted green.
3. If they are different, they are painted red.
Up there all right?


4. Now, if you press Ok on the msgox, what do you want to happen?

Try the following file

https://www.dropbox.com/s/jzqcchn0bitka4e/Test dam.xlsm?dl=0
 
Upvote 0
As soon as i scan into textbox2 with a matching code they go red and the error message pops up. Regarding the msgbox when ok is pressed I need it to clear textbox2.
 
Upvote 0
As soon as i scan into textbox2 with a matching code they go red and the error message pops up. Regarding the msgbox when ok is pressed I need it to clear textbox2.


Apparently they look the same, but I guess your scan machine is adding some special character in the textbox2, it can be a space or an enter.
You could check, after scan into textbox2 if to the right or to the left of the value there is some strange character.
 
Upvote 0
This is how I need it to work.

1. Select an item from combo, Textbox5 is filled
2. scan into Textbox2, if they match Textbox 2 & 5 turn green
3. If Textbox 2 & 5 don't match they turn red and an error msgbox pops up.
4. When ok is pressed on error msgbox Textbox5 is cleared.

Please ignore #25 where I say clear textbox2 it should be 5
 
Upvote 0
#26
I scaned into textbox2 there is no strange character once it is scaned it jumps to the next box

My tests work.


You can try manually.
1. Select combo
2. Copy the textbox5 value and paste it into the textbox2.

Tell me what happens?


3. Delete a character from the value of the textbox2.

Tell me what happens?
 
Upvote 0
Doing as you said in #29 works perfectly its when i scan with the gun it seems to do the first charactor then the error msg any ideas why?
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,613
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