'限制只能输入数字
Public Sub CheckKeyPress(TargetTextBox As TextBox, KeyAscii As Integer, _
        Optional Negative As Boolean = True, Optional DecimalCount As Integer = 2)    KeyAscii = Asc(UCase$(Chr$(KeyAscii)))
    
    If KeyAscii = Asc("-") And TargetTextBox.SelStart = 0 And Negative Then
        TargetTextBox.Locked = False
    ElseIf KeyAscii = Asc(".") And InStr(TargetTextBox.Text, ".") = 0 And DecimalCount > 0 Then
        TargetTextBox.Locked = False
    ElseIf KeyAscii = 8 Then
        TargetTextBox.Locked = False
    ElseIf KeyAscii >= Asc("0") And KeyAscii <= Asc("9") And _
        ((InStr(TargetTextBox.Text, ".") > 0 And _
        (Len(TargetTextBox.Text) - InStr(TargetTextBox.Text, ".")) < DecimalCount) Or _
        InStr(TargetTextBox.Text, ".") = 0) Then
            TargetTextBox.Locked = False
    Else
        TargetTextBox.Locked = True
    End If
End Sub
''''''
Optional Negative As Boolean = True'是否允许负数
Optional DecimalCount As Integer = 2'小数位数
小于多少自己再加判断
可以在lostfocus中

解决方案 »

  1.   

    sub text1_change()
    aaa=你要限制的数的大小
    bbb = Right(Text3(Index).Text, 1)
    If InStr("1234567890.", bbb) = 0 or bbb>aaa Then
    MsgBox "只能输入数字!或数字超过限制" & aaa
    Text3(Index).Text = ""
    Exit Sub
    End If
    end sub
      

  2.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii > 57 Or KeyAscii < 48 Or CInt(Text1.Text) > 100 Then
            KeyAscii = 0
        End If
    End Sub
      

  3.   

    在KEYPRESS事件中    
    If (KeyAscii< vbKey0 Or KeyAscii> vbKey9) And KeyAscii<> 8 Then
            
            KeyAscii= 0
       
        End If
    在CHANGE事件中判断是否大于某一数值
      

  4.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        
        If KeyAscii < 48 Or KeyAscii > 57 Then  '如果輸入的不是數字﹐不允許輸入
            KeyAscii = 0
        Else                                    '如果是數字﹐檢查是否大于1000
            Dim strTmp As String
            
            strTmp = Text1.Text & Chr(KeyAscii)
            
            If Val(strTmp) > 1000 Then
                KeyAscii = 0
            End If
        End If
    End Sub
      

  5.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii <> 8 And KeyAscii < 48 Or KeyAscii > 57 Then
            Beep
    KeyAscii = 0
        End If
    End Sub
      

  6.   

    我的通用过程,在文本框的keypress事件调用
    '*********************************************************************************************
    '名称     Sub_CheckNum
    '作者   Morn Woo in 1998.10
    '功能     检查键入的是否是数字(不允许负数)判断输入字符是否是数字,不是则释放字符
    '参数     s(引用传递):待检查的字符串(如文本框中的文本);i(引用传递):字符的ASCII码.
    '*********************************************************************************************
    Sub Sub_CheckNum(ByRef i As Integer, Optional ByRef s = "")
    If Len(Trim(s)) Then
        Select Case i
           Case 48 To 57, 1 To 8, 11 To 13, 14 To 31, 46, 45
           Case Else
                Beep
                i = 0
        End Select
    Else
       Select Case i
          Case 8, 48 To 57, 45
          Case 44:  If s = "" Then i = 0
          Case 46
             If InStr(s, ".") > 0 Then Beep: i = 0
          Case Else: Beep: i = 0
       End Select
     End If
    End Sub
      

  7.   

    If Not IsNumeric(Text1(3).Text) Then
          MsgBox "折扣必须为数字!", 64, "错误"
          Text1(3).SetFocus
          checkform = False
          Exit Function
       End If
       If CDbl(Text1(3).Text) >= 100 Then
          MsgBox "折扣填写有误,不能大于100%!", 64, "错误"
          Text1(3).SetFocus
          checkform = False
          Exit Function
       End If
      

  8.   

    多谢各位的帮助!
    *gump2000(阿甘)的有点复杂,总觉得有杀鸡用牛刀的感觉。*cheerdesiger(随意)的答案中strTmp没有在If Val(strTmp) > 1000 Then中更新,造成一旦输入值超出而无法继续输入的局面。*hbm(江夏浪八)的答案运行时出错:“实时错误'13'/类型不匹配”;把CInt(...)改为Val(...)后不出错,但判断数值大小失败。*no_com(探花) 的答案有问题,输入任何数字都弹出msgbox
    -------------------------------------------------------
    我用的方法如下,请高手指点,有什么更简单的方法吗?
    public Temp As String
    MAXNUM=35Private Sub txtHis_Change(Index As Integer)
        If Len(txtHis(Index).Text) > 2 Then
            txtHis(Index).Text = Temp
        ElseIf Val(txtHis(Index).Text) > MAXNUM Then
            txtHis(Index).Text = ""
        Else
            Temp = txtHis(Index).Text
        End If
    End SubPrivate Sub txtHis_KeyPress(Index As Integer, KeyAscii As Integer)
        If KeyAscii < 48 Or KeyAscii > 75 Then
            KeyAscii = 0
        End If
    End Sub
      

  9.   

    修改一下我的程序把,我说一下思路:
     可以再设定一个参数,就是你要判断输入树脂的最大值,假设为x
     把s转换为树脂,然后x减s,假设得a
     在判断完按键是数字后,把按键转换为数字,假设为b
     判断b是否大于a,是则释放,并提示输入数据过大!
     虽然过程复杂点,但一次写完,以后可以多次调用,很有价值.
     我上面的那段带码,已经是多个mis应用程序中得一部分. 
      

  10.   

    用maskedit这个控件试试,比写代码更方便!!
      

  11.   

    使用ISNUMERIC 函数,调用 PRESS事件
      

  12.   

    下列代码是输入之1-100之间的值,只要进行变量名和范围限定值的改动即可使用
    Private Sub txtSpeed_Change()
        Dim lngBusSpeed As Long
        Dim strBusSpeed As String
        strBusSpeed = txtSpeed.Text
        On Error Resume Next
        lngBusSpeed = Abs(CLng(strBusSpeed))
        If Err <> 0 Or lngBusSpeed = 0 Then SendKeys "{BS}": Exit Sub
        If Len(Trim(Str(lngBusSpeed))) <> Len(strBusSpeed) Then SendKeys "{BS}": Exit Sub
        If lngBusSpeed > 100 Then SendKeys "{BS}"
        
    End Sub
      

  13.   

    aaa = 1000
    bbb = Right(Text1.Text, 1)
    If InStr("1234567890.", bbb) = 0 Then
    Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1)
    Text1.SetFocus
    Text1.SelStart = Len(Text1.Text)Exit SubEnd IfIf Val(Text1.Text) > aaa Then
      MsgBox "big"
      End If
      

  14.   

    private sub text1_keypress()
        '判断是否为数值
        if isnumber(text1.text) and val(text1.text)<100 then        ........
        else
              text1.text=""       endif
    end sub

    private sub text1_change()
        '判断是否为数值
        if isnumber(text1.text) and val(text1.text)<100 then        ........
        else
            text1.text=left(text1.text,len(text1.text)-1)    endif
    end sub
      

  15.   

    建议使用MaskEdit控件,限制输入数字和长度,然后进行有效性验证,很简单!
      

  16.   

    用isnumeric判断是不是数值
    再用一个if语句判断是否小于某一给定数值(这个给定数值也可以自己输入)
    if not isnumeric(text1.txt) then
        msgbox""
        exit sub
    end if
    if text1.txt>给定数值 then
       msgbox""
       exit sub
    end if
      

  17.   

    另外,可用text的maxlength属性限制输入的位数
    如果为0则基本是无限长(只要小于32K,我说的对吧)
      

  18.   

    HE HE 在这里看到好东东了
      

  19.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 Then
            KeyAscii = 0
        End If
    End Sub
      

  20.   


    Private Sub Text1_KeyPress(KeyAscii As Integer)
        KeyAscii = FilterForInt(KeyAscii)
    End Sub通用函数:
    '按键输入过滤函数
    Public Function FilterForInt(keyvalue) As Long
        FilterForInt = keyvalue
        If (((keyvalue > vbKey9) Or (keyvalue < vbKey0)) _
            And (keyvalue <> vbKeyBack) _
            And (keyvalue <> vbKeyReturn)) Then FilterForInt = 0        
    End Function只输入数字,判断大小自个来