?
Solved

DateTimePicker in vb.net

Posted on 2011-05-08
14
Medium Priority
?
3,368 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:vbstudent9
  • 7
  • 5
  • 2
14 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35717639
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
 

Author Comment

by:vbstudent9
ID: 35717764
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35717804
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vbstudent9
ID: 35717839
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35719150
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
 

Author Comment

by:vbstudent9
ID: 35790371
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35792225
I would save TimeIn in a Varchar/text field in HH:mm format.
0
 

Author Comment

by:vbstudent9
ID: 35798575
code cruiser
after i save time i have to do date math.  will varchar/text field will do it
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35800329
You would have to convert it to datetime before doing the calculations. For me, this gives me more control.
0
 

Author Comment

by:vbstudent9
ID: 35864085
Code cruiser,

Using your method,  I will have to modify my entire program which is not possible
0
 

Author Comment

by:vbstudent9
ID: 36171367
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36171555
>Can you give some ideas as how can change these values elegantly
What you mean by changing? Converting to datetime?
0
 

Author Comment

by:vbstudent9
ID: 36174719
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 36183914
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question