parsing a "simple" text string

jadd

Registered User.
Local time
Today, 19:24
Joined
Oct 24, 2002
Messages
13
Hi all,

First I want to say I've been surfing this forum for a year and you all have helped me far more than you could ever imagine.

I've looked quite a bit for an answer to this problem, but have yet to find it.

I have a text string [timedisp] that takes in an amount of time, i.e. :15 or 1:00 or 11:15, etc. The text string is not a fixed length, like hh:mm. What I am trying to do is simply take the number of hours and convert it into minutes and then add the minutes to that.

The following is what I have written:

Me.txttimereal = (Val(Left(Me.txttimedisp, InStr(1, Me.txttimedisp, ":"))) * 60 + Val(Right(Me.txttimedisp, InStr(1, Me.txttimedisp, ":") + 1)))

I currently get the correct answers if [timedisp] is 1:00 (60 minutes) and if it is :15 (15 minutes) but not when the string is 1:15 (it should be 75 minutes, but I get 60 minutes)

I think I'm on the right track, but maybe not.

Any solutions would be greatly appreciated.

Thanks,
John
 
Access 2000 and Up
The Split function and getting the Ubound+1 of the Array

Access 97 and before
See Knowledge base Article:

HOWTO: Simulate Visual Basic 6.0 String Functions in VB5
ID: Q188007

Change your code to:
Me.txttimereal = CalcMin(Me.txttimedisp)

Place this in a Module
Code:
Public Function CalcMin(ByVal sTime As String) As Single
    Dim arTime As Variant
    Dim iLP As Integer
    Dim siValue As Single
    
        'Splits the sTime variable into an Array
        arTime = Split(sTime, ":")
        For iLP = LBound(arTime) To UBound(arTime)
            Select Case iLP
                Case 0: siValue = siValue + (Val(arTime(iLP)) * 60)
                Case 1: siValue = siValue + (Val(arTime(iLP)))
                Case 2: siValue = siValue + (Val(arTime(iLP)) / 60)
            End Select
        Next iLP
        CalcMin = siValue
        
End Function
 
Instead of using the Right function, use the Mid function:

Me.txttimereal = (Val(Left(Me.txttimedisp, InStr(1, Me.txttimedisp, ":"))) * 60 + Val(Mid(Me.txttimedisp, InStr(1, Me.txttimedisp, ":") + 1)))
 
As always you all are great! Thanks a bunch for the suggestion on using Mid instead of Right that is what I was looking for. I'll read up on the difference between the two.

Travis, thanks for the code. I'll plugged it in and try it out, too.

Thanks again

John
 

Users who are viewing this thread

Back
Top Bottom