I am going to solve two problems with a DatePicker object and a workaround for them.
- You cannot put an empty value in a DatePicker, which will lead me to the second problem.
- You cannot change the font color to at least make it blank.
To preserve the functionality of DatePicker and to be able to have an empty value and your usual font formatting options (colors, etc.), I used two objects. First create a DTP object and set the width so that you can only see the down arrow. For me it was 15. Then make a regular TextBox that is big enough to hold your date. Place the DTP arrow right to the right (or left) of the text box. Then you simply add the code to the DTP change event to copy it .Value to .Text TextBox, for example:
Private Sub MyDTP_Change() MyUserForm.MyDateTextBox.Text = MyUserForm.MyDTP.Value End Sub
Then you have links to the data you need to access MyDateTextBox.Text, not MyDTP.Value and presto! You get DTP functionality with a regular TextBox formatting control.
EDIT: Sorry JeffK, I did not work with VB in the production environment 9 years ago. :) I would like to add the other side of functionality to this. This allows two-way synchronization between TextBox and DTP. IE: Manually enter the date in the TextBox and then select DTP Calendar. If the TextBox is empty or has an invalid date, DTP defaults to today's date.
Private Sub MyDateTextBox_Change() If MyUserForm.MyDateTextBox.Text <> "" And IsDate(MyUserForm.MyDateTextBox.Text) = True Then If CDate(MyUserForm.MyDateTextBox.Text) <= MyUserForm.MyDPT.MaxDate And _ CDate(MyUserForm.MyDateTextBox.Text) >= MyUserForm.MyDPT.MinDate Then MyUserForm.MyDTP.Value = MyUserForm.MyDateTextBox.Text Else MyUserForm.MyDTP.Value = Date End If Else MyUserForm.MyDTP.Value = Date End If End Sub
source share