在text1的changed事件中加入:
if not  isnumeric(text1.text) then
    msgbox "请输入数字!"
    text1.setfocus
    exit sub
endif

解决方案 »

  1.   

    最好是用KEYASCII来判断,做一个通用函数即可用IsNumeric不是很严密,因为当有“e”时也会认为数字,系统以为是指数。
      

  2.   

    Private Sub aaa_KeyPress(KeyAscii As Integer)
        KeyAscii = OnlyInt(KeyAscii)
    End Sub'只能输入INT
    Public Function OnlyInt(KeyAscii As Integer) As String
        OnlyInt = ValiText(KeyAscii, "0123456789", True)
    End Function
      

  3.   

    '在FORM1中放个TEXT1,设置其text为""
    Private Sub Form_Load()
    Form1.KeyPreview = True
    End SubPrivate Sub Text1_Change()
    If IsNumeric(Text1) = False Then Text1 = "错误,只可以是数字"
    End Sub
    '但似乎还不如用KEYASCII来判断简单
      

  4.   

    private sub Text1_KeyPress(keyascii as integer)       if isnumeric(text1)=false then keyascii=0end sub
      

  5.   

    IsNumeric
    Returns a Boolean value indicating whether an expression can be evaluated as a number.Syntax
    IsNumeric(expression)The expression argument can be any expression.Res
    IsNumeric returns True if the entire expression is recognized as a number; otherwise, it returns False.IsNumeric returns False if expression is a date expression. 
      

  6.   

    private sub txt_keypress(keyascii as integer)if keyascii>9 or keyascii<0 then
    msgbox "请正确输入数字!"
    txt.setfocus
    txt.text=""
    end sub
      

  7.   

    keyascii很好用啊?但要记住添进Enter和Back space,要不然很麻烦的。
      

  8.   

    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
      

  9.   

    其实你一定要用的话可以使用MASK EDIT控件
    mask 为#######,数目为你要输入的最大字符数
    promptchar为空格
    不过我觉得不怎么好用
      

  10.   

    If KeyAscii <> vbKeyBack And KeyAscii <> vbKeyReturn And KeyAscii < vbKey1 Or KeyAscii > vbKey9 Then
    KeyAscii = 0
    End If
      

  11.   

    最有效的方法
    If Val(text1.text) & "" = text1.text Then
        MsgBox "是数字"
    else
       MsgBox "不是数字"
    End If