在对一个表格第三列,和第四列控制,只能小数,和整数,语句如下,
Private Sub gd_KeyPress(KeyAscii As Integer)
If gd.Col = 2 Or gd.Col = 3 Then
If KeyAscii < 48 Or KeyAscii > 57 Then '48=is0,57 is9
MsgBox gd.Col & KeyAscii
KeyAscii = 0
End If
End If
结果不理想,如果先输入一个数字,在输入字母也能输入。如:9kdsjjd也能输入。怎么解决这个问题,
我这个语句还不能对小数进行判断,请高手帮改正一下。多谢。

解决方案 »

  1.   

    在对一个表格第三列,和第四列控制,只能小数,和整数,语句如下,
    Private Sub gd_KeyDown(KeyCode As Integer)
    If gd.Col = 2 Or gd.Col = 3 Then
      If KeyCode >= 48 and KeyAscii <= 57 Then '48=is0,57 is9
        'MsgBox gd.Col & KeyAscii
        'KeyAscii = 0
        else
        'MsgBox gd.Col & KeyAscii
        'KeyAscii = 0
            exit sub
      End If
    End If
    结果不理想,如果先输入一个数字,在输入字母也能输入。如:9kdsjjd也能输入。怎么解决这个问题,
    我这个语句还不能对小数进行判断,请高手帮改正一下。多谢。
      

  2.   

    set form's keypreview attirbutes true
      

  3.   

    我试过了,可行的
    Private Sub gd_KeyPress(KeyAscii As Integer)
           If KeyAscii = 8 Then Exit Sub'箭头键
              If KeyAscii < 48 Or KeyAscii > 57 Then
                 Beep
                 If KeyAscii <> 46 Then KeyAscii = 0 '小数点可以
                                
            End If      
                
     End Sub
      

  4.   

    我试的结果很正常,完全拦截了字母。允许小数:
    Private Sub gd_KeyPress(KeyAscii As Integer)
    If gd.Col = 2 Or gd.Col = 3 Then
    select case keyascii
    case asc("0") to asc("9"),asc("."),vbkeytab,vbkeyback
    case else
    MsgBox gd.Col & KeyAscii
    KeyAscii = 0
    end select
    End If
      

  5.   

    jennyvenus(JennyVenus) 写得对。
      

  6.   

    Private Sub gd_KeyPress(KeyAscii As Integer)
      If KeyAscii = 13 Then Exit Sub
      If InStr(".1234567890", Chr(CStr(KeyAscii))) = 0 Then
         KeyAscii = 0
      End If
    End Sub
      

  7.   

    Private Sub txtchinese_keypress(keyascii As Integer)
    If keyascii < Asc("0") Or keyascii > Asc("9") Then
        keyascii = 0  '取消输入
    End If
    End Sub
    只是整数