I created a form to calculate sign in time, sign out time, and the elapsed duration.
Below is my code.
CAn anyone please tell me where i dont get it.
Sign in time is OK,Sign out time OK but the worked hrs is not..
Any Help???

Option Explicit
Dim signin As Variant
Dim signout As Variant
Dim worked As Variant
Dim today As Variant


Private Sub cmdsignin_Click()
signin = Now
lblreport.Caption = Format(report, "hh:mm:ss")

today = Now
lblreport.Caption = Format(today, "hh:mm:ss")

End Sub

Private Sub cmdsignout_Click()
signout = Now
lblleave.Caption = Format(leave, "HH:MM:SS")

today = Now
lblleave.Caption = Format(today, "Hh:mm:ss")

End Sub

Private Sub cmdworked_Click()

today = Now
signin = lblreport.Caption
signout = lblleave.Caption
worked = signout - signin
lblworked.Caption = Format(worked, "HH:MM:SS")
lblworked.Caption = Format(today, "hh:mm:ss")

lblreport.Caption = signin
lblleave.Caption = signout

End Sub


Fourty

Recommended Answers

All 6 Replies

Fourty, First things first, Please add your code into code brackets. You just need to click on the (CODE) icon in the reply post toolbar.

Second, your declarations in Variant form will use much more resources than proper declarations as in Date etc.

Thirdly. your problem above lies with your "worked" calculation, because you are trying to deduct numbers from each other, but VB6 reads is with the format type with the ":" inserted, hence your error.

Below is some code that will help you solve your problem. I am using this to calculate my attendance apps. -

'Add the following to a module -
Public Function GetTimeDiff(TimeStart, TimeEnd, txtResult As TextBox)

Dim diff As Long
diff = DateDiff("s", TimeStart, TimeEnd)

TimeString diff, txtResult
End Function

Public Function TimeString(Seconds As Long, txtResult As TextBox, Optional Verbose As Boolean = False) As String

'if verbose = false, returns
'something like
'02:22.08
'if true, returns
'2 hours, 22 minutes, and 8 seconds

Dim lHrs As Long
Dim lMinutes As Long
Dim lSeconds As Long

lSeconds = Seconds

lHrs = Int(lSeconds / 3600)
lMinutes = (Int(lSeconds / 60)) - (lHrs * 60)
lSeconds = Int(lSeconds Mod 60)

Dim sAns As String

If lSeconds = 60 Then
    lMinutes = lMinutes + 1
    lSeconds = 0
End If

If lMinutes = 60 Then
    lMinutes = 0
    lHrs = lHrs + 1
End If

sAns = Format(CStr(lHrs), "#####0") & ":" & _
  Format(CStr(lMinutes), "00") & "." & _
  Format(CStr(lSeconds), "00")

If Verbose Then sAns = TimeStringtoEnglish(sAns)
TimeString = sAns
txtResult.Text = TimeString
End Function

Public Function TimeStringtoEnglish(sTimeString As String) As String

Dim sAns As String
Dim sHour, sMin As String, sSec As String
Dim iTemp As Integer, sTemp As String
Dim iPos As Integer
iPos = InStr(sTimeString, ":") - 1

sHour = Left$(sTimeString, iPos)
If CLng(sHour) <> 0 Then
    sAns = CLng(sHour) & " hour"
    If CLng(sHour) > 1 Then sAns = sAns & "s"
    sAns = sAns & ", "
End If

sMin = Mid$(sTimeString, iPos + 2, 2)

iTemp = sMin

If sMin = "00" Then
   sAns = IIf(Len(sAns), sAns & "0 minutes, and ", "")
Else
   sTemp = IIf(iTemp = 1, " minute", " minutes")
   sTemp = IIf(Len(sAns), sTemp & ", and ", sTemp & " and ")
   sAns = sAns & Format$(iTemp, "##") & sTemp
End If

iTemp = Val(Right$(sTimeString, 2))
sSec = Format$(iTemp, "#0")
sAns = sAns & sSec & " second"
If iTemp <> 1 Then sAns = sAns & "s"

TimeStringtoEnglish = sAns
MsgBox TimeStringtoEnglish
End Function

'Timestart/TimeEnd will be your actual time to start/End counting. This will probably be from a textbox, by using the following code -
Dim xTimeOut, xTimeIn

xTimeOut = txtTotalOut.Text
xTimeIn = txtTimeInOut.Text
'Call the function
GetTimeDiff xTimeOut, xTimeIn, txtTotalOut

'txtTotalout will hold the time difference between your 2 dates.

Also enclose the text in your textboxes using a proper format -

txtTimeInOut.Text = Format(time, "hh:mm:ss")

I hope this helped...

Thanks Andre

Very Very much
I owe you big..

No problem. All in a days work...

Andre, looks good but have a look at CDate to convert strings to a numerica value, then do the subtraction followed by the format function as in this abbreviated example...

Option Explicit

Dim Start As Date

Private Sub Command1_Click()
Dim MyStop As Date, Difference As Date
MyStop = Now
Difference = MyStop - Start
MsgBox Format(Difference, "hh:mm:ss")
End Sub

Private Sub Form_Load()

Start = Now

End Sub

Also Andre, I think there may be times where your code would be preffered over other code, but remember, there is always more than one way to skin a cat... :)

Good Job :)

Poor cat, 7 lives AND a few skins later...LOL

I know my code tends to be "long" some times, but I do a lot of OOP in my apps. I hate to reproduce code, and I firmly believe that 90% of users are fools, they WILL find a way to crash your app. I rather catch all before that happens.

Thanks for the compliment, highly appreciated.

nO CAT FIGHTS..
pROBLEM SOLVED I just had to edit the code a little to do

Hey prgrmr, I like your precision too..sometimes come handy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.