在一个表单上,我有13个textbox
。当用户单击保存按钮时,Access调用API并将这些文本框中的值传递给API(通过HTTP REST)
有一些字符是不允许的,如"
,不允许通过API保存。因此,我想限制用户输入这些字符。
目前,我已经创建了一个Public
Function
,从KeyPress
事件调用,以检查是否允许字符。但是我必须从每个textbox
KeyPress
事件中调用这个函数。有没有办法不允许"
跨越所有的文本框??
Public Function CharsNotAllowed(myChar As Integer) As Integer
If myChar = 34 Then
Beep 'Let the user know they hit an illegal key
CharsNotAllowed = 0 'Don't let the keystroke through
Else
CharsNotAllowed = myChar
End If
End Function
函数调用
Private Sub StudentName_KeyPress(KeyAscii As Integer)
KeyAscii = CharsNotAllowed(KeyAscii)
End Sub
Private Sub StudentClass_KeyPress(KeyAscii As Integer)
KeyAscii = CharsNotAllowed(KeyAscii)
End Sub
'and so on for all the 13 text boxes
注释: 34
代表"
您尝试过Form
级别吗?顺便说一句,大多数时候用户复制粘贴,这将允许用户输入那些不需要的字符(只是一个提醒:))
Private Sub Form_KeyPress(KeyAscii As Integer)
If ActiveControl.ControlType = 109 Then 'only for textboxes
KeyAscii = CharsNotAllowed(KeyAscii)
End If
End Sub