关键是text1文本框的输入过程,我在其他应用程序中见过很多,都是直接输入如:"19780512",显示"1978-05-12",不可能再按其他按钮的.
我的教材上只有Date()函数"返回系统当前日期",Int()函数"正数取整",不知前面加上"C"什么意思?
既然你要那种类\效果,那你就放在private sub text1_change()里咯.
我那代码只是个参考,具体怎么用你自己抓主意.
Repeat Life=Study;Until (death);
Const conDateInterval = "/"
Const conDateFormat = "yyyymmdd"
Const conDateFormat1 = "####/##/##"
Private Sub Form_Load()
Text1 = ""
Text2 = 0
Text1.MaxLength = 10
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Text2.SetFocus
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> vbKeyBack And KeyAscii <> 47 And KeyAscii <> 45 Or KeyAscii = vbKeyReturn Then KeyAscii = 0
End If
End Sub
Private Sub Text1_LostFocus()
If IsDate(ChangeDateFormat(Text1)) Then
Text1 = ChangeDateFormat(Text1)
Text2 = DateDiff("yyyy", Text1, Date)
Else
MsgBox "请输入正确的日期格式"
Text1.SetFocus
End If
End Sub
Private Function ChangeDateFormat(ByVal lsInputDate As String) As String
If Len(lsInputDate) >= 8 Then
If Len(lsInputDate) = 8 Then
lsInputDate = Left(lsInputDate, 4) & conDateInterval & Mid(lsInputDate, 5, 2) & conDateInterval & Right(lsInputDate, 2)
End If
If Len(lsInputDate) = 10 Then
If IsDate(lsInputDate) Then
If Format(lsInputDate, conDateFormat) <= Format(Date, conDateFormat) Then
ChangeDateFormat = Format(Format(lsInputDate, conDateFormat), conDateFormat1)
Else
ChangeDateFormat = lsInputDate
End If
Else
ChangeDateFormat = lsInputDate
End If
End If
Else
ChangeDateFormat = lsInputDate
End If
End Function