Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting date picker value to null 2

Status
Not open for further replies.

crysma

Programmer
Oct 17, 2002
21
CA
Hi,

I have a date picker, dtpickerBdate with its checked value set to true. When the date has been entered into the database, it shows the correct birthdate. However, when there is not value in the database, it shows a default date of 2000-01-01. I tried changing the value to null in the properties window, but it still shows this default. Can anyone tell me how to fix this, or point me towards other resources?

Thanks in advance

Sheryll
 
in the form's Load procedure, do something like this:

[tt]If Not IsNull(rs!BDate) Then
dtpickerBdate = CDate(rs!BDate)
Else
dtpickerBdate = Date
End If[/tt]

Using the Date function if no value is foudn will set the value of your dtp to today's date (or whatever the current system date is on your PC).

HTH,
jp
 
Thanks jpbrassard . But what if I don't want there to be any value at all? What I'd like is for the field to remain blank if there is no value entered in the database.
 
Short answer: the DTPicker won't let you do that. You can't give it a NULL value, and if you set it to 0 it gives you a date of 12/31/1899.

Easy solution: set the value to today's date, but disable the control if you don't have a value for BDate in the database.

dtpBDate.Enabled = False

Harder solution: use a combination of TextBox and MonthView controls to simulate how the DTPicker works, but gives you greater control. You'd have to figure out where to pop the MonthView control on our form, etc. - all the low-level stuff that the DTPicker control does for you. But it would let you do what you need to do.

HTH,
jp
 
thanks so much jpbrassard. I'll try that out, and let you know how it goes either way :)
 
Hi

I had a play with this and struck the same problem you had - perhaps someone else knows the "quick" solution to this?

So I had a think about other ways of doing this and came up with this ... note, it may require more "bullet proofing" for your needs, but its a start at least.

Add a maskedbox, a command button and a monthview to your project.

Then in your code,

Code:
Private Sub Form_Load()
    MonthView1.Visible = False
    MaskEdBox1.Format = "dd/mm/yyyy"
    MaskEdBox1.Mask = "##/##/####"
End Sub

Private Sub Command2_Click()
    MonthView1.Visible = True
    MonthView1.SetFocus
End Sub

Private Sub MaskEdBox1_Validate(Cancel As Boolean)
    If Not IsDate(MaskEdBox1.Text) And Val(MaskEdBox1.Text) <> 0 Then
        Cancel = True
    End If
End Sub

Private Sub MonthView1_DateDblClick(ByVal DateDblClicked As Date)
    MaskEdBox1.Text = Format(MonthView1.Value, MaskEdBox1.Format)
    MonthView1.Visible = False
    MaskEdBox1.SetFocus
End Sub

Private Sub MonthView1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        MaskEdBox1.Text = Format(MonthView1.Value, MaskEdBox1.Format)
        MonthView1.Visible = False
        MaskEdBox1.SetFocus
    End If
End Sub

Hope this helps ...
 
I thought of a solution to this last night. Its not particularly elegant, but I think it will work. Basically, I'm just going to set the foreground value of dtpickerBdate to white, same as the background, when there is no entry in the database. This should make it *look* empty. In its onclick event, I'll set the foreground color back to black if it is checked. Like I said, its not very elegant, but it will fool the users into thinking that the box is empty.
 
You know, that's not a bad idea. Obviously, you should make sure that, if the user doesn't enter a value in your DTPicker, that your database update code handles for this and passes a null value or empty string or whatever to your db. But this is a nice quick-and-dirty solution - and who cares about elegance, as long as it works consistently?

jp
 
gah! Stupid date picker won't allow me to change the color! >.< I'ts so frustrating.. I've got more important things to do than worry about this all day..
 
Hi crysma

I had a similar problem and found a datepicker component at this address:


I believe I was first told about the Common Controls Replacement Project site here in this forum.... regardless, I found the datepicker control to behave exactly as I wanted, INCLUDING the ability to have a blank date. Unfortunately, they're not updating their site any longer, but the component downloads are still available.

Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top