Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catch keyboard command from Scanner 1

Status
Not open for further replies.

LLBP

IS-IT--Management
Jan 31, 2002
39
US
I am using a keyboard wedge scanner to input data into my Access form. I want to keep the user from having to use the keyboard or mouse. I enabled keypreview on the form and coded some non-alphanumeric keys to move the focus to certain fields or reset the form. I barcoded these symbols and labeled them per function. The scanner is configured to add an <ENTER> command after the data is sent. This moves the focus to the next field during data entry. I need to be able to catch that <ENTER> command when they scan the barcoded symbols to keep it from moving to the next field. I hide some fields based on certain input criteria so I can't set the focus to the previous field and let the <ENTER> get me to the correct field...tried that.

Is there a CLEAR KEYBOARD BUFFER function in VBA? Or, some other function that will effectively do this?

Thanks,
Brent
 
LLBP,

Since the keyboard wedge is sending keystrokes, just test each keystroke. Just attached code to the KeyPress event procedure for each control. Something like this.

Code:
' Test each keystroke........

Select Case KeyAscii
    Case 13            ' Carriage Return
        KeyAscii = 0
        ' your code here...
    Case 45            ' Minus key
        KeyAscii = 0
        ' your code here...
End Select

I hope this will get you close to your goal... [thumbsup2]

 
How can I distinguish between a needed CR (one after data)and one after field movement?
 
Perhaps by maintening yourself a stack of keystrokes, or with static boolean variable(s) ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 


Here are two suggestions...

Get rid of "enter" completly.

Remove it from the scanner.

or

Use the above code to strip it out and create a custom barcode (single ASCI character) that your user must scan to advance to the next input field. Modify your KeyPress event "catch" this character and set focus to the next input field.

Let me know....


 
I think you have to trap Enter and Tab keystrokes in the KeyDown event to override them properly. You can also use a form-wide handler so you don't have to code every control on the form. One trick is to put the name of the next control in the tab order in the "Tag" property of each textbox, so you can move the focus programmatically:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  Dim ctl As Control
  
  On Error Resume Next
  
  [green]'capture reference to the active control.[/green]
  Set ctl = Me.ActiveControl
  
  If Err = 0 And ctl.ControlType = acTextBox Then
    Select Case KeyCode
    
      Case vbKeyReturn
        [green]'nullify[/green]
        KeyCode = 0
        
        [green]'do stuff[/green]
        
        [green]'focus next control[/green]
        Me.Controls(ctl.Tag).SetFocus
        
      Case vbKeyTab
        [green]'nullify[/green]
        KeyCode = 0
        
        [green]'do stuff[/green]
        
        [green]'focus next control[/green]
        Me.Controls(ctl.Tag).SetFocus
        
      Case Else
        [green]'ignore[/green]
    End Select
  Else
    Err.Clear
  End If
End Sub

VBSlammer
redinvader3walking.gif

"You just have to know which screws to turn." - Professor Bob
 
Thanks for the replay VBSlammer. Forgive me, but I don't follow the line [red]Me.Controls(ctl.Tag).SetFocus[/red] in your code. Is the Tag property set somewhere else? How does this line set the focus to the next control?
 
The Tag is a string property available to most of the Access controls. It is there to provide developers with a generic property to be used for whatever we want.

Normally, you would set the TabIndex of each control to set the Tab Order, and let Access do the focusing. Since you're trying to do it through code, it's easy to move the focus using "SetFocus()" if you know the name of the control you want to focus on. Therefore, you can set the Tag property of each control you're updating to the name of the next control you want move focus to.

For example, using textboxes named txtUPC and txtExpDate, I'd set the Tag for txtUPC to "txtExpDate" and set the Tag for txtExpDate to "txtUPC" to move the focus back and forth. Then when txtUPC has the focus (the ActiveControl), its Tag property tells me which control to set focus on next:
Code:
Set ctl = Me.ActiveControl  [green]'assume it's txtUPC[/green]
Me.Controls(ctl.Tag).SetFocus
[green]'is the same as:[/green]
Me.Controls("txtExpDate").SetFocus()
You might not need to do this at all, but if you cancel the Chr(13) and Chr(9) keypresses the focus won't change.

VBSlammer
redinvader3walking.gif

"You just have to know which screws to turn." - Professor Bob
 
Well, it took a lot of trial and error, but I got it working the way I wanted it to. It took a combination of form_keydown() and form_keypress() procedures to make it work. I had to capture the active control in the keydown() procedure in a global variable then do the key checks in the keypress() procedure.

The user only needs to scan a barcode to go to a specific field or reset the form.

Thank you all for your help.

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Set ActCtrl = Me.ActiveControl
            
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
    
    'Scanner sends <ENTER> with scan
    If KeyAscii = 13 Then ' <ENTER>
        If lstkey > 33 And lstkey < 44 Then
            Me.Controls(ActCtrl.Tag).SetFocus
        End If
    End If
    Select Case KeyAscii
        Case 33 ' !
            err = ResetForm()
        Case 35 ' # Setfocus to Code A via Scanner
            Me.Code_1.SetFocus
        Case 36 ' $ Setfocus to Serial A via Scanner
            Me.Serial_1.SetFocus
        Case 37 ' % Setfocus to Code B via Scanner
            Me.Code_2.SetFocus
        Case 38 ' & Setfocus to Serial B via Scanner
            Me.Serial_2.SetFocus
        Case 43 ' + Setfocus to CoidID via Scanner
            Me.CoilID.SetFocus
    End Select
    
    lstkey = KeyAscii
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top