在validate事件中
if val(text1)< 0 and val(text1)<> int(val(text1)) then
msgbox "不是合法的正整数"
cancel = true
End If

解决方案 »

  1.   

    更正:
    if val(text1)< 0 or val(text1)<> int(val(text1)) then
      

  2.   

    If Not IsNumeric(Text1.Text) Then
        MsgBox "请输入整数"
        Exit Sub
    End If
    If CLng(Text1.Text) < 0 Then
        MsgBox "请输入正整数"
        Exit Sub
    End If
      

  3.   

    还有一种方法,那就是避免用户输入小数和负数:
    private sub text1_keypress(byval keyascii as integer)
    select case keyascii
    case "0" to "9",8,13 '8是退格键,可以让用户删除;13是回车
    case else
    keyascii=0
    end select此法限制用户只能输入数字键。
      

  4.   

    不好意思,又要更正:
    case asc("0") to asc("9"),8,13 '8是退格键,可以让用户删除;13是回车
      

  5.   

    在 text1失去焦点事件里:
    if val(text1)< 0 and val(text1)<> int(val(text1)) then
    msgbox "不是合法的正整数"
    cancel = true
    End If
      

  6.   

    你可以通过ascii码来判断输入的是否合法!
      

  7.   

    定义一个常量
    Const ValidKeyInput As String = "0123456789abcdefABCDEF"
    上面的字符串可以任意改动为想允许输入的范围!然后在该控件的KeyPress事件里加入:
        If (KeyAscii > 31) Then
            If (InStr(ValidKeyInput, Chr$(KeyAscii)) = 0) Then
                MsgBox "输入无效!", vbExclamation, "错误"
                KeyAscii = 0
            End If
        End If