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

Don't allow specific letters in ALL textbox for MS Access

在表格上,我有 13 textbox。当用户单击保存按钮时,Access 调用 API 并将这些文本框中的值传递给 API(通过 HTTP REST)

有些字符是不允许的,例如"不允许通过API保存。因此我想限制用户输入这些字符。

目前我已经创建了一个 Public FunctionKeyPress 事件调用来检查字符是否被允许。但是我必须从每个 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