Date/Time Pickers/VB2005

D

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
 
C

Chris Dunaway

Darhl said:
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.
 
H

Herfried K. Wagner [MVP]

Darhl Thomason said:
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.
 
D

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top