Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) Then
   KeyAscii = 0
End If
End Sub

解决方案 »

  1.   

    to youcheng1:那样做按“退格”就不好使了!
      

  2.   

    Private Sub txtZYHM_KeyPress(KeyAscii As Integer)
        If KeyAscii = vbKeyReturn Then
            If Del_Space(txtZYHM.Text) = "" Then
                MsgBox "对不起,住院号码不能够为空!", vbOKOnly, Me.Caption
                Exit Sub
            End If
               ElseIf (KeyAscii < 48 Or KeyAscii > 57) And (KeyAscii < 8 Or KeyAscii > 13) Then
            KeyAscii = 0
        End If
       
    End Sub
      

  3.   

    用isNumeric函数判断是否为数值更简单,上面两位好像都没考虑到小数问题,用这个函数就一劳永逸了
      

  4.   

    Private Sub Text1_LostFocus()
        If Not IsNumeric(Text1.Text) Then
            MsgBox "请输入数字", vbOKOnly + vbInformation, "输入错误"
            Text1.SetFocus
            Text1.SelStart = 0
            Text1.SelLength = Len(Text1.Text)
        End If
        
    End Sub
      

  5.   

    this is best
    回复人: chewinggum(口香糖) ( ) 信誉:100  2002-12-17 16:27:00  得分:0 
     
     
      用isNumeric函数判断是否为数值更简单,上面两位好像都没考虑到小数问题,用这个函数就一劳永逸了
      
     
      

  6.   

    Public Function Dec_Only(ByVal X As Integer) As Integer
    Select Case X
    Case 8, Asc("."), Asc("0") To Asc("9")
    Dec_Only = X
    Case 13
    Dec_Only = 9
    Case Asc("。")
    Dec_Only = Asc(".")
    Case Else
    Dec_Only = 0
    End Select
    End Function引用例子:
    Private Sub text1_KeyPress(KeyAscii As Integer)
    KeyAscii = Dec_Only(KeyAscii)
    End Sub