我有一个表单,上面有3个TextBox控件:股票代码、数量、证书编号。股票代码TextBox设置为在加载表单时自动聚焦。
我还将条形码扫描器附加到我的PC上,因为用户希望能够扫描条形码来填充TextBox,或者手动输入数据。
正在扫描的标签包含两个条形码。一个是证书编号,另一个是股票代码。
股票条形码的前缀是"SBC/",而证书条形码的前缀是"C/“。
当用户扫描条形码时,如果焦点中的TextBox是股票代码TextBox,那么我想运行下面的检查。
Private Sub txtStockCode_Change()
On Error GoTo errError1
If Len(txtStockCode.Text) >= 5 Then
If bChangeCode Then
If Left(txtStockCode.Text, 2) = "C/" Then
msgbox "You have scanned the certificate barcode; please scan the stock barcode."
txtStockCode.Text = ""
Else
bChangeCode = False
txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
End If
End If
End If
Exit Sub假设当前的焦点是股票代码TextBox。
如果扫描了股票条形码,则应发生以下情况:
TextBox文本值以删除所有*和前缀"SBC/“例如。"SBC/A12-TR0*“改为"A12-TRO”
和
MsgBox发送给用户TextBox值重置为"“。但是,无论哪种代码被扫描到股票代码TextBox中,该值都不会被验证。
例如。"SBC/A12-TR0*“仍为"SBC/A12-TR0*”,"C/29760“仍为"C/29760”。
由于验证代码在证书TextBox中是相同的,所以重复相同的模式,反之亦然。
为什么我的值不更新,或者如何在触发_Change之前验证输入?
编辑
我现在已经将代码更改为
Private Sub txtStockCode_Change
If txtStockCode.Text <> "" Then
txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
End If
End Sub但是它仍然显示SBC/的前缀,但正在删除两个*字符(在条形码的开头和结尾,这是扫描器将其作为条形码读取所需的)。
发布于 2018-12-03 00:31:17
您可以尝试将条形码读取器设置为在扫描条形码的末尾返回Enter键,然后使用Keypress事件检查它并进行更改。
Sub txtStockCode_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
If Len(txtStockCode.Text) >= 5 Then
If bChangeCode Then
If Left(txtStockCode.Text, 2) = "C/" Then
msgbox "You have scanned the certificate barcode; please scan the stock barcode."
txtStockCode.Text = ""
Else
bChangeCode = False
txtStockCode.Text = Replace(txtStockCode.Text, "SBC/", "")
txtStockCode.Text = Replace(txtStockCode.Text, "*", "")
End If
End If
End If
End If
End Subhttps://stackoverflow.com/questions/53393564
复制相似问题