vb调用控制输入数字和字母

防止SQL注入:

在模块写入
Public Function IsNumber(KeyAscii As Integer) As Integer
    Select Case KeyAscii
        Case 48 To 57    '只能输入数字
        Case 65 To 90    '只能输入大写字母
        Case 97 To 122   '只能输入小写字母
        Case 8           '只能输入退格
        Case Else        '否则输入无效
        KeyAscii = 0
    End Select
End Function


在对应文本框写入
Private Sub Text1_KeyPress(KeyAscii As Integer)
     Call IsNumber(KeyAscii)
    If Len(Text1) >= 3 Then    ' 3的意思是只能输入三个字符。
          Text1.Text = "" '输入框内容清零
             KeyAscii = 0    '输入字符清零
           MsgBox "输入数值位数过长", vbOKCancel, "提示"
        End If
End Sub

猜你喜欢

转载自blog.csdn.net/Ciel_Y/article/details/82924314