DateTimePicker in vb.net

I am using visual studio.net2003 and Access Database
I am developing a small program of “Employees check in and check out”.
It records the “check in” and “check out” times, and calculates the hours worked when an employee checks out.
I am lost in how to enter the time of checkout at a later date, in case an employee forgets to check out.  
I have a “DatetimePicker” control with format set to time.

Q1- Is the DateTimePicker the best control for what I am planning to do
Q2- If yes then how do I capture only time
Q3- After capturing time how do I send the data(Time) back to database so the math of calculating hours is right.  I need to know the format.  I know how to update field or fields of a record.
I have attached the snapshot of my form

Private Sub frmModHours_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgrHours.Hide() 'datagrid  to show the employee record for the selected date
        gbName.Hide()
        cal.Hide()
        gbRad.Hide()
        lblCal.Hide()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()

    End Sub
    Private Sub txtEmpID_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEmpID.TextChanged

        If txtEmpID.TextLength > 4 Then
            cal.Hide()
            lblCal.Hide()
            MessageBox.Show("Please enter 4 digit empID")
            txtEmpID.Text = ""
            txtEmpID.Focus()
            Exit Sub
        ElseIf txtEmpID.TextLength = 4 Then
            cal.Show()
            lblCal.Show()
            DsEmpBrief1.Clear()
            Dim meSearch As String
            meSearch = "Select * from tblEmployeeBrief where empID=" & txtEmpID.Text
            daEmpBrief.SelectCommand.CommandText = meSearch

            Try
                daEmpBrief.Fill(DsEmpBrief1)

            Catch ex As Exception
                MessageBox.Show(ex.Message, ex.GetType.ToString)
            End Try

            gbName.Show()

        End If

    End Sub

    Private Sub cal_DateSelected(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles cal.DateSelected
        Dim result, mysearch As String
        Dim myDate As Date
        myDate = cal.SelectionStart.Date
        txtDate.Text = myDate 'txtDate is a hidden textbox behind the calender
        result = MessageBox.Show("Desired date of time modification is  " & myDate & ",  If correct then click yes", " D A T E C O R R E C T ?", MessageBoxButtons.YesNo)
        If result = DialogResult.Yes Then
            dgrHours.Show()
            mysearch = "Select * from tblhours where workdate=#" & myDate & "# and empID='" & txtEmpID.Text & "'"
            daHours.SelectCommand.CommandText = mysearch
            daHours.Fill(DsHours1)
            gbRad.Show()
            cal.Hide()
            lblCal.Hide()
            gbName.Show()

        Else : MessageBox.Show("Please select desired date")
            Exit Sub
        End If
    End Sub
    Private Sub radOUT_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radOUT.CheckedChanged
        Dim dtetime As Date = Convert.ToDateTime(txtHours.Text)

        MessageBox.Show(dtetime)


    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        Dim metime As Date
        Dim meDate As Date
        
       





    End Sub

End Class

Open in new window

ModTime-form.bmp
vbstudent9Asked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
0
 
käµfm³d 👽Commented:
Is the DateTimePicker the best control for what I am planning to do
I think it should be fine.

If yes then how do I capture only time
Me.DateTimePicker1.Value.TimeOfDay

Open in new window


After capturing time how do I send the data(Time) back to database so the math of calculating hours is right.  I need to know the format.  I know how to update field or fields of a record.
What type did you set the database field to?
0
 
vbstudent9Author Commented:
I am not trying to capture time of day
Date time picker needed to be used to select the time of check out by an emplyee from previous day, may be few days
Example:
suppose on may 1 an employee checked in successfully but forget to check out Now he comes back to work on may 5th
Tries to check in.  since the Programm is designed to alert the employee that he didnt check out on may 1st. His supervisor has to fix it.
so supervisor accesses this form on may 5th
and then selects the time of checkout for may 1st and need to save
Now my question is the the dateTimepicker is the right control for this scenerio
access data base field is set for date/time format medium time
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
käµfm³d 👽Commented:
I am not trying to capture time of day
Date time picker needed to be used to select the time of check out by an emplyee from previous day,
Those two statements seem contradictory to me  : )

TimeOfDay is a member of the DateTime struct, which is what is returned by the Value member of the DateTimePicker. TimeOfDay is a TimeSpan struct, which holds the hours, minutes, and seconds of the selected time value from the DateTimePicker.

Now my question is the the dateTimepicker is the right control for this scenerio
Again, I don't see why not.
0
 
vbstudent9Author Commented:
I guess you are not reading my question right'

Here is more detail
You work for a company which uses this program
You went in on May 1st and checkd in at 9AM
but you forgot to check out that day

You went back to work on may 5th
when you tried to check in (on may 5th) then the program doesnt let you check in and alerts that you didnt check out on may 1st.

You go to your supervisor and inform her, after lashing out at you, she has to correct your time out for May 1st  for 6PM, on May 5th
0
 
CodeCruiserCommented:
I think you have misunderstood it vbstudent9. TimeOfDay does not necessarily mean TimeOfToday.

Devexpress has 60 free controls for winforms and one of them is TimeEditor. Its a great control and I use it myself.

https://www.devexpress.com/Products/Free/NetOffer/
0
 
vbstudent9Author Commented:
when I change time at execution the value of inTime is correct example 9.00AM on 5/11/11
I used messagebox.show(metime and also intime variable)
but when it is saved in the datbase it saves as 1/1/2001 at 12.00 AM.  All other values are saved correctly.  I am using Access data base with "INtime" property as long time
here is the code
 Dim meTime As DateTime = dtpTime.Value.ToLongTimeString

        inTime = FormatDateTime(meTime, DateFormat.LongTime)    'INTIME is global variable


        Dim newrow As dsHours.tblHoursRow
        Dim hours As New frmModHours

        Try
            newrow = DsHours1.tblHours.NewRow
            hours.newrow = newrow
            newrow("empID") = txtEmpID.Text
            newrow("Workdate") = mydate      'a global variable gets its value from  a calander control
            newrow("TimeIN") = inTime
            newrow("Status") = "Open"
            DsHours1.tblHours.Rows.Add(newrow)
            daHours.Update(DsHours1)
        Catch except As Exception
            MessageBox.Show(except.Message, except.GetType.ToString())
            newrow.CancelEdit()
        End Try
      
        MessageBox.Show("Your signIN time is " & inTime)

        Me.Close()
        MessageBox.Show("Employee Checked In Successfully")

Open in new window

0
 
CodeCruiserCommented:
I would save TimeIn in a Varchar/text field in HH:mm format.
0
 
vbstudent9Author Commented:
code cruiser
after i save time i have to do date math.  will varchar/text field will do it
0
 
CodeCruiserCommented:
You would have to convert it to datetime before doing the calculations. For me, this gives me more control.
0
 
vbstudent9Author Commented:
Code cruiser,

Using your method,  I will have to modify my entire program which is not possible
0
 
vbstudent9Author Commented:
CodeCruiser:
I thought and read more about this issue
instead of using datetimepicker i am using a text box.  and setting it to "Now"
A picture of RUN TIME screen is attached.
Program is working fine and data is loading correct.
Problem I am facing  is " The method of changing date & time is rather crude and doesnt seem elegant
I have to change the date, the hours and  AM/PM manually at runtime.
Can you give some ideas as how can change these values elegantly
I will appreciate your comments
modhours.bmp
modhours2.bmp
0
 
CodeCruiserCommented:
>Can you give some ideas as how can change these values elegantly
What you mean by changing? Converting to datetime?
0
 
vbstudent9Author Commented:
CodeCruiser
I meant changing date, hour, min, AM/PM values on the form.
This form is used for clocking in or clocking out an employee who forgot to clockin or clockout at an earlier date or time.
Henceforth this is occuring at later time.
The text box is set to the current time (ie. "now") at load time.
I need to change the "NOW" time to actual date and time of check in or out, before I upload the clock in or out date and time in the database,
I was thinking to use the buttons with (<   & > symbols) but then I have to use 4 buttons on each side for "Date", Hour, Min, and AM/PM.  I think it will not again be elegant
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.