问题
At a road block here. Simple user form using three text boxes, one for user id, two to enter serial number using hand scanner. User loads excel file, userform.show loads, user enters id then simple validation to verify numberic number, then focus set on first textbox, user scans barcode to input serial number, again simple validation to ensure numeric and length, same with last textbox, scan serial number, validate first textbox entry matches second textbox entry.
Hand scanner is used to input serial number and also returns a "return carriage" character; e.g. enter button press after serial number scan.
Using "return carriage" to fire textbox_exit event handler. Issue is very intermittent but consistent. I load userform, input data, when record is complete, data is transferred to object worksheet. However, when troubleshooting, I initially open workbook and userform, create a few records, save, and close. Everything works well and data is recorded and archived. Issue generally arises when i load workbook a second time, enter data for one record, save, and begin second record. Once serial number is entered into first textbox, exit event never fires using "return carriage". I can manually transfer focus to other objects; e.g. diff textbox, but the overall operation is not as expected.
I have tried inserting application.eventhandler=true commands, different event handlers, as well as numerous code changes; e.g. exit sub at end of IF statements, to make this work.
Thought I would reach out to the community for some feedback. FYI, issues still arises if I simulate hand scanner using copy/paste and enter key.
Example of exit event handler for first serial textbox below.
Private Sub SerialIn_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = False
If Not IsNumeric(SerialIn.Value) Then 'validate serial number is numeric
'error msg serial number is not numeric
Msg = "Opps, something went wrong! Serial number was incorrect." _
& vbNewLine & vbNewLine & "Rescan module serial number."
MsgBox Msg, vbCritical, "Warning" 'display msg
Cancel = True 'stop user from changing focus
SerialIn.SelStart = 0 'highlight user text
SerialIn.SelLength = Len(SerialIn.Value) 'select user text
'Image1.Picture = LoadPicture(StopLightRed) 'display red stop light
Exit Sub
Else
If Not Len(SerialIn.Value) = 19 Then 'validate serial number length
'error msg incorrect length
Msg = "Opps, something went wrong! Serial number was incorrect." _
& vbNewLine & vbNewLine & "Rescan module serial number."
MsgBox Msg, vbCritical, "Warning"
Cancel = True 'stop user from changing focus
SerialIn.SelStart = 0 'highlight user text
SerialIn.SelLength = Len(SerialIn.Value) 'select user text
'Image1.Picture = LoadPicture(StopLightRed) 'display red stop light
Exit Sub
Else
SerialInTime.Caption = Now 'record date and time
'Image1.Picture = LoadPicture(StopLightYellow) 'display yellow WIP stop light
Me.SerialOut.SetFocus
Exit Sub
End If
End If End Sub
回答1:
The Exit
event has more to it than meets the eye. On most forms, one enters a value in a textbox and then clicks "OK", a command button. Tbx.Exit event occurs. It also occurs if you click anywhere else, such as a button to close the form. In your system the CR triggers the event as well, and that would appear to be the problem.
When a CR or TAB is entered in your Tbx the form's tapping sequence takes over. The exit occurs, followed by the enter event of the next control in your tapping order. (Compare that to the manual change of focus that occurs when the user determines the next control.)
So, the solution should be not to use the Exit event but to let the CR initiate a change of focus, using the tapping order setting, which lands the focus on an "OK" button, then let that button's Enter event trigger its Click event (if it doesn't do that by default).
You probably want a cycle there, where the CR triggers the worksheet entry and the focus comes back to the Tbx. That's one more reason not to use the Exit event because you don't want an entry made in the worksheet when you exit the Tbx to close the form.
However, perhaps your form doesn't have or need an "OK" button. In that case I would recommend the BeforeUpdate
event of the Tbx. Both Exit
and BeforeUpdate
occur in close succession. The sequence isn't important however (though that's not the reason why I forgot it :-)) but their nature. Use the exit event in the context of form control, setting focus or reacting to focus change. BeforeUpdate
obviously refers to data. Each of these events is fine tuned to its own task and therefore does it better than the other.
回答2:
If you want to detect the "Enter" from the scanner then use the Change
event to check if the textbox value ends with vbCrLf
or vbLf
(in that order): if it does, then trigger the "scan" action.
Note you need to set your textbox to "multiline=true" in order for the Change event to capture the enter key.
Private Sub TextBox1_Change()
Dim v
v = ScannedValue(TextBox1.Text)
If Len(v) > 0 Then
TriggerScanAction v
TextBox1.Value = ""
End If
End Sub
'check if a value ends with vbcrlf or vblf
' - if yes strip that off and return the rest
' - otherwise returns empty string
Function ScannedValue(vIn) As String
Dim rv As String
If Right(vIn, 2) = vbCrLf Then
ScannedValue = Replace(vIn, vbCrLf, "")
ElseIf Right(vIn, 1) = vbLf Then
ScannedValue = Replace(vIn, vbLf, "")
End If
End Function
'execute some action triggered by a scanned value
Sub TriggerScanAction(v)
MsgBox "You scanned " & v
End Sub
来源:https://stackoverflow.com/questions/59870537/excel-vba-textbox-exit-event-handler