我想过一个半透明的文字来显示日期应该如何输入的格式。添加代码后,我发现如果单击我的txtA_DateCR文本框,然后单击另一个文本框或命令按钮,错误将继续提示。
'This was the code that works fine before adding the following 2 below.
Private Sub txtA_DateCR_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Mid(txtA_DateCR.Value, 4, 2) > 12 Then
MsgBox "Invalid date, make sure format is (dd/mm/yyyy)", vbCritical
txtA_DateCR.Value = vbNullString
txtA_DateCR.SetFocus
Exit Sub
End If
dDate = DateSerial(Year(Date), Month(Date), Day(Date))
txtA_DateCR.Value = Format(txtA_DateCR.Value, "dd/mm/yyyy")
dDate = txtA_DateCR.Value
End Sub
'Supposed to change the textbox values to light grey and have the value of dd/mm/yyyy
Private Sub txtA_DateCR_AfterUpdate()
With txtA_DateCR
If .Text = "" Then
.ForeColor = &HC0C0C0
.Text = "dd/mm/yyyy"
End If
End With
End Sub
'When user clicks here, the value of dd/mm/yyyy will disappear and font colour will return back to black
Private Sub txtA_DateCR_Enter()
With txtA_DateCR
If .Text = "dd/mm/yyyy" Then
.ForeColor = &H80000008
.Text = ""
End If
End With
End Sub
Private Sub UserForm_Initialize()
txtA_DateCR.ForeColor = &HC0C0C0
txtA_DateCR.Text = "dd/mm/yyyy"
cmdEnter.SetFocus
End Sub
总结一下,如果用户输入错误的日期格式而不是在我点击文本框时弹出,我只希望弹出错误信息。