Question Read barcodes into database

chowchow123

Active member
Joined
Sep 6, 2007
Messages
34
Programming Experience
1-3
I want to develop a small application that reads in scanned barcodes into a database table - Would a vb.net form based application that connects to the database be best approach? What I'm think is each time the user scans in the barcode - a message appears on the form saying the barcode that has been scanned.

1.Would I need to program anything within the program to check whether the barcode scanner is connected (or is this not necessary since it works like a keyboard device?)
2. How could I automate the scanning in without requiring any user intervention like a button which saves it to the database - so basically as soon as a barcode is scanned in - it is saved to the database and the next barcode can be scanned in etc.

Cheers

Chowchow
 
Hello.

Barcode-Scanners are emulating Keyboard-input, at least most of them do. Means all you'd need would be a form with a Label on it "Please scan the barcode" und processing the ProcessCmdKey function of that form. Also, most of the scanners are ending the barcode with a CrLf, Lf or Cr so that you know that it ends...something like this should do:

VB.NET:
Private prv_scannedBarcode As String = String.Empty

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.A To Keys.Z
                Me.prv_scannedBarcode &= keyData.ToString()
            Case Keys.NumPad0 To Keys.NumPad9
                Me.prv_scannedBarcode &= keyData.ToString()(6)
            Case Keys.D0 To Keys.D9
                Me.prv_scannedBarcode &= keyData.ToString()(1)
            Case Keys.Return, Keys.Enter
                Me.saveScannedBarcode()
                Me.prv_scannedBarcode = String.Empty
            Case Keys.Escape
                Me.Close()
            Case Else
                'do nothing?
        End Select

        ' We won't return anything, since there isn't something to return...
        ' Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

You can save the Barcode within saveScannedBarcode(). This way you can also type a barcode without the need for a scanner.

Bobby
 
There is a problem with the code.. It only catch number and lowercase letters BUT NOT CAPITAL LETTERS.

Try scan this barcode: R2SP3456 will see it as 23456
 
There is no difference in Keys values for letter case (look at your keyboard), casing is a modifier operation with Shift key, where lower case has no modification and upper case has (or opposite if Caps lock). You can use bitwise operation to see if keyData contains Keys.Shift.

ProcessCmdKey is rather low-level, I would probably rather set a forms KeyPreview property to True and handle its KeyUp or KeyDown event, where you can easier check e.KeyCode and for letters check e.Shift.
 
I think you are right. I see Keydata value in the range of 65000. It actually contain Keys.ShiftKey{65536} OR Keys.Shift {16} = 65552.
Scanning a single "A" give me 65601 ( 65536+65)

In a clumsy way, the code below works for me

VB.NET:
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

Select Case keyData
   Case Keys.Shift + Keys.A To Keys.Shift + Keys.Z
       Me.scannedBarcode &= keyData.ToString()(0)
   Case Keys.A To Keys.Z
       Me.scannedBarcode &= keyData.ToString()
   Case Keys.NumPad0 To Keys.NumPad9
       Me.scannedBarcode &= keyData.ToString()(6)
   Case Keys.D0 To Keys.D9
       Me.scannedBarcode &= keyData.ToString()(1)
   Case Keys.Return, Keys.Enter
      Label1.Text = scannedBarcode
      Me.ValidateScannedBarcode()
      Me.scannedBarcode = String.Empty
   Case Else
   'do nothing?
End Select


' We won't return anything, since there isn't something to return...
' Return MyBase.ProcessCmdKey(msg, keyData)
End Function



How do I go about bitwise operation?
 
Last edited:
How do I go about bitwise operation?
Keys.ShiftKey{65536} OR Keys.Shift {16} = 65552
That's a bitwise combine.

To check if keyData contains shift modifier, and extract value without it:
        Dim hasShift = (keyData And Keys.Shift) = Keys.Shift
        Dim value = keyData And Not Keys.Shift

That said, why not do it the easy way and and use KeyDown or KeyUp event?
 
Correct me if I am wrong, but the KeyPress event does require KeyPreview to be true, and lot of applications dont have attach mouse, keyboard ... With the ProcessCMdKey do not have to worry about losing the focus.
 
lot of applications dont have attach mouse, keyboard ... With the ProcessCMdKey do not have to worry about losing the focus.
These messages comes from the message loop, through the windows WndProc, there can be lots of "keyboard" handlers and ProcessCmdKey is just one of them (at an early stage), whether the window has focus or the system has other input devices (like physical keyboard) is of no regard if a process is sending such messages to this window. Getting KeyUp events etc has inherently nothing to do with focus, although pressing a key on a keyboard will normally result in a window message being sent to the window that has focus.
bachphi said:
the KeyPress event does require KeyPreview to be true
I said KeyUp/KeyDown, not KeyPress. The forms KeyPreview property must be set to True for it to raise the Key events, unless the form has no visible or enabled controls. And so what if you have to set a property?

You may find this article about keyboard messaging interesting: Probably more than you want to know about keyboarding in Windows Forms…. - jfo's coding - Site Home - MSDN Blogs
 
Thanks for the link, clear as mud after reading. I found another link that help me a little bit more

What's the difference between KeyDown and KeyPress in .NET? - Stack Overflow

Below is the code that use OnKeyUp
VB.NET:
Private scannedBarcode As String = String.Empty    
Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
        MyBase.OnKeyUp(e)
        If (e.KeyCode >= 65 And e.KeyCode <= 90) Then
            scannedBarcode &= e.KeyCode.ToString
            Label3.Text = scannedBarcode
            e.Handled = True
        End If
        If (e.KeyCode >= 48 And e.KeyCode <= 57) Then
            scannedBarcode &= e.KeyCode.ToString()(1)
            e.Handled = True
        End If
    End Sub

But I still need to use ProcessCmdKey to suppress the enter key at the end of scan, otherwise it will jump/open another form ( i have a button to goto another form). In a case , where barcode has lowercase, I think using KeyPress will preserve that.
 
I still need to use ProcessCmdKey to suppress the enter key at the end of scan, otherwise it will jump/open another form ( i have a button to goto another form)
That is what ProcessDialogKey method is for.
 
I went back and reread the link , it sounded like ProcessDialogKey can suppress with Return,Enter key but with KeyDown event and not KeyUp.
 
That is correct, as is written, there is no preprocessing for KeyUp events. The button you mentioned won't be pressed if there is no KeyDown though.
This article is similar and may be tidier to read: How Keyboard Input Works
 
Back
Top