Private Sub Text3_KeyPress(KeyAscii As Integer)
    If KeyAscii < vbKey0 Or KeyAscii > vbKey9 Then  '不接受数字以外的字符
        MsgBox "输入字符无效"
        Text3 = Left(Text3, Len(Text3) - 1)
    End If
End Sub

解决方案 »

  1.   

    好像要转换成对应的ASCII码
    0-->48
    9__>57
      

  2.   

    If KeyAscii < vbKey0 Or KeyAscii > vbKey9 Then  '不接受数字以外的字符
            MsgBox "输入字符无效"
               KeyAscii = 0
        End If
      

  3.   

    不好意,再问一下,
    If KeyAscii < vbKey0 Or KeyAscii > vbKey9 Then  '不接受数字以外的字符
            MsgBox "输入字符无效"
               KeyAscii = 0End If好像吧删除键都屏蔽掉了,不方便修改内容,怎样改一下使删除键有效??
      

  4.   

    Public Function sffunLimitNumber(ByVal IntVal As Integer) As Integer
    '-------------------1-------------------
    '目    的:只允许在文本框内输入数字、退格、删除及回车键
    '输    入:ByVal IntVal As Integer,任意的键值
    '被传递值:无
    '返 回 值:过滤后的键值
    '输    出:无
    '注    解:
    '用    法:在文本框的KeyPress事件中输入KeyAscii = sffunLimitNumber(KeyAscii)即可
    '修 订 版:
    '-------------------1-------------------
    If (IntVal <> vbKeyDelete) _
    And (IntVal <> vbKeyBack) _
    And (IntVal <> 13) _
    And (IntVal < 48 Or IntVal > 57) Then
        IntVal = 0
    End If
    sffunLimitNumber = IntValEnd Function
      

  5.   

    怎样限制只输入汉字??这好像不好判断它的ascii值吧
      

  6.   

    应该是KeyAscii<asc(0)才对,我是这么干的。可是这么干的话就会屏蔽掉推格键。后来我是反过来干,不允许用户输入所有字母键,因为字母是连续的,很好做。
    汉字也一样,只允许ASCII的范围是0-255就是了!
      

  7.   

    Private Sub txtHigh_KeyPress(KeyAscii As Integer)                   '字段为数值的有效性验证
    On Error Resume Next'***********************************字段不为数值的有效性验证********************
        If Chr(KeyAscii) = vbBack Or KeyAscii = 46 Then Exit Sub
        If Not IsNumeric(Chr(KeyAscii)) Or Chr(KeyAscii) < 0 Then
            KeyAscii = 0
            MsgBox "数量必需是数值!请重新输入!!", vbOKOnly, "Stop!!"
        End If
        
    End Sub
      

  8.   

    退格和小数点:
    chr(keyascii)=vbback or keyascii=46