当我在文本框中输入为0004-24-24的时候,isdate判断此日期合法..只要年份前2位为00,日期合法性的判定就会出问题,请教怎么解决.
本人代码
Private Sub Form_Load()'定义TEXT1为日期格式
Text1.Text = Format(Date, "yyyy-mm-dd")
End SubPrivate Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
'屏蔽DELETE键
If KeyCode = 46 Then
KeyCode = 0
End If
End SubPrivate 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 SubPrivate 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

解决方案 »

  1.   

    过于复杂,建议用DTPICKER 或 MASKEDBOX
      

  2.   

    IsDate 函数
          返回 Boolean 值,指出一个表达式是否可以转换成日期。语法IsDate(expression)必要的 expression 参数是一个 Variant,包含日期表达式或字符串表达式,这里的字符串表达式是可以作为日期或时间来认定的。说明如果表达式是一个日期,或可以作为有效日期识别,则 IsDate 返回 True;否则返回 False。在 Microsoft Windows 中,有效日期的范围介于公元 100 年 1 月 1 日与公元 9999 年 12 月 31 日之间;其有效范围随操作系统不同而不同。
    MSDN中讲的很清楚,有效日期的范围介于公元 100 年 1 月 1 日与公元 9999 年 12 月 31 日之间,显然0002年不在此范围内,但因为并不一定最前面的就一定是年份,还有其他一些格式,比如ddmmyy或mmddyy等,在最前面的0002不满足年份的要求时,VB会自动检测它是否符合其他格式,如果符合,则会自动调整年月日的位置,比如0002-24-24会被VB解释成2024年2月24日,而如果试遍每一种格式都不符合有效日期的范围,才会返回false,比如isdate("0032-32-32"),不管年月日的位置怎么变换,它都不可能是一个有效日期,这时,isdate返回false
      

  3.   

    0004这个日期无效,建议用DTPICKER 或 MASKEDBOX