是否有办法让日期输入字段在月/日/年之间自动输入"/“?
我的当前表单有一些子类,以确保日期以正确的日期格式输入,并且它还检查条目是否写入mm/dd/yyyy,但是,我希望取消需要手动输入"/“的用户,并且在我的研究和测试中没有找到可行的解决方案。任何有帮助的解决方案都将不胜感激。
谢谢!
If Me.TourDateText.Value = "" Then
MsgBox "Please enter Tour Date.", vbExclamation, "frmEntry"
Me.FirstNameText.SetFocus
Exit Sub
End If
With Me.TourDateText
If Not IsDate(.Value) Then
.SetFocus
MsgBox "Enter a valid date"
Exit Sub
End If
End With
If Not IsDate(Me.TourDateText.Value) Then
MsgBox "The Tour Date field must contain only dates in the format mm/dd/yyyy.", vbExclamation, "frmEntry"
Me.TourDateText.SetFocus
Exit Sub
End If发布于 2014-09-17 06:35:53
然后您可以跟踪到UserForm
Private Sub TourDateText_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Else
KeyAscii = 0
End Select
If Len(Me.TourDateText.Text) = 2 Then
If Val(Me.TourDateText.Text) > 12 Then KeyAscii = 0 Else _
Me.TourDateText.Text = Me.TourDateText.Text & "/"
End If
If Len(Me.TourDateText.Text) = 5 Then
If Val(Mid(Me.TourDateText.Text, 4, 2)) > 31 Then KeyAscii = 0 Else _
Me.TourDateText.Text = Me.TourDateText.Text & "/"
End If
If Len(Me.TourDateText.Text) >= 10 Then KeyAscii = 0
End Sub第二版:
Private Sub TourDateText_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Debug.Print KeyAscii
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
If Len(Me.TourDateText.Text) = 2 Then
If Val(Me.TourDateText.Text) > 12 Then KeyAscii = 0 Else _
Me.TourDateText.Text = Me.TourDateText.Text & "/"
End If
If Len(Me.TourDateText.Text) = 5 Then
If Val(Mid(Me.TourDateText.Text, 4, 2)) > 31 Then KeyAscii = 0 Else _
Me.TourDateText.Text = Me.TourDateText.Text & "/"
End If
If Len(Me.TourDateText.Text) >= 10 Then KeyAscii = 0
End Sub现在用户可以输入09172014,文本为09/17/2014。或者输入为09any key17any key2014,文本为09/17/2014。
月> 12,天> 31也是可以预防的。这不是最终的数据验证,因为它不能检查月是否有31天。但是最终的数据验证已经有了。
问候
Axel
https://stackoverflow.com/questions/25876226
复制相似问题