'数值型判断:isnumeric(text1.text) '日期型判断:isdate(text1.text)private sub text1_lostfocus() if text1.text<>"" then if not isnumeric(text1.text) then msgbox "请输入数值型数据!",48,"提示" text1.setfocus exit sub end if end if end sub
Private Sub Text1_Change() If IsNumeric(Text1.Text) = False Then MsgBox "请输入数字" End If End Sub
Private Sub Text1_Validate(Cancel As Boolean) if text1.text<>"" then if not isnumeric(text1.text) then cancel=true end if end if End Sub
一 Private Sub Text1_KeyPress(KeyAscii As Integer) If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0 End Sub 二 Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 48 To 57 Case 8 Case Else Beep MsgBox "error!" KeyAscii = 0 End Select三 Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Const ES_NUMBER = &H2000 Private Const GWL_STYLE = -16Private Sub ForceNumeric(Box As TextBox) On Error Goto Catch Dim nStyle As Long nStyle = GetWindowLong(Box.hWnd, GWL_STYLE) Call SetWindowLong(Box.hWnd, GWL_STYLE, nStyle Or ES_NUMBER) Goto Finally Catch: Call MsgBox(Err.Description, vbCritical Or vbOKOnly, "Error") Finally: End Sub
'日期型判断:isdate(text1.text)private sub text1_lostfocus()
if text1.text<>"" then
if not isnumeric(text1.text) then
msgbox "请输入数值型数据!",48,"提示"
text1.setfocus
exit sub
end if
end if
end sub
If IsNumeric(Text1.Text) = False Then
MsgBox "请输入数字"
End If
End Sub
if text1.text<>"" then
if not isnumeric(text1.text) then
cancel=true
end if
end if
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
End Sub
二
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57
Case 8
Case Else
Beep
MsgBox "error!"
KeyAscii = 0
End Select三
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const ES_NUMBER = &H2000
Private Const GWL_STYLE = -16Private Sub ForceNumeric(Box As TextBox)
On Error Goto Catch
Dim nStyle As Long
nStyle = GetWindowLong(Box.hWnd, GWL_STYLE)
Call SetWindowLong(Box.hWnd, GWL_STYLE, nStyle Or ES_NUMBER)
Goto Finally
Catch:
Call MsgBox(Err.Description, vbCritical Or vbOKOnly, "Error")
Finally:
End Sub