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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datepicker field 1

Status
Not open for further replies.

Saturn57

Programmer
Aug 30, 2007
275
CA
I have a form connected to an sql database that has datepicker fields attached to a table in the database with dates. When I open the form I would like it to show the value in the database or be blank if that field is null in the table. Right now it is putting in the current date for fields that are null in the table.
 

And your code right now looks like.......

Have fun.

---- Andy
 
'HIREDATEDateTimePicker
'
Me.HIREDATEDateTimePicker.DataBindings.Add(New System.Windows.Forms.Binding("Value", Me.EmployeeTrainingBindingSource, "HIREDATE", True))
Me.HIREDATEDateTimePicker.Location = New System.Drawing.Point(147, 85)
Me.HIREDATEDateTimePicker.Name = "HIREDATEDateTimePicker"
Me.HIREDATEDateTimePicker.Size = New System.Drawing.Size(200, 20)
Me.HIREDATEDateTimePicker.TabIndex = 6
'
 
I wrestled with this for ages and even seasoned pro's on this site didn't have a solution so I gave up.

In the end I settled for a DateTimePicker and a Textbox.

The textbox sits ontop of the DTP (Slightly less wide than the DTP so user can see the dropdown button)

I bound the database field to the text box and this is the behaviour

1. Record Loads - If textbox loads as NULL then show the textbox, if it isn't NULL then set the datetimepicker value to the same as the textbox and hide the textbox.

(This give the user the full functionality of the DTP).

2. If user wants to change the date they must click on the DTP (Not type in the textbox) and when the DTP value changes, it is set to automatically change the textbox to the value of the DTP.

This works fine for me but if you have any problems with this just post back.

Hopefully someone has found a better way of doing this though.

John
 
Would you have code for this as I'm very green.
 
My DateTimePicker is named 'DateOutDTP'
My Textbox is named 'DateOutFake'

here is each procedure that works with these objects..
Code:
Private Sub LoadJob()
'If the job loads and the DateOut is NOT NULL (i.e a date has appeared in the textbox) then set the DTP to the same value as the textbox and make the textbox invisible
If DateOutFake.Text <> "" Then
DateOutDTP.Value = DateOutFake.Text
DateOutFake.Visible = False
End If
End Sub
Code:
Private Sub DateOutDTP_KeyUp
'User needs the option to completely remove the date so this bit will do that when they press the delete key
If e.KeyCode = Keys.Delete Then
'show the fakebox and set it as a blank string
DateOutFake.Visible = True
DateOutFake.Text = ""
End If
Code:
Private Sub DateOutDTP_ValueChanged
'If someone uses the calendar to change the date value then put this date in the textbox and hide the textbox
'Set the DateOutFake tetxbox (fake because the real one wouldn't display a blank date value)
DateOutFake.Text = Format(DateOutDTP.Value, "dd/MM/yyyy HH:mm")
'Hide the fakebox so that user can retain functunality of the real datetimepicker
DateOutFake.Visible = False
That pretty much covers it. One other thing you should know is that you need to slightly change your bindingsource so that if the user has deleted the date from the textbox then the database accepts it as NULL.

My DataBase field is called 'DateOut'
Code:
Me.DateOutFake.DataBindings.Add("Text", My BindingSource, "DateOut", True, System.Windows.Forms.DataSourceUpdateMode.OnValidation, "")

Hope that helps

John

 
Thanks for the help John. I got the null value portion to work but the change value is not. Any ideas. Here is my code:
Private Sub HireDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles HireDateTimePicker.ValueChanged
'If someone uses the calendar to change the date value then put this date in the textbox and hide the textbox
'Set the DateOutFake tetxbox (fake because the real one wouldn't display a blank date value)
If FakeHireDateTB.Text = Format(HireDateTimePicker.Value, "dd/MM/yyyy HH:mm") Then
'Hide the fakebox so that user can retain functunality of the real datetimepicker
FakeHireDateTB.Visible = False
End If
 
John I figured it out I had to press save on the navigator bar but now Im trying the delete routine and when I highlight the date and press delete it deletes the data but the cursor freezes in that data box. Any help would be appreciated.
Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top