不允许MS Access的所有文本框中的特定字母

时间:2016-08-14 11:01:59

标签: vba textbox access-vba user-input

在表单上,​​我有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代表"

1 个答案:

答案 0 :(得分:3)

你试过Form级吗?顺便说一下,大多数时候用户都会复制和粘贴,这样就可以让用户输入那些不需要的字符(只是提醒:))

Private Sub Form_KeyPress(KeyAscii As Integer)
   If ActiveControl.ControlType = 109 Then 'only for textboxes
       KeyAscii = CharsNotAllowed(KeyAscii)
   End If
End Sub