Check input by scanner (1 Viewer)

lookforsmt

Registered User.
Local time
Today, 12:15
Joined
Dec 26, 2011
Messages
672
HI! I have form where user will scan a barcode read as mentioned below.
EN-030117-0001.
How can I stop the user if the barcode scanned starts with "IB" instead of "EN"

I have put the below code on keypress. whether this is the correct way. I already have before and after event which checks for duplicate barcode.

Code:
Private Sub chq_brcd_c_KeyPress(KeyAscii As Integer)
If (KeyAscii = 69 And KeyAscii = 78) Or (KeyAscii = 8) Then
      KeyAscii = KeyAscii
      Else:
      MsgBox ("Barcode must start by 'EN' aphphabet Only!")
      KeyAscii = 0
   End If
End Sub
 

TJPoorman

Registered User.
Local time
Today, 02:15
Joined
Jul 23, 2013
Messages
402
This would probably be best in the after update of your textbox since most scanners move fields after they scan. Then you would simply check for the string and if it doesn't start with "EN" then set the field to "" and set focus.
 

Ranman256

Well-known member
Local time
Today, 04:15
Joined
Apr 9, 2015
Messages
4,337
Code:
if left(keyascii,2)="IB" then
   msgbox "bad entry"
else
    'allow data
endif
 

lookforsmt

Registered User.
Local time
Today, 12:15
Joined
Dec 26, 2011
Messages
672
hi! Ranman, I have updated the code but it accepts all the alphabets including the EN. Is there something I need to amend in the code
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:15
Joined
Aug 30, 2003
Messages
36,126
Code:
if left(keyascii,2)="IB" then

keyascii is the integer value of a single keystroke, thus will never equal "IB". Use the update event as TJ suggested and test the whole scan using the Left() function.
 

lookforsmt

Registered User.
Local time
Today, 12:15
Joined
Dec 26, 2011
Messages
672
sorry guys, one thing I forgot to tell you, I am a beginner, so these codes help will be bouncers to me. if you can tell me simpler way. Re-typing the code just to confirm if I have understood what you are trying to say.

Code:
Private Sub chq_brcd_c_KeyPress(KeyAscii As Integer)
if left(keyascii,2)="TJ" then
   msgbox "bad entry"
else
    'allow data
endifEnd Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:15
Joined
Aug 30, 2003
Messages
36,126
No, that will not work. In the before or after update event of the textbox, more like:

if left(Me.TextboxName,2)="TJ" then

The before update event can be cancelled, so is usually used for validation.
 

lookforsmt

Registered User.
Local time
Today, 12:15
Joined
Dec 26, 2011
Messages
672
I already have a code in the before and after update, as mentioned below.

Code:
Private Sub chq_brcd_c_BeforeUpdate(cancel As Integer)
 Dim SID As String
 Dim stlinkcriteria As String
 Set rsc = Me.RecordsetClone
 SID = Me.Capture_date_c.Value
 stlinkcriteria = "[chq_brcd_c]=" & "'" & SID & "'"

 'Check tbl_Masterchqbrcd for duplicate barcode
 If DCount("chq_brcd_c", "tbl_Master_chqbrcd", stlinkcriteria) > 0 Then
 
 'Undo duplicate entry
 Me.Undo
 
 'Message box warning of duplication
 MsgBox "Warning Cheque Barcode " _
 & SID & " has already been Scanned." _
 & vbcr & vbcr & "Kindly check previous record and Re-scan correct Barcode.", vbInformation _
 , "Duplicate Barcode Information"
 End If
 Set rsc = Nothing
 End Sub

where can I put the below code:
if left(Me.TextboxName,2)="TJ" then
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:15
Joined
Aug 30, 2003
Messages
36,126
Before or after the current test for duplicates, as appropriate to your desired flow. Hopefully you realize that the line I posted alone is just the test.
 

lookforsmt

Registered User.
Local time
Today, 12:15
Joined
Dec 26, 2011
Messages
672
thanks pbaldy,

all I wanted to know where would the line fit in which row, as adding to the code the line provided by you would change the code and it would not work.
As I said I am just new to vba code so I cannot think of any guess work.
So requested your help.

regards
 

lookforsmt

Registered User.
Local time
Today, 12:15
Joined
Dec 26, 2011
Messages
672
Hi! guys

I have tried using the code on keypress event as mentioned below.
It works fine, but I am still not able to stop user if he starts to input with the word BL instead of EN.
My barcode starts with EN-BL-00000995 and the length of the barcode is 14.

Code:
Private Sub chq_brcd_c_KeyPress(KeyAscii As Integer)

    Select Case KeyAscii
        ' 45= -; 48 To 57=Numbers 0 to 9; 69 and 78=EN; 66 and 76=BL
        ' 8 Backspace, 9 Tab Key
    Case 45, 48 To 57, 66, 69, 76, 78, 8, 9
    Case Else
        'Setting KeyAscii to zero cancels the key input
        KeyAscii = 0
        MsgBox ("Code sholuld start with EN and BL on 4th & 5th word in Barcode")
    End Select
  
End Sub

I have two questions.
1) how can I stop the user if the code starts with BL instead of EN
2) If the barcode is less than 14 it should prompt pop-up msg box, barcode is less than 14 digit.

I hope you all guys don't feel I am trying to put my problems on here. I want to tell I am new to vba, so if you feel you can provide me the solution then I request you to help me.
Thanks
 

lookforsmt

Registered User.
Local time
Today, 12:15
Joined
Dec 26, 2011
Messages
672
Hello Mr. Hans, Sorry for the late response.
I don't mind using the input mask, but would prefer the vba code for this. Do you have the input code and the vba code.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 01:15
Joined
Aug 30, 2003
Messages
36,126
I already gave you the test:

if left(Me.TextboxName,2)="TJ" then

You'd add what you want to do if the test is met, and the End If at the end.
 

mjdemaris

Working on it...
Local time
Today, 01:15
Joined
Jul 9, 2015
Messages
426
if you need to check for letters in different positions, you can use Left, Mid, Right, etc depending on where these letters might appear. I did a quick test:
Code:
Private Sub txt2_AfterUpdate()
    Dim a As String
    Dim b As String
    Dim c As String
    a = Me.txt2.Value
    b = Left(a, 2)
    c = Mid(a, 4, 1)
    Debug.Print c
    If Asc(c) > 64 Or Asc(c) < 123 Then MsgBox "more letters in the bar code"
    If b <> "EN" Then MsgBox "EN is not first, " & b
    
End Sub

This checks the ascii key codes from Upper Case A to lower case z, including a couple of special characters between the upper and lower case ranges.
 

lookforsmt

Registered User.
Local time
Today, 12:15
Joined
Dec 26, 2011
Messages
672
thank you everyone.

First I want to apologize for the late response. I was on long leave and was not accessable to my laptop.

thanks once again
 

Users who are viewing this thread

Top Bottom