Hi all, first time poster here.
I am looking into solutions for making a USB barcode scanner (ie "keyboard wedge" type that emulates the keyboard) more useful in conjunction with Excel, via VBA / Userforms etc.
I have some current solutions involving AutoHotkey, but am looking into what VBA etc can offer me.
I am, however, pretty much new to VBA...
The kind of features that I am looking to implement are:
I have managed to knock up some proof-of-concept code (Largely cobbled together from stuff on this site). The "preamble character" in this case is Tilde (~) and most of the code is concerned with giving the TextBox TextScannerInput focus whenever the tilde character is pressed.
UserForm1:
Class Module CControls:
Regarding this code:
Is there not a better way to add the _KeyPress listener to all controls rather than having to make arrays for each type of control?
Failing that, is there at least a way to handle pushing to the arrays in UserForm_Initialize better? This ReDim business seems like a pain in the rear end, I tried UBounds() to avoid having to keep individual counters, but couldn't get it to work.
Anyway, I suppose the main reason I am posting is to seek advice on the best way in general of approaching this project.
Some thoughts:
Any thoughts appreciated. TIA
I am looking into solutions for making a USB barcode scanner (ie "keyboard wedge" type that emulates the keyboard) more useful in conjunction with Excel, via VBA / Userforms etc.
I have some current solutions involving AutoHotkey, but am looking into what VBA etc can offer me.
I am, however, pretty much new to VBA...
The kind of features that I am looking to implement are:
- Spreadsheet protection
Stop the barcode scanner from overwriting data in Excel (ie user pulls trigger while cursor is in cell), whilst still allowing editing of the spreadsheet via the keyboard.
Likely to be handled by trapping a "preamble" character that the scanner is configured to send before typing whatever it scanned. - Quick Searching
Allow the user to see if the scanned item is present in the spreadsheet.
Audible feedback would be nice. - Audit Mode
Keeps some record of which items present in the spreadsheet have been found.
Maybe with "Found x of y" counter.
Option in userform to select a column on spreadsheet to mark when found (Change colour, put Y in cell etc) - Stock Take Mode
For compiling a list of what is where. Barcodes are created with a prefix (eg !StockRoom01) and when one of those is scanned, scanning subsequent barcodes will add the scanned item to the StockRoom01 worksheet.
I have managed to knock up some proof-of-concept code (Largely cobbled together from stuff on this site). The "preamble character" in this case is Tilde (~) and most of the code is concerned with giving the TextBox TextScannerInput focus whenever the tilde character is pressed.
UserForm1:
Code:
Option Explicit
Dim TextBoxes() As New CControls
Dim TextBoxCount As Long
Dim OptionButtons() As New CControls
Dim OptionButtonCount As Long
' Add Listener for Tilde (~) Key to all Controls
Private Sub UserForm_Initialize()
Dim ctl As MSForms.Control
TextBoxCount = 1
OptionButtonCount = 1
For Each ctl In Me.Controls
Select Case TypeName(ctl)
Case "TextBox"
ReDim Preserve TextBoxes(1 To TextBoxCount)
Set TextBoxes(TextBoxCount).TextGroup = ctl
TextBoxCount = TextBoxCount + 1
Case "OptionButton"
ReDim Preserve OptionButtons(1 To OptionButtonCount)
Set OptionButtons(OptionButtonCount).OptionGroup = ctl
OptionButtonCount = OptionButtonCount + 1
End Select
Next ctl
End Sub
' Detect Enter key pressed in TextScannerInput
Private Sub TextScannerInput_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then
'MsgBox "It's your thang..." & vbCrLf & "Do whatcha wanna do...", 64, "Enter key was pressed."
KeyCode = 0
End If
End Sub
Class Module CControls:
Code:
Option Explicit
Public WithEvents TextGroup As MSForms.TextBox
Public WithEvents OptionGroup As MSForms.OptionButton
Private Sub TextGroup_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
KeyAscii = PreambleCheck(KeyAscii)
End Sub
Private Sub OptionGroup_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
KeyAscii = PreambleCheck(KeyAscii)
End Sub
Private Function PreambleCheck(KeyAscii) As Integer
' Check for preamble character from barcode scanner
If KeyAscii = 126 Then
PreambleCheck = 0
UserForm1.TextScannerInput.Value = ""
UserForm1.TextScannerInput.SetFocus
Else
PreambleCheck = KeyAscii
End If
End Function
Regarding this code:
Is there not a better way to add the _KeyPress listener to all controls rather than having to make arrays for each type of control?
Failing that, is there at least a way to handle pushing to the arrays in UserForm_Initialize better? This ReDim business seems like a pain in the rear end, I tried UBounds() to avoid having to keep individual counters, but couldn't get it to work.
Anyway, I suppose the main reason I am posting is to seek advice on the best way in general of approaching this project.
Some thoughts:
- How would one make the code portable such that it can be added to any spreadsheet?
Ideally by semi-technical users.
ie what is the best way to allow people to add the userform and all associated logic to any spreadsheet they wish? - Is there a way to trap the preamble character (eg ~) whilst the focus is a cell in the spreadsheet, rather than the userform?
ie, if user has a cell selected when he pulls the scanner trigger, set the focus to the userform input box without altering the spreadsheet.
Solution should also not make point (1) more complicated if possible.
I could probably solve this with AHK, but an all-excel solution would be nice.
Any thoughts appreciated. TIA