Scanning Bar Code - Event and Coding Help

DavidWE

Registered User.
Local time
Today, 12:56
Joined
Aug 4, 2006
Messages
76
I am scanning a bar code into a textbox called txtBar. After each scan, a record is created and other code is executed. Then the txtBar textbox is cleared and the focus is automatically returned to txtBar. Currently all bar codes are of length 15 and it works perfectly using the On Change event. We are now moving to a 19 character bar code. During the transition period some bar codes of both lengths will have to be scanned. I can no longer use the On Change event with variable length tickets. I am attempting to code it so that when the user presses Enter or some other key, the code will check the length of the bar code and perform the correct code. The following code currently works for scanning 15 character bar codes only:

Code:
Public Sub txtBar_Change()
  If Len(txtBar.Text) = 15 Then
     'code for extracting characters from bar code, querying
     ' database and creating a record
     txtBar.Text = " "
  End If
End Sub

After switching to 19 character bar codes, it will look something like this:

Code:
Private Sub txtBar_Enter()
  If Len(txtBar.Text) = 15 Then
     'code for extracting characters from bar code, querying
     ' database and creating a record
     txtBar.Text = " "
  If Len(txtBar.Text) = 19 Then
     'code for extracting characters from bar code, querying
     ' database and creating a record
     txtBar.Text = " "
  End If
End Sub

After I scan a bar code, the cursor tabs to the next textbox on the form. If I then click back on txtBar the above code is executed and does what it is supposed to do, but I don't want the user to have to click back into the textbox.

I would like the focus to stay in txtBar and have the user press Enter or another key to execute the above code. I have tried txtBar.SetFocus in the After Update and On Exit events, but it still tabs to the next field.

Does anyone have any suggestions? Is there another event or another way to get the results I want?

Thanks,
David
 
Shortly after posting the above question I found a solution that looks like it will work. I added a command button and put its tab order immediately after the txtBar textbox. The click event executes the code. Since the button has the focus, the user can hit enter or click it.

I am still open to any other suggestions.
 
Many scanners are programmable, and can send a CRLF or some other character following the bar code, and that could be used to trigger some action in AfterUpdate, irrespective of the number of barcode characters transmitted.
 
Thanks, Spike. I had earlier downloaded the manual for my scanner and was just getting ready to try to figure out if I can do what you suggest. I'll know later.
 
"After I scan a bar code, the cursor tabs to the next textbox on the form." this implies the scanner is already sending a crlf, otherwise you would not have the cursor tabbing on.

You should put your decoding stuff in the AfterUpdate event.

Then perhaps in OnLostFocus set focus back to the same control. (and figure out some logic allowing escape from this)
 
I did try putting the code in the AfterUpdate event and just tried it again. After scanning the code executes but then gives an error on txtBar.Text = " ". The error message is
Run time error 2115
The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Microsoft Access from saving the data in the field.

I don't have an event set for BeforeUpdate. At this point, the data is not yet saved to the database. I don't know what could be causing the ValidationRule error. I only get this error on the AfterUpdate event.

Code:
Private Sub txtBar_AfterUpdate()
  If Len(txtBar.Text) = 15 Then
     'code for extracting characters from bar code, querying
     ' database and creating a record
     txtBar.Text = " "
  If Len(txtBar.Text) = 19 Then
     'code for extracting characters from bar code, querying
     ' database and creating a record
     txtBar.Text = " "
  End If
End Sub
 
I have also tried using the LostFocus event in combination with AfterUpdate. It still tabs to the next field in the form.

Code:
Private Sub txtBar_LostFocus()
  txtBar.SetFocus
  txtBar.Text = " "
  
End Sub
 
Is there a way to prevent tabbing to the next field after scanning? I might be able to get this to work in the AfterUpdate event without the user having to press Enter if I can keep the cursor in the textbox receiving the scan.
 
In case anyone is interested, I did find a solution. After scanning, the cursor tabs to a command button. I set the OnGotFocus event for the button to txtBar.SetFocus (the textbox that receives the scan). The code is executed in the AfterUpdate event of txtBar before tabbing to the command button. This is a roundabout way to get what I want. I'm sure there is a more straightforward solution.

Thanks again for the suggestions above. Some of the comments steered me in the right direction.

Code:
Private Sub txtBar_AfterUpdate()
  If Len(txtBar.Text) = 15 Then
     'code for extracting characters from bar code, querying
     ' database and creating a record
  ElseIf Len(txtBar.Text) = 19 Then
     'code for extracting characters from bar code, querying
     ' database and creating a record
  End If
End Sub

Code:
Private Sub cmdProcessScan_GotFocus()
   txtBar.SetFocus
End Sub
 

Users who are viewing this thread

Back
Top Bottom