How to check if scanned barcode is either 8 or 13 digits long?

lpking2005

Board Regular
Joined
Mar 21, 2011
Messages
140
Hi,

I have a system where the user scans a barcode into a textbox and it returns data from another workbook.

The problem is that the barcode will either be 8 or 13 digits in length.

If i put the code in Textbox_change event, the code will trigger for each number and therefore will always try to run my macro when it gets to 8 digits even if its a 13 digit barcode.

The Text_Afterupdate seems like the better approach but it needs the user to interact with a key for the macro to run.
I need to run as soon as the user scans the barcode because they have to scan a different barcode after the first one.

This is the code im using so far...

Any help would be appreciated.


Thanks.

Code:
Private Const BARCODE_LENGTH As Integer = 13
Private Const MF_BARCODE_LENGTH As Integer = 8
_____________________________________________________________________________________
Private Sub TextBox1_AfterUpdate()


'Code runs after full number is entered and user clicks/tabs to EAN field


If Len(TextBox1.Text) = BARCODE_LENGTH Or Len(TextBox1.Text) = MF_BARCODE_LENGTH Then
        disableClose = True
        Call CheckEANData
Else
        Exit Sub
End If


End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Unless it is possible to consistently distinguish berween an 8 digit barcode and the 1st 8 digits of a 13 digit barcode then you must introduce a trigger AFTER the whole of barcode has been input

I would add an active-x command button that is only visible after the barcode is entered
The user is then required to click on the button before entering the other barcode
Clicking on the button renders the button invisible and runs CheckEANData

Something along these lines...
Code:
Private Sub TextBox1_Change()
    With CommandButton1
        .Visible = True
        If TextBox1.Value = vbNullString Then .Visible = False
    End With
End Sub

Private Sub CommandButton1_Click()
    Dim t As String:    t = TextBox1.Text
    CommandButton1.Visible = False
    Select Case Len(t)
        Case BARCODE_LENGTH, MF_BARCODE_LENGTH
            disableClose = True
            Call CheckEANData
        Case Else
            MsgBox "Bar Code Error?" & vbTab & t
            ClearTextBox
    End Select
End Sub

Private Sub ClearTextBox()
    With Application
        .EnableEvents = False
        TextBox1.Value = vbNullString
        .EnableEvents = True
    End With
End Sub


Supplementary notes

An alternative to...
Code:
Case BARCODE_LENGTH, MF_BARCODE_LENGTH
could be...
Code:
Case 15, 8

If you want to run different macros based on the length of the barcode then...
Code:
   Select Case Len(t)
        Case BARCODE_LENGTH
            disableClose = True
            Call CheckEANData
        Case MF_BARCODE_LENGTH
            disableClose = True
            Call DoSomethingElse
        Case Else
            MsgBox "Bar Code Error?" & vbTab & t
            ClearTextBox
    End Select
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,978
Messages
6,175,755
Members
452,667
Latest member
vanessavalentino83

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