Private Sub txt_KeyPress(KeyAscii As Integer)
'只允许输入数字
    If (KeyAscii <> vbKeyDelete) And (KeyAscii <> vbKeyBack) And (KeyAscii <> 13) _
    And (KeyAscii < 48 Or KeyAscii > 57) Then
            KeyAscii = 0
    End If
End Sub

解决方案 »

  1.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
    End Sub
    功能
    只允许输入数字
      

  2.   

    Public Function ValiText(KeyIn As Integer, ValidateString As String, Editable As Boolean) As Integer '密码设置
    Dim ValidateList As String
    Dim KeyOut As Integer
    If Editable = True Then
    ValidateList = UCase(ValidateString) & Chr(8)
    Else
    ValidateList = UCase(ValidateString)
    End If
    If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) > 0 Then
    KeyOut = KeyIn
    Else
    KeyOut = 0
    Beep
    End If
    ValiText = KeyOut
    End Function
    然后再text的keypress事件中
    KeyAscii = ValiText(KeyAscii, "0123456789.", True)
    其中上面的式子中" "可以填入你限制的字符。等
    此函数通用,如光是数字还是上面方便。
      

  3.   

    上面的都可以。在KeyPress事件中判断
    你可以用Select 语句加断点判断,很快就学会了