Results 1 to 10 of 10

Thread: [RESOLVED] TimeSerial Function Overflow

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Resolved [RESOLVED] TimeSerial Function Overflow

    Hey guys, ive been using this code for one of my battery monitor programs for a laptop, and it works fine unless this happens:
    Code:
    MsgBox Format(TimeSerial(0, 0, Int(355) * 100), "H:MM:SS")
    That will give a run time error 6 stating its an overflow. Does anyone know how could make it continue and calculate it without telling me its an overflow? And just so you know, I put 355 in an Int because I replaced lblALabel.Caption with 355, lblALabel.Caption contains 355 in it. I changed it just to show you.

    Alright, thanks!
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: TimeSerial Function Overflow

    It overflows on 355*100 = 35000 which exceeds Integer boundaries.
    If you need to show current time why not using Now() or Time() or at least TimeSerial(hour(time), minute(time), second(time)) instead?
    And what does 355 (other than it is current label caption) mean anyway?

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: TimeSerial Function Overflow

    Why are you using the Int function (which takes a number, and returns the integer portion of it), rather than CInt (which converts data from another data type [in this case, a String] to an Integer value)?

    Once you have that, it is only a small change to CLng, which converts to a Long, which will avoid the error (unless you get to numbers in the billions).


    However, RhinoBull does have valid questions about the purpose of this code.. it seems likely there will be a better way.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: TimeSerial Function Overflow

    As I said, the purpose of this code is for my laptop battery monitor. Im not trying to show the current time, im trying to show the battery life which doesn't have any coding revolving around the current time. TimeSerial is by far, the best way I've found by doing this so far. My project can be located in my previous topic that ive created if you are interested in looking at it. Its not the most updated, but its an idea to give you of why and how im trying to code this.

    Si_geek, what would I need to convert to a long? TimeSerial or the Int function?

    EDIT:
    I've found out that by doing this code, it still gives me the same runtime error. Any suggestions?
    Code:
    MsgBox Format(TimeSerial(0, 0, CLng(CInt(355) * 100)), "H:MM:SS")
    Last edited by GRPsuper9; Jul 14th, 2008 at 02:32 PM.
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: TimeSerial Function Overflow

    Like I said, the Int function should be CInt instead - but as the Integer data type cannot hold the value 355*100, you need to use the Long data type instead - so instead of CInt, use CLng.

  6. #6

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: TimeSerial Function Overflow

    I've edited my post so you guys see how ive converted it with CLng but it still doesn't work. And Rhino, would I be able to convert seconds into time? Because 355 is the seconds it takes for 1% of a charge from my laptop to change, and by multiplying it by 100, it gives me the life of my laptop. So I would like to convert those seconds into time.
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: TimeSerial Function Overflow

    CLng needs to be instead of CInt (which you should have used in the first place, instead of Int).

    The code CInt(355) * 100 will always give an error, no matter what you put around it.



    However, as RB rightly pointed out (I hadn't thought of it myself), the TimeSerial function only allows Integer values anyway, so even if you correct the mistakes in the parameter it will be no use to you.

    One way to do it (there are many!) is to store the value in a Double, but divided by the number of seconds in a day (calculator says 86400), eg:
    Code:
    Dim dblNumDays as Double 
      dblNumDays = (CLng(355) * 100) / 86400
    This may seem odd, but the Date data type is actually stored as a Double (with whole numbers being the number of days, the fractional part being the time).

    So having the value stored like that allows you to Convert it to a Date (CDate), and use the Format function to get the time as you want, eg:
    Code:
    MsgBox Format(CDate(dblNumDays), "H:MM:SS")

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: TimeSerial Function Overflow

    Alright, problem solved. Thank you very much guys.
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [RESOLVED] TimeSerial Function Overflow

    If you want to get ellapsed time your computer is on since last restart then try this as well:
    Code:
    Option Explicit
    
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    
    Private Sub Command1_Click()
    Dim totSeconds As Long
    Dim days As Integer
    Dim hours As Integer
    Dim minutes As Integer
    Dim seconds As Integer
    Dim SecToTime As String
    
        totSeconds = GetTickCount / 1000
        
        days = totSeconds \ 86400
        hours = (totSeconds Mod 86400) \ 3600
        minutes = ((totSeconds Mod 86400) Mod 3600) \ 60
        seconds = ((totSeconds Mod 86400) Mod 3600) Mod 60
        
        SecToTime = "Your computer is on for " & _
                    days & IIf(days > 1, " days and ", " day and ") & _
                    Format(hours, "00") & ":" & _
                    Format(minutes, "00") & ":" & _
                    Format(seconds, "00")
        
        MsgBox SecToTime
    
    End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width