private sub text1_lostfocus()
if trim(text1.text)<>"" then
if not isnumeric(text1.text) then
msgbox "请输入数值型数据!",48,"提示"
text1.setfocus
end if
end if
end sub
在keypress事件的时候限定KeyAscii的值在asc("0")~asc("9")之间
同时注意[TAB]和[Backspace]最好也允许输入,还有"."
例如
Private Sub txtTotal_KeyPress(KeyAscii As Integer)
KeyAscii = ValidateData_Number(KeyAscii)
End Sub
这是我写的函数,平时用的,作为参考
Public Function ValidateData_Number(ByVal vData As Integer) As Integer
'{0-9,'+','-','.'} Supported
'BACKSPACE,DELETE Supported
'Return: KeyAscii As Integer
' 0:Invalidate Key Press
If (vData > 47) And (vData < 58) Then
ValidateData_Number = vData
ElseIf (vData = 46) Or (vData = 45) Or (vData = 43) Or (vData = 8) Or (vData = 13) Then
ValidateData_Number = vData
Else
ValidateData_Number = 0
End If