MS Access:仅允许数字和退格

时间:2016-08-11 10:18:02

标签: vba ms-access

如何才允许用户仅在字段中键入数字和退格?

Private Sub Field_KeyPress(KeyAscii As Integer)

    If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0

End Sub

有了这个,用户也无法使用退格...

3 个答案:

答案 0 :(得分:1)

Del TAB 箭头等等怎么样?

我更倾向于使用"仅限数字"的验证规则。

http://allenbrowne.com/ValidationRule.html

答案 1 :(得分:0)

使用此:

If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> 8 Then KeyAscii = 0

答案 2 :(得分:0)

以下是两种方式

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    Select Case KeyAscii
        Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57

        Case Else
        KeyAscii = 0
    End Select

End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0

End Sub