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

Control arrays and time entry

Status
Not open for further replies.

gaus

Programmer
Feb 5, 2001
92
US
Hi all,
I am a relative beginner in VB. I know I can do certain things, but I am not sure of how. I have a time entry form that has a control array of 12 text boxes for check-in/out times (6 for each). I want to know the syntax for looping thru the controls. That is probably the easy part. I was wondering if anyone knows how, as I do, I can calc the actual time between each check-in and check-out to figure the total hours worked. E.G., txt_checkin_out(0) = 8:50 (this is the first check-in time and the first control in the array), and txt_checkin_out(1) = 11:30 (this is the first check-out time and the 2nd control in the array). How do I translate that into hours and minutes worked. I assume it is either some type of time() function or perhaps datediff()? Any help would be greatly appreciated.
Thanks,
Gary
 
If the check in and check out values are to be set in real time, you can easilly do this with the Now() function (remember to dim the variable as Date). If user is to manually submit the check in and check out,time himself, put following values in to a Date variable according to this syntax:
myVar = #month/day/year hour/minute/second#

You can then use these values to calculate difference with the function Datediff(). The result will be given in minutes.
Try the example below:

Dim startWork As Date
Dim endWork As Date
startWork = #5/8/2002 8:00:00 AM# ' indicates 08:00 AM, 8th of may 2002, set manually
endWork = Now() 'date set realtime (system date)
Debug.Print "Start: " & startWork
Debug.Print "End: " & endWork
Debug.Print "Time worked: " & DateDiff("n", startWork, endWork) & " minutes"

Hope it gets you further.

Magnus
 
If not, I have another way...

If you convert the date to a single, you'll find that a day takes 1 whole unit. Since the day begins at 12 o'clock midnight, you can calculate an exact value for the time you want (either break up the string, or if the time is entered as a decimal, use some maths...) You should look at the mod operator - it's very handy if you are going to do the conversion yourself...
 
Hi,
Thanks for the response. What I am doing is allowing them to input their own start and end times within the text boxes (up to 6 of each - check in and out), then calculate the time diff between each starting and ending time. I think accumulating that would be easy, but I need to discover how to calculate the difference between the 2 values entered in the text boxes by the user. They are simply in the format hh:mm. Does that make sense? I put the boxes in an array to allow easy looping/manipulation/rollup of calculated time differences. During the day, they may enter 4 check-in times and 4 check-out times, and I would need to calculate the difference between check-in/out, and then add them all together to determine hours worked. Prety strange, but that is the way they want it. Perhaps there is an easier method.
Thanks,
Gary




 
I have a couple of functions here that you may find helpful.

===================================================================

Public Function TimeToDec(InTime As String) As Single

Dim Hours As Integer
Dim Minutes As Integer
Dim DecMins As Single
Dim Delim As Integer

Delim = InStr(1, InTime, ":")
If (Delim > 0) Then
Hours = Val(Left(InTime, (Delim - 1)))
Minutes = Val(Mid(InTime, (Delim + 1)))
Else
Hours = Val(InTime)
Minutes = 0
End If

DecMins = Int(((Minutes / 60) * 100) + 0.5)

TimeToDec = (Hours + (DecMins / 100))

End Function

Public Function DecToTime(InTime As Single) As String

Dim Hours As Integer
Dim Minutes As Integer

Hours = Int(InTime)
Minutes = ((InTime - Hours) * 100)
Minutes = (Minutes * 60) / 100

DecToTime = Format(Hours, "0#") & ":" & Format(Minutes, "0#")

End Function

===================================================================

To calculate the total time you could do something like the following:

TotalTime = 0
For Idx = 0 To 10 Step 2
ElapTime = TimeToDec(txt_checkin_out(Idx + 1)) - TimeToDec(txt_checkin_out(Idx))
TotalTime = TotalTime + ElapTime
Next idx

FormatedTotal = DecToTime(TotalTime)

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
This looks like it is exactly what I am looking for! Is this from past experience? I will give it a try and see how it works. Thanks very much!!!
Gary
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top