Private Sub Command1_Click() If IsNumeric(Text1.Text) Then MsgBox "你输入了数字" End If End Sub
Public Function CheckTxt(sTemplate As String) As Boolean On Error GoTo 0 CheckTxt = False
If Not ((KeyAscii >= Asc("0") And KeyAscii <= Asc("9"))) Then CheckTxt = True End If
Exit Function ----------------------------------- 上面的函数把小数点、正负号也排除在外了,如果你在只要求在事后进行一个判断,TXT中是否为数字,那么采用1楼的做法就OK了,如果在录的时候都不允许录入非数字的字符,按以下做法:'TXT只能输入数字,不带小数点 Private Sub MyEditTxt2_KeyPress(KeyAscii As Integer) On Error Resume Next On Error GoTo 0 Dim sTemplate As String
If Not ((KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) _ Or KeyAscii = 8) Then 'KeyAscii = 8ʱΪÍ˸ñ¼ü KeyAscii = 0 End If End Sub或'TXT只能输入数字,不带小数点 Private Sub MyEditTxt1_KeyPress(KeyAscii As Integer) On Error Resume Next Dim sTemplate As String
If Not ((KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) _ Or KeyAscii = 8 Or KeyAscii = Asc(".")) Then 'KeyAscii = 8ʱΪÍ˸ñ¼ü KeyAscii = 0 End If
'如果输入两个小数点则报错 If InStr(1, MyEditTxt1(Index).Text, ".") > 0 And KeyAscii = Asc(".") Then KeyAscii = 0 End IfEnd Sub
发上去才发现这么多乱码的?重发一遍:Private Sub MyEditTxt1_KeyPress(KeyAscii As Integer) On Error GoTo 0 Dim sTemplate As String
If Not ((KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) _ Or KeyAscii = 8 Or KeyAscii = Asc(".")) Then KeyAscii = 0 End If
If InStr(1, MyEditTxt1(Index).Text, ".") > 0 And KeyAscii = Asc(".") Then KeyAscii = 0 End IfEnd Sub ------------------------------------------------------------- Private Sub MyEditTxt2_KeyPress(KeyAscii As Integer) On Error GoTo 0 Dim sTemplate As String
If Not ((KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) _ Or KeyAscii = 8) Then KeyAscii = 0 End IfEnd Sub
If IsNumeric(Text1.Text) Then
MsgBox "你输入了数字"
End If
End Sub
On Error GoTo 0 CheckTxt = False
If Not ((KeyAscii >= Asc("0") And KeyAscii <= Asc("9"))) Then
CheckTxt = True
End If
Exit Function
-----------------------------------
上面的函数把小数点、正负号也排除在外了,如果你在只要求在事后进行一个判断,TXT中是否为数字,那么采用1楼的做法就OK了,如果在录的时候都不允许录入非数字的字符,按以下做法:'TXT只能输入数字,不带小数点
Private Sub MyEditTxt2_KeyPress(KeyAscii As Integer)
On Error Resume Next
On Error GoTo 0
Dim sTemplate As String
If Not ((KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) _
Or KeyAscii = 8) Then 'KeyAscii = 8ʱΪÍ˸ñ¼ü
KeyAscii = 0
End If
End Sub或'TXT只能输入数字,不带小数点
Private Sub MyEditTxt1_KeyPress(KeyAscii As Integer)
On Error Resume Next Dim sTemplate As String
If Not ((KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) _
Or KeyAscii = 8 Or KeyAscii = Asc(".")) Then 'KeyAscii = 8ʱΪÍ˸ñ¼ü
KeyAscii = 0
End If
'如果输入两个小数点则报错
If InStr(1, MyEditTxt1(Index).Text, ".") > 0 And KeyAscii = Asc(".") Then
KeyAscii = 0
End IfEnd Sub
On Error GoTo 0 Dim sTemplate As String
If Not ((KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) _
Or KeyAscii = 8 Or KeyAscii = Asc(".")) Then
KeyAscii = 0
End If
If InStr(1, MyEditTxt1(Index).Text, ".") > 0 And KeyAscii = Asc(".") Then
KeyAscii = 0
End IfEnd Sub
-------------------------------------------------------------
Private Sub MyEditTxt2_KeyPress(KeyAscii As Integer)
On Error GoTo 0 Dim sTemplate As String
If Not ((KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) _
Or KeyAscii = 8) Then
KeyAscii = 0
End IfEnd Sub
用IsNumeric函数就可以清松实现了,要多多看看VB自带的函数,这样可以方便你编程哟!