-
[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? :)
-
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?
-
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.
-
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.
-
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
-
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.
-
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
-
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 :D
-
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.
-
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?
-
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
-
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
-
Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox
-
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?
-
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.
-
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!
-
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.
-
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.
-
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?
-
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.
Quote:
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.
-
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.
-
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
-
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.
-
Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox
-
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
Quote:
...
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.
...
-
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.
-
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.
-
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:
If Not runningallready Then
runningallready = True
'if text1.text matches some partnumber then
'end if
DoEvents
End If
runningallready = False
-
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
-
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
-
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