0

When I scan the barcode, it only shows me the first number of barcode in the TextBox1 instead of the full length number which doesn't allow me to test any other conditions except "Wrong Format" I want to have the full barcode so that the other conditions can be tested.

Private Sub TextBox1_Change()
Dim scan As String
Dim premier As Integer
Dim deuxieme As Integer
Dim troisieme As Integer
Dim quatrieme As Integer

scan = TextBox1


    premier = InStr(1, scan, "-")
    deuxieme = InStr(premier + 1, scan, "-")
    troisieme = InStr(deuxieme + 1, scan, "-")
    quatrieme = InStr(troisieme + 1, scan, "-")
    
    If premier = 9 And deuxieme = 18 And troisieme = 22 And quatrieme = 27 Then
        magnetA = InStr(1, scan, "A")
        magnetB = InStr(1, scan, "B")
        disque = InStr(1, scan, "D")
        
            'MAGNET A'
            If magnetA = 28 Then
            TextBox1.BackColor = RGB(0, 255, 0)
            TextBox2.SetFocus
            MsgBox "Magnet A detecté !"
            End If
            
            'MAGNET B'
            TextBox1.BackColor = RGB(0, 255, 0)
            If magnetB = 28 Then
            TextBox2.SetFocus
            MsgBox "Magnet B detecté !"
            End If
            
            'DISC  '
            TextBox1.BackColor = RGB(0, 255, 0)
            If disque = 28 Then
            TextBox2.SetFocus
            MsgBox "Disque detecté !"
            End If
            
    End If
    
    'WRONG FORMAT'
    If premier = 0 And deuxieme = 0 Then
            TextBox1.BackColor = RGB(255, 0, 0)
            MsgBox "format incorrect !"
            TextBox1.BackColor = RGB(255, 255, 255)
            TextBox1 = ""
    End If
    
    'BRAKE'
    If premier = 9 And deuxieme = 11 Then
            TextBox1.BackColor = RGB(0, 255, 0)
            TextBox2.SetFocus
            MsgBox "Numero frein detecté !"
    End If

End Sub

12
  • 1
    Is this is what your scanner does, what do you expect from (any) VBA code? To make it return correctly, or what? Please, explain in words what do you need solving.
    – FaneDuru
    Aug 11, 2022 at 8:27
  • For example, I want to scan this barcode "30315359-11619728-003-K022-B" I want to identify if it's a Magnet A, Magnet B a Disc or a Break. If not, it's the wrong format. My program doesn't allow me to identify the barcode because it goes straight to "Wrong Format". It goes straight to "Wrong Format" because when I scan the program it doesn't scan the whole barcode -> "30315359-11619728-003-K022-B" but just the first figure "3".
    – Luxior
    Aug 11, 2022 at 8:42
  • I would firstly suggest to cancel the event. I mean, modify TextBox1_Change() in TextBox1_Change_(). Adding an underscore character ("_"). In order to see if your event does something wrong to the correctly dropped barcode string. Now select the text box and scan inside it. If it looks as it should be, correct this part "it only shows me the first number of barcode in the TextBox1` of your question and better explain (in words) what you expect from the above code. If it does not show the string you need (want), the question about the code is useless. And let us know about the result...
    – FaneDuru
    Aug 11, 2022 at 8:43
  • If you scan in an Excel cell, does it scan correctly?
    – FaneDuru
    Aug 11, 2022 at 8:50
  • Yes in an Excell cell it does scan correctly. But the thing is when the barcode is scanned the numbers are displayed one after another in real-time and not all at once as in a copy and paste
    – Luxior
    Aug 11, 2022 at 8:58

1 Answer 1

0

Barcode scanners act as keyboards to a pc, and having this fire on change is only checking after the first "key" is pressed.

The scanner probably adds a CR/LF after the whole barcode is typed out, or can be configured to, so you should have it fire after it exits a cell to check the whole value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.