isdate函数的问题,急等.
当我在文本框中输入为0004-24-24的时候,isdate判断此日期合法..只要年份前2位为00,日期合法性的判定就会出问题,请教怎么解决.
本人代码
Private Sub Form_Load()
'定义TEXT1为日期格式
Text1.Text = Format(Date, "yyyy-mm-dd")
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
'屏蔽DELETE键
If KeyCode = 46 Then
KeyCode = 0
End If
End Sub
Private Sub text1_lostfocus()
Dim mystr1, mystr2 As String
mystr1 = Text1.Text
mystr2 = Mid(mystr1, 6, 2)
'当年份的4个字符中含有空格时提示重新输如,焦点停留在年份
If Trim(Text1.Text) <> "" Then
If Mid(mystr1, 1, 1) = " " Or Mid(mystr1, 2, 1) = " " Or Mid(mystr1, 3, 1) = " " Or Mid(mystr1, 4, 1) = " " Then
MsgBox "请输入日期型数据!", 48, "提示"
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = 4
'当月份中有空格是提醒重新输入,焦点停留在月份
ElseIf Mid(mystr1, 6, 1) = " " Or Mid(mystr1, 7, 1) = " " Then
MsgBox "请输入日期型数据!", 48, "提示"
Text1.SetFocus
Text1.SelStart = 5
Text1.SelLength = 2
'当日子有空格时提醒重新输入,焦点停留在日子
ElseIf Mid(mystr1, 9, 1) = " " Or Mid(mystr1, 10, 1) = " " Then
MsgBox "请输入日期型数据!", 48, "提示"
Text1.SetFocus
Text1.SelStart = 8
Text1.SelLength = 2
'当输入在TEXT1中的日期不合法时,提醒重新输入
ElseIf Not IsDate(Text1.Text) Then
MsgBox "请输入日期型数据!", 48, "提示"
'如果月份大于12或者小于1,则焦点停留在月份
If Val(mystr2) < 1 Or Val(mystr2) > 12 Then
Text1.SetFocus
Text1.SelStart = 5
Text1.SelLength = 2
'如果月份不错,那么一定日子不合法,焦点停留在日子
Else
Text1.SetFocus
Text1.SelStart = 8
Text1.SelLength = 2
End If
End If
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim nSta As Integer
nSta = Text1.SelStart
'如果碰到backspace键,则用空格代替光标前的字符
If KeyAscii = 8 And Text1.SelStart > 0 Then
If nSta = 5 Or nSta = 8 Then nSta = nSta - 1
Text1.Text = Left(Text1.Text, nSta) & " " & Right(Text1.Text, Len(Text1.Text) - nSta)
Text1.SelStart = nSta
'如果输入字符是数字之外的字符则输入无效
ElseIf KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
'如果长度已经10位或者大于10,则输入无效
ElseIf nSta >= 10 Then
KeyAscii = 0
'如果输入是数字型字符,则按下面算法运算
Else
If nSta = 4 Or nSta = 7 Then nSta = nSta + 1
Text1.Text = Left(Text1.Text, nSta) & "" & Right(Text1.Text, Len(Text1.Text) - nSta - 1)
Text1.SelStart = nSta
End If
End Sub