# Thread: houres counting

1. Registered User
Join Date
Apr 2013
Posts
2

## Unanswered: houres counting

please i do want to do somme calculation like : 123h34 - or + 3456h12min and have result in a text box and a table. all that in order to control MTBF(mean time between failer) of a mecanical componenent
thanks

2. Moderator
Join Date
Mar 2009
Posts
5,442
Search in Access help for the DateAdd() and DateDiff functions. They also work for time-only values.

3. Registered User
Join Date
Apr 2013
Posts
2
thanks
i did but no answer to my question

Join Date
Nov 2004
Location
out on a limb
Posts
13,692
how is your data stored
is it stored as decimal hours, minutes or as a string (999h99)
the smartedt woudlbe as noMinutes, and then format that as required

however the fact that you are asking the question suggests to me is as a string to do that I'd probably write a function that returned the number of minutes or hours

eg
Code:
```Public Function ConvertStringTime(TimeText As String) As Double
'ConvertStringTime converts a string representation of hours and minute (TimeText)
'and returns then decimal number of hours that string represents
'TimeText inputformat is xhyy
'where x is the number of hours can be any size and be signed, if not signed assume +ve
'y is two digits reprsenting the number of minutes
'the function returns a representation of that time as decimal hours
'ferinstance 123h45 returns 123.75

ConvertStringTime = 0 'set up our default return value

'lets filter out duff data
'first off if we have a null string or a string comprising less than 3 characters its duff, so can it
If IsNull(TmeText) Or Len(TimeText) < 3 Then Exit Function

'have they been dumb enough to supply a decimal value already
If (Mid(TimeText, Len(TimeText) - 2, 1)) = "." Then
If IsNumeric(TimeText) = True Then
ConvertStringTime = CDbl(TimeText)
End If
Exit Function
End If

'OK so is it a valid format? IE has an h in 3rd right position
If UCase(Mid(TimeText, Len(TimeText) - 2, 1)) <> "H" Then Exit Function

'OK so are the last two characters (the minutes portion) numeric
If IsNumeric(Right(TimeText, 2)) = False Then Exit Function

'AND is the number of minutes valid
If CInt(Right(TimeText, 2)) > 59 Then Exit Function

'just in case we have 0 hours eg h56 force that to be 0h56
If Len(TimeText) = 3 Then TimeText = "0" & TimeText

'are the first n characters (the hours portion) numeric
If IsNumeric(Left(TimeText, Len(TimeText) - 3)) = False Then Exit Function

'phew, so we got to here we think timetext contains valid data
'so convert timetext into decimal hours
ConvertStringTime = CInt(Left(TimeText, Len(TimeText) - 3))

'if the hour component is negative
If ConvertStringTime < 0 Then 'subtract the minute component
ConvertStringTime = ConvertStringTime - CDbl(Right(TimeText, 2)) / 60
Else 'otherwise add the minute component
ConvertStringTime = ConvertStringTime + CDbl(Right(TimeText, 2)) / 60
End If
End Function

Public Function FormatToHoursMinutes(TimeValue As Double) As String
'converts decimal hours to xxhmm
'ferinstance 45.5 = 45h30
Dim NoHours As Integer
Dim NoMinutes As Integer
NoHours = Fix(TimeValue) 'use fix to round the hours component
NoMinutes = (Abs(TimeValue) - Abs(NoHours)) * 60 'find the minutes component (but alwasy use a positive number
FormatToHoursMinutes = NoHours & "H" & Format(NoMinutes, "00")
End Function```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•