Private Sub Text1_LostFocus() If Text1.Text ...... Then '判断Text1.Text是否是合法,如果不合法 Text1.Text = "" Text1.SetFocus End If End Sub
希望我得这段代码对你有帮助.由于时间紧日和月的判断上不完善,如果需要我可以给你写全.Option Explicit Dim IsTrue As Boolean Function Is_True(Str As String, FText As TextBox) If IsTrue = False Then IsTrue = True Exit Function End If Select Case Str Case "Year" If Len(FText.Text) <> 4 And IsNumeric(FText.Text) = False Then MsgBox ("输入的日期无效") FText.SetFocus FText.Text = "" IsTrue = False End If Case "Month", "Day" If Len(FText.Text) <> 2 And IsNumeric(FText.Text) = False Then MsgBox ("输入的日期无效") ' FText.SetFocus FText.Text = "" IsTrue = False End If End Select End FunctionPrivate Sub Form_Load() IsTrue = True End SubPrivate Sub Text2_LostFocus() Is_True "Year", Text2 End SubPrivate Sub Text3_LostFocus() Is_True "Year", Text3 End SubPrivate Sub Text1_LostFocus() Is_True "Year", Text1 End Sub
不好意思3个Text的LostFocus的事件弄错了. Private Sub Text2_LostFocus() Is_True "Month", Text2 End SubPrivate Sub Text3_LostFocus() Is_True "Day", Text3 End SubPrivate Sub Text1_LostFocus() Is_True "Year", Text1 End Sub
SETFOCUS方法
在这里判断啊
If IsNumeric(Text1.Text) = False Then '判断TextBox.Text是否是数字
Text1.Text = ""
Text1.SetFocus
End If
End Sub
If IsNumeric(Text1.Text) = False Then '判断Text1.Text是否是数字
Text1.Text = ""
Text1.SetFocus
End If
End Sub
If Text1.Text ...... Then '判断Text1.Text是否是合法,如果不合法
Text1.Text = ""
Text1.SetFocus
End If
End Sub
Dim IsTrue As Boolean
Function Is_True(Str As String, FText As TextBox)
If IsTrue = False Then
IsTrue = True
Exit Function
End If
Select Case Str
Case "Year"
If Len(FText.Text) <> 4 And IsNumeric(FText.Text) = False Then
MsgBox ("输入的日期无效")
FText.SetFocus
FText.Text = ""
IsTrue = False
End If
Case "Month", "Day"
If Len(FText.Text) <> 2 And IsNumeric(FText.Text) = False Then
MsgBox ("输入的日期无效") '
FText.SetFocus
FText.Text = ""
IsTrue = False
End If
End Select
End FunctionPrivate Sub Form_Load()
IsTrue = True
End SubPrivate Sub Text2_LostFocus()
Is_True "Year", Text2
End SubPrivate Sub Text3_LostFocus()
Is_True "Year", Text3
End SubPrivate Sub Text1_LostFocus()
Is_True "Year", Text1
End Sub
Private Sub Text2_LostFocus()
Is_True "Month", Text2
End SubPrivate Sub Text3_LostFocus()
Is_True "Day", Text3
End SubPrivate Sub Text1_LostFocus()
Is_True "Year", Text1
End Sub