Home All Groups Group Topic Archive Search About

Date/Time Pickers/VB2005

Author
7 Sep 2006 4:57 PM
Darhl Thomason
I'm building a database application that has a number of date fields in it.
When I add the date/time picker control to my form and run it, if the field
is null in my db, then it shows the current date.  How can I get this
control to be blank when the corresponding field in my db is blank?

Thanks!

Darhl

Author
7 Sep 2006 9:03 PM
Chris Dunaway
Darhl Thomason wrote:
> I'm building a database application that has a number of date fields in it.
> When I add the date/time picker control to my form and run it, if the field
> is null in my db, then it shows the current date.  How can I get this
> control to be blank when the corresponding field in my db is blank?

There is no built in support in the control for null dates.  Here's
what I did:

First I create two string to hold the date format.  Note that the Not
Specified string includes an apostrophe (') at each end.  'Not
Specified'

Dim _userCustomFormat As String = "MM/dd/yyyy"
Dim _nullFormat As String = "'Not Specified'"

On the DateTimePicker control, set the format to Custom

Then in the code, check to see if the date is null (Nothing) and if so,
us the _nullFormat, otherwise use your desired date format:

If _dateVariable.HasValue Then
    dtpDate.Value = _currentEmployee.AgreementDate.Value
    dtpDate.CustomFormat = _userCustomFormat
Else
    dtpDate.Value = DateTime.MinValue
    dtpDate.CustomFormat = _nullFormat
End If

Hope this gives you some ideas.
Author
8 Sep 2006 3:09 PM
Darhl Thomason
Thanks Chris,

I know you're just making names up because you don't know my db, but I don't
know how to pull the value of my date field.  You're using "_dateVariable"
and "_currentEmployee.AgreementDate" and I don't know where they come
from...I'm assuming these are variables that get assigned somewhere.  I get
the concept of this, but am struggling with how/where to assign my values.



Can I put this into a loop?  I've got 6 dtp controls...so could I do
something like:
For Each dtp in frmStoreData
  if/then...
  else...
  endif...
Next

Darhl


Show quoteHide quote
"Chris Dunaway" <dunaw***@gmail.com> wrote in message
news:1157663037.981340.80300@i3g2000cwc.googlegroups.com...
> Darhl Thomason wrote:
>> I'm building a database application that has a number of date fields in
>> it.
>> When I add the date/time picker control to my form and run it, if the
>> field
>> is null in my db, then it shows the current date.  How can I get this
>> control to be blank when the corresponding field in my db is blank?
>
> There is no built in support in the control for null dates.  Here's
> what I did:
>
> First I create two string to hold the date format.  Note that the Not
> Specified string includes an apostrophe (') at each end.  'Not
> Specified'
>
> Dim _userCustomFormat As String = "MM/dd/yyyy"
> Dim _nullFormat As String = "'Not Specified'"
>
> On the DateTimePicker control, set the format to Custom
>
> Then in the code, check to see if the date is null (Nothing) and if so,
> us the _nullFormat, otherwise use your desired date format:
>
> If _dateVariable.HasValue Then
>    dtpDate.Value = _currentEmployee.AgreementDate.Value
>    dtpDate.CustomFormat = _userCustomFormat
> Else
>    dtpDate.Value = DateTime.MinValue
>    dtpDate.CustomFormat = _nullFormat
> End If
>
> Hope this gives you some ideas.
>
Author
7 Sep 2006 10:14 PM
Herfried K. Wagner [MVP]
"Darhl Thomason" <darhlt@papamurphys.nospamplease.com> schrieb:
> I'm building a database application that has a number of date fields in
> it. When I add the date/time picker control to my form and run it, if the
> field is null in my db, then it shows the current date.  How can I get
> this control to be blank when the corresponding field in my db is blank?

Untested:

<URL:http://www.grazioli.ch/Blog/PermaLink.aspx?guid=c4a63d35-71f9-4b02-9de7-0c87e5b1c770>

Nullable DateTimePicker
<URL:http://www.codeproject.com/cs/miscctrl/Nullable_DateTimePicker.asp>

(For a translation to VB.NET, take a look at the comments section of the
page.)

You may want to set the 'ShowCheckBox' property to 'True' and use the
checkbox as a null value indicator (if 'Checked' is 'False', no date is
selected).  Notice that the 'ShowCheckBox' property is flawed and thus this
is not the best way.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>