No date in DateTimePicker Control 
Author Message
 No date in DateTimePicker Control

I have a date/time field in a database field which is usually Null and
which I would like to display as blank in a DateTimePickerControl on
the form. How do I do that? Even if I uncheck the (optional) check box
in the control it displays the default today's date.


Mon, 18 Apr 2005 23:46:17 GMT  
 No date in DateTimePicker Control

Quote:
> I have a date/time field in a database field which is usually Null and
> which I would like to display as blank in a DateTimePickerControl on
> the form. How do I do that? Even if I uncheck the (optional) check box
> in the control it displays the default today's date.

Put the code at the end of this message in the Form's code window and
see if it does what you want. (I'm not sure if you can adapt this to a
data-bound DatePicker control or not.) If you want to blank the
DatePicker contorl within code, say using a CommandButton, put this in
its Click event.

Private Sub Command1_Click()
  DTPicker1.Value = vbNull
  FormatDTPicker
End Sub

Rick - MVP

Private Sub Form_Load()
  DTPicker1.Value = vbNull
  FormatDTPicker
End Sub

Private Sub DTPicker1_CloseUp()
  FormatDTPicker
End Sub

Private Sub DTPicker1_Format(ByVal CallbackField As String, _
                                             FormattedString As String)
  If CallbackField = "X" Then
    FormattedString = ""
  End If
End Sub

Private Sub FormatDTPicker()
  With DTPicker1
    If .Value = vbNull Then
      .Format = dtpCustom
      .CustomFormat = "X"
    Else
      .Format = dtpShortDate
    End If
  End With
End Sub

Private Sub DTPicker1_MouseDown(Button As Integer, Shift As Integer, X
As Single, Y As Single)
  With DTPicker1
    If .Value = vbNull Then
      .Value = Now
    End If
  End With
End Sub



Tue, 19 Apr 2005 02:45:50 GMT  
 No date in DateTimePicker Control
Thanks Rick. It's working well although it seems a lot of code for
what must be a fairly common problem.
Quote:



>> I have a date/time field in a database field which is usually Null and
>> which I would like to display as blank in a DateTimePickerControl on
>> the form. How do I do that? Even if I uncheck the (optional) check box
>> in the control it displays the default today's date.

>Put the code at the end of this message in the Form's code window and
>see if it does what you want. (I'm not sure if you can adapt this to a
>data-bound DatePicker control or not.) If you want to blank the
>DatePicker contorl within code, say using a CommandButton, put this in
>its Click event.

>Private Sub Command1_Click()
>  DTPicker1.Value = vbNull
>  FormatDTPicker
>End Sub

>Rick - MVP

>Private Sub Form_Load()
>  DTPicker1.Value = vbNull
>  FormatDTPicker
>End Sub

>Private Sub DTPicker1_CloseUp()
>  FormatDTPicker
>End Sub

>Private Sub DTPicker1_Format(ByVal CallbackField As String, _
>                                             FormattedString As String)
>  If CallbackField = "X" Then
>    FormattedString = ""
>  End If
>End Sub

>Private Sub FormatDTPicker()
>  With DTPicker1
>    If .Value = vbNull Then
>      .Format = dtpCustom
>      .CustomFormat = "X"
>    Else
>      .Format = dtpShortDate
>    End If
>  End With
>End Sub

>Private Sub DTPicker1_MouseDown(Button As Integer, Shift As Integer, X
>As Single, Y As Single)
>  With DTPicker1
>    If .Value = vbNull Then
>      .Value = Now
>    End If
>  End With
>End Sub



Tue, 19 Apr 2005 21:28:29 GMT  
 No date in DateTimePicker Control

Quote:
> Thanks Rick. It's working well although it seems a lot of code for
> what must be a fairly common problem.

The main problem is that Dates are not Strings; they must contain
something. There is no such thing as a "blank" date in the same way that
an Integer or Double can't be "blank". Dates are stored internally as
Doubles with the integer part representing the number of days away from
"date zero" and the decimal part representing the time as a fraction of
a 24-hour day. ("Date zero" is December 30, 1899 at midnight.) What VB
shows us for Dates is a human-readable convenience.

Rick - MVP

Quote:



> >> I have a date/time field in a database field which is usually Null
and
> >> which I would like to display as blank in a DateTimePickerControl
on
> >> the form. How do I do that? Even if I uncheck the (optional) check
box
> >> in the control it displays the default today's date.

> >Put the code at the end of this message in the Form's code window and
> >see if it does what you want. (I'm not sure if you can adapt this to
a
> >data-bound DatePicker control or not.) If you want to blank the
> >DatePicker contorl within code, say using a CommandButton, put this
in
> >its Click event.

> >Private Sub Command1_Click()
> >  DTPicker1.Value = vbNull
> >  FormatDTPicker
> >End Sub

> >Rick - MVP

> >Private Sub Form_Load()
> >  DTPicker1.Value = vbNull
> >  FormatDTPicker
> >End Sub

> >Private Sub DTPicker1_CloseUp()
> >  FormatDTPicker
> >End Sub

> >Private Sub DTPicker1_Format(ByVal CallbackField As String, _
> >                                             FormattedString As
String)
> >  If CallbackField = "X" Then
> >    FormattedString = ""
> >  End If
> >End Sub

> >Private Sub FormatDTPicker()
> >  With DTPicker1
> >    If .Value = vbNull Then
> >      .Format = dtpCustom
> >      .CustomFormat = "X"
> >    Else
> >      .Format = dtpShortDate
> >    End If
> >  End With
> >End Sub

> >Private Sub DTPicker1_MouseDown(Button As Integer, Shift As Integer,
X
> >As Single, Y As Single)
> >  With DTPicker1
> >    If .Value = vbNull Then
> >      .Value = Now
> >    End If
> >  End With
> >End Sub



Tue, 19 Apr 2005 23:46:06 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to handle Medium Date and General Date in a DateTimePicker control

2. Binding a datetimepicker control to a date time dataset field does not work

3. Displaying blank dates in DateTimePicker...

4. DateTimePicker with blank date

5. DateTimePicker and custom date format

6. How to make dates bold in DateTimePicker

7. formatting date on DateTimePicker

8. DateTimePicker and other controls from Windows Common Controls-2 6.0

9. Control Similar to Datetimepicker control

10. How to install DateTimePicker Control to another computer

11. Blank .text value for DateTimePicker control

12. How to simulate DateTimePicker control in ASP.NET?

 

 
Powered by phpBB® Forum Software