|
-
Jan 15th, 2009, 02:01 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Jan 15th, 2009, 02:02 PM
#2
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?
-
Jan 15th, 2009, 02:09 PM
#3
Thread Starter
Hyperactive Member
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.
-
Jan 15th, 2009, 02:13 PM
#4
Re: Have an event fire as soon as a part number is scanned in a textbox
 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.
-
Jan 15th, 2009, 02:20 PM
#5
Thread Starter
Hyperactive Member
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
-
Jan 15th, 2009, 02:38 PM
#6
Thread Starter
Hyperactive Member
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.
-
Jan 15th, 2009, 02:48 PM
#7
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
-
Jan 15th, 2009, 02:51 PM
#8
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
-
Jan 15th, 2009, 03:25 PM
#9
Thread Starter
Hyperactive Member
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.
-
Jan 15th, 2009, 03:44 PM
#10
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?
-
Jan 15th, 2009, 04:00 PM
#11
Thread Starter
Hyperactive Member
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
-
Jan 15th, 2009, 04:01 PM
#12
Re: Have an event fire as soon as a part number is scanned in a textbox
 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
-
Jan 15th, 2009, 04:02 PM
#13
Thread Starter
Hyperactive Member
Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox
-
Jan 15th, 2009, 05:39 PM
#14
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?
-
Jan 15th, 2009, 06:43 PM
#15
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.
-
Jan 15th, 2009, 06:53 PM
#16
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!
-
Jan 15th, 2009, 07:38 PM
#17
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.
-
Jan 15th, 2009, 09:08 PM
#18
Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox
 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.
-
Jan 16th, 2009, 12:27 AM
#19
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?
-
Jan 16th, 2009, 01:52 AM
#20
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.
-
Jan 16th, 2009, 12:01 PM
#21
Thread Starter
Hyperactive Member
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.
-
Jan 16th, 2009, 12:18 PM
#22
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
-
Jan 16th, 2009, 02:14 PM
#23
Thread Starter
Hyperactive Member
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.
-
Jan 16th, 2009, 02:57 PM
#24
Re: [GAVE UP] Have an event fire as soon as a part number is scanned in a textbox
-
Jan 17th, 2009, 08:27 AM
#25
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.
...
-
Jan 17th, 2009, 02:06 PM
#26
Junior Member
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.
-
Jan 19th, 2009, 01:31 PM
#27
Junior Member
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.
-
Jan 19th, 2009, 03:40 PM
#28
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
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
-
Jan 19th, 2009, 04:21 PM
#29
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.
-
Jan 20th, 2009, 05:27 PM
#30
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?? 
-
Jan 20th, 2009, 07:30 PM
#31
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|