Results 1 to 31 of 31

Thread: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    Hi,
    I'm writing a program to allow users to scan a part number (encoded in a Code39 barcode) into a text box. Originally I was going to have a Submit button right beside it but then I thought maybe I don't need the button...

    I've seen some programs where as soon as you scan text into a textbox it triggers an event automatically without the need to click a button.

    Does that make any sense?
    Last edited by Tomexx; Jan 15th, 2009 at 04:01 PM.
    Thanks

    Tomexx.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Have an event fire as soon as a part number is scanned in a textbox

    I think the TextChanged event of the textbox should fire. Have you tried that?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Have an event fire as soon as a part number is scanned in a textbox

    I was thinking that TextChanged would be firing for every character in the part number as they come in from the scanner.

    Have to go to a scanner and try it.
    Thanks

    Tomexx.

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Have an event fire as soon as a part number is scanned in a textbox

    Quote Originally Posted by Tomexx
    I was thinking that TextChanged would be firing for every character in the part number as they come in from the scanner.

    Have to go to a scanner and try it.
    I think it would fire only once.
    But just in case it fires multiple times, you could check the length of the textbox text and ignore the event when the length is less than what is expected.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Have an event fire as soon as a part number is scanned in a textbox

    Pradeep1210,
    Good idea but can't do that since the part numbers are between 6 and 11 characters each
    Thanks

    Tomexx.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Have an event fire as soon as a part number is scanned in a textbox

    Yeah, textbox.Change fires multiple times when scanning barcodes.
    Thanks

    Tomexx.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Have an event fire as soon as a part number is scanned in a textbox

    In previous threads on this topic, people have said that the barcode is followed by an end of line marker - but I can't remember whether that is vbCr, vbLF, or vbNewLine

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Have an event fire as soon as a part number is scanned in a textbox

    I think you are doing something wrong somewhere.
    Are you using some API to read the scanner? A barcode has starting code and an ending code (2 lines). So the API should have some way to tell you that it is finished reading the barcode.


    EDIT: Aaah... si beat me at that
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Have an event fire as soon as a part number is scanned in a textbox

    I just scan the barcode directly to a textbox, no APIs or anything else.
    This is equivalent to typing the P/N in.
    The same goes when you open notepad and scan it there. It's just one line without any control characters.
    Thanks

    Tomexx.

  10. #10
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Have an event fire as soon as a part number is scanned in a textbox

    What is it that you want now then? You wanted an event to fire when you scan it and you have the Change event. Or do you want something else more?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Have an event fire as soon as a part number is scanned in a textbox

    What I wan't now... is explained the 1st post. I was hoping to scan the Code39 barcode and fire some event. The Change event fires after every character in the barcode, so that's no good.

    However doesn't look like I'm gonna be able to do this, so I'll just go back to my original idea and have them scan the barcode and click the button.

    Thanks to all
    Thanks

    Tomexx.

  12. #12
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Have an event fire as soon as a part number is scanned in a textbox

    Quote Originally Posted by Tomexx
    What I wan't now... is explained the 1st post. I was hoping to scan the Code39 barcode and fire some event. The Change event fires after every character in the barcode, so that's no good.

    However doesn't look like I'm gonna be able to do this, so I'll just go back to my original idea and have them scan the barcode and click the button.

    Thanks to all

    How about using a temp textbox and a timer set to say 500ms, send the scan to the temp box and reset the timer on each change event, in the timer event have it stop itself and copy the temp text to the scan textbox.


    Code:
    Option Explicit
    
    Private Sub Form_Load()
        TmrDelayScan.Interval = 500
    End Sub
    
    Private Sub TmrDelayScan_Timer()
        TmrDelayScan.Enabled = False
        TxtScanned = TxtTemp
    End Sub
    
    Private Sub TxtScanned_Change()
    ' new scan
    End Sub
    
    Private Sub TxtTemp_Change()
        TmrDelayScan.Enabled = False
        TmrDelayScan.Enabled = True
    End Sub

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    I'll try it tomorrow.
    Thanks

    Tomexx.

  14. #14
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    I'm curious, how does scanning a barcode put anything at all in your textbox?

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    Apparently they act like a keyboard, so will send the text to whatever has the focus.

  16. #16
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    Some kind of barcode scanner (reader) just acts like a keyboard input.

    You beat me si!
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  17. #17
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    If the scanner sends a trailing CR, Tab, etc. that can be detected via the KeyXXXX events. Just look for the terminating "keystroke." If there is no terminator but a fixed number of characters use the Change event and check until the length of the Textbox contents is the final length.

    Pretty routine stuff.

  18. #18
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    Quote Originally Posted by dilettante
    Pretty routine stuff.
    But the OP is basically saying, there are no terminating characters and the numbers can vary in length, plus each character of the number fires the change event when sent to a textbox.
    Last edited by Edgemeal; Jan 15th, 2009 at 09:16 PM.

  19. #19
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    Doesn't it end (and start) with asterisk according to this article?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  20. #20
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    As far as I know the barcode reader has a beginning character, data and then an ending character. I'm not sure how you are using; but if you apply the correct method you will always get the ending character. Look for some API sort of if your barcode scanner came with a CB or look online on the manufacturer's site.

    How about using a temp textbox and a timer set to say 500ms, send the scan to the temp box and reset the timer on each change event, in the timer event have it stop itself and copy the temp text to the scan textbox.
    This could be a good workaround, but you can always run into problems. You can't guarantee 100% accuracy. Using a timer in VB is risky as the interval is not very accurate. If you set the interval to a high value, you will unnecessarily waste time.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    Pradeep1210 and others,
    I think it's up to the barcode creator what he encodes in the barcode. In this case only the partnumber itself is encoded (I checked it by scanning the p/n in notepad). So no control characters.

    As for my original post where I saw another program where you scan a barcode into a Username field and upon scanning, the cursor goes automatically goes to the Password field ready to be entered, it's not an event firing as I first thought. I scanned that "Username" & "Password" barcodes in notepad and I could see that the Username value has a TAB after it which makes the cursor go to the next textbox, and the Password value had has a CR in it to trigger enter button (it scanned the password in notepad and I saw the cursor go to the next line).

    So I went back to my original plan (scan a P/N and click the button). The advantage here is that the user can scan OR type the part number if the scanner stops working.
    Thanks

    Tomexx.

  22. #22
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    There still may be an unprintable character at the end of the string. Go ahead and add this to your form somewhere, scan a barcode in and then click the button and post the results.
    Code:
    Private Sub Command1_Click()
    Dim i As Integer
    Dim strTemp As String
    Dim txt As TextBox
    
        Set txt = Text1 '<-- change textbox name as needed
    
        For i = 1 To Len(txt.Text)
            strTemp = strTemp & Asc(Mid(txt.Text, i, 1)) & " "
        Next i
        
        Debug.Print Trim(strTemp)
    End Sub

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    MarkT,
    Tried your code and the only ascii codes showing are those of the actual text entered.
    Thanks

    Tomexx.

  24. #24
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    Nothing less than 48?

  25. #25
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    Still can't figure out how you are able to read the barcode.
    Since you are using Code39 barcode, here is what their site say about this:
    http://www.code39barcodes.com
    ...
    The asterisk ( * ) is used as the Code 39 start bar and stop bar. All Code 39 bar codes begin and end with an asterisk. For example: *ELMER* scans as ELMER.
    ...
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  26. #26
    Junior Member
    Join Date
    Jan 2009
    Posts
    16

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    If his bar code scanner is like the one I have and have not played with in a while the firmware in the actual bar code scanner uses the * internally during the read but DOES NOT report it as scanned output. So the output the scanner provides is ONLY the SCANNED CODE between the ASTERISK's.

    Bill.

  27. #27
    Junior Member
    Join Date
    Dec 2008
    Posts
    17

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    Try this code. I guess that the barcode ends with vbkeryreturn. If am right, this code should work for you.

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii = vbKeyReturn Then
            MsgBox "Fire the code you need here"
        End If
    End Sub
    Please let me know.

  28. #28
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    you can in your textbox change event set a global boolean so it will not run again while it is already running
    then check if the part number is valid, if not exit and allow the change event to fire again
    like
    vb Code:
    1. If Not runningallready Then
    2.     runningallready = True
    3.     'if text1.text matches some partnumber then
    4.    
    5.     'end if
    6.     DoEvents
    7. End If
    8. runningallready = False
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  29. #29
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    I also have got a barcode scanner here next to me. The scanner reads the code sequence, then emulates keystrokes one-by-one to pasting the received code. Theres no closing trail or anything else, but it can be managed to do!

    If you dont have any control panel for the scanner, there should be a manual for the scanner, that have some printed special codes, that is for managing the settings of the scanner. Simply, you just have to scan these spec codes, to apply any settings, just like you can select a closing (or opening/closing) trails, just like (*) asterisk or carriage-return+linefeed keystrokes. Based on what the abilities of the firmware/hardware of your scanner.

    To get the code, you can use the TextBox's Change event, you just have to count on how much characters are received.
    Some example:
    Code:
    Private Sub Text1_Change()
    Dim sStart As Long
      If Len(Text1.Text) < 6 Or Len(Text1.Text) > 11 Then Exit Sub
      'remove spacebars
      sStart = Text1.SelStart
      Text1.Text = Replace$(Text1.Text, " ", "") 'you can do more extended filters here
      If sStart > Len(Text1.Text) Then
        Text1.SelStart = Len(Text1.Text)
      Else
        Text1.SelStart = sStart
      End If
     
      '
    End Sub
    Last edited by Jim Davis; Jan 19th, 2009 at 04:28 PM.

  30. #30
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    This approach is not ideal but it does work. You can shorten the Timer interval to what will work for the longest scan you think you'll encounter. I added the Beep API because I doubt that you will be using a MsgBox. Therefore you will need an alert to begin the next scan.

    Code:
    Option Explicit
    Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
    
    Private Sub Form_Load()
       Timer1.Enabled = False
       Timer1.Interval = 3000          ' Make this long enough to complete a scan
    End Sub
    
    Private Sub txtScannedTxt_Change()
       Timer1.Enabled = True           ' Fires on the first chr from scanner
    End Sub
    
    Private Sub Timer1_Timer()         ' Fires 3 seconds after the first chr_
       Dim strScan As String           ' is entered into txtScannedTxt
       strScan = txtScannedTxt.Text
       MsgBox "Do you want to submit " & strScan & " ?"        ' Do something.
       Timer1.Enabled = False
       Beep 800, 1000                  ' Alert user that scanner is ready for_
    End Sub                            ' another scan
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  31. #31
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox

    I found a little glitch in the code I posted previously. It would fire the Change event when I tried to clear the textbox to ready it for the next scan. This is the same code with a added counter intReset that takes care of that.

    Code:
    Option Explicit
    Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
    Dim intReset As Integer
    
    Private Sub Form_Load()
       intReset = 0
       Timer1.Enabled = False
       Timer1.Interval = 3000          ' Make this long enough to complete a scan
    End Sub
    
    Private Sub txtScannedTxt_Change()
       If intReset = 0 Then               ' Fire Timer only when intReset = 0
          Timer1.Enabled = True           ' Fires on the first chr from scanner
       End If
    End Sub
    
    Private Sub Timer1_Timer()         ' Fires 3 seconds after the first chr_
       Dim strScan As String           ' is entered into txtScannedTxt
          intReset = intReset + 1
       strScan = txtScannedTxt.Text
          MsgBox "Do you want to submit " & strScan & " ?"        ' Do something.
       Timer1.Enabled = False
          Beep 800, 1000           ' Alert user that scanner is ready for another scan
       txtScannedTxt.Text = ""     ' At this point IntReset = 1 and will not_
          intReset = 0             ' trigger the txtScannedTxt_Change() event until 0.
    End Sub
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width