private sub text1_validate(cancel as boolean) if not isdate(text1) then msgbox "日期格式不对" cancel=true end if end sub
private sub text1_lostfocus() if trim(text1.text)<>"" then if not isdate(text1.text) then msgbox "请输入日期型数据!",48,"提示" text1.setfocus exit sub else text1.text=format(text1.text,"yyyy-mm-dd") end if end if end sub
Function CheckDate(Byval strDate as String) as Boolean Dim temp As String temp = Format(strDate, "YYYY-MM-DD") If IsDate(temp) Then CheckDate = True Else CheckDate = False End If End Function
private sub Text1_Validate(Cancel as Boolean) if trim(text1.text) = "" then exit sub if not isdate(Trim(text1.text)) then msgbox "錯誤的日期格式." cancel=true else text1.text = format(trim(text1.text),"yyyy-mm-dd") end if end sub
'限制输入日期函数 Public Function SetDate(obj As TextBox) As Boolean On Error Resume Next SetDate = False If Not IsDate(obj.Text) And obj.Text <> "" Then MsgBox "对不起,您输入的日期格式不正确!" & vbCrLf & "正确的日期格式为:年/月/日 时:分", vbExclamation SetDate = True End If End Function'需要限制控件的Validate事件里调用函数即可 Private Sub txtDaiLiRiQi2_Validate(Cancel As Boolean) On Error Resume Next Cancel = SetDate(txtDaiLiRiQi2) End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii <> 13 Then Exit Sub End If If Len(Trim(Text1.Text)) = 0 Then MsgBox "日期不能为空!", , "操作提示" Text1.SetFocus Exit Sub End If If Not IsDate(Text1.Text) Then MsgBox "请输入正确的日期格式!", , "操作提示" Text1.SelStart = 0 Text1.SelLength = Len(Text1.Text) Text1.SetFocus Exit Sub End If MsgBox Text1.Text
if not isdate(text1) then
msgbox "日期格式不对"
cancel=true
end if
end sub
if trim(text1.text)<>"" then
if not isdate(text1.text) then
msgbox "请输入日期型数据!",48,"提示"
text1.setfocus
exit sub
else
text1.text=format(text1.text,"yyyy-mm-dd")
end if
end if
end sub
必要的 expression 参数是一个 Variant,包含日期表达式或字符串表达式,这里的字符串表达式是可以作为日期或时间来认定的。如果表达式是一个日期,或可以作为有效日期识别,则 IsDate 返回 True;否则返回 False。在 Microsoft Windows 中,有效日期的范围介于公元 100 年 1 月 1 日与公元 9999 年 12 月 31 日之间;其有效范围随操作系统不同而不同。
eg:
Dim MyDate, YourDate, NoDate, MyCheck
MyDate = "February 12, 1969": YourDate = #2/12/69#: NoDate = "Hello"
MyCheck = IsDate(MyDate) ' 返回 True。
MyCheck = IsDate(YourDate) ' 返回 True。
MyCheck = IsDate(NoDate) ' 返回 False。
Dim temp As String
temp = Format(strDate, "YYYY-MM-DD")
If IsDate(temp) Then
CheckDate = True
Else
CheckDate = False
End If
End Function
if trim(text1.text) = "" then exit sub
if not isdate(Trim(text1.text)) then
msgbox "錯誤的日期格式."
cancel=true
else
text1.text = format(trim(text1.text),"yyyy-mm-dd")
end if
end sub
Public Function SetDate(obj As TextBox) As Boolean
On Error Resume Next
SetDate = False
If Not IsDate(obj.Text) And obj.Text <> "" Then
MsgBox "对不起,您输入的日期格式不正确!" & vbCrLf & "正确的日期格式为:年/月/日 时:分", vbExclamation
SetDate = True
End If
End Function'需要限制控件的Validate事件里调用函数即可
Private Sub txtDaiLiRiQi2_Validate(Cancel As Boolean)
On Error Resume Next
Cancel = SetDate(txtDaiLiRiQi2)
End Sub
If KeyAscii <> 13 Then
Exit Sub
End If
If Len(Trim(Text1.Text)) = 0 Then
MsgBox "日期不能为空!", , "操作提示"
Text1.SetFocus
Exit Sub
End If
If Not IsDate(Text1.Text) Then
MsgBox "请输入正确的日期格式!", , "操作提示"
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Text1.SetFocus
Exit Sub
End If
MsgBox Text1.Text
End Sub