|
-
Jul 14th, 2008, 12:12 PM
#1
Thread Starter
Fanatic Member
[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.
-
Jul 14th, 2008, 12:19 PM
#2
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?
-
Jul 14th, 2008, 01:20 PM
#3
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.
-
Jul 14th, 2008, 02:21 PM
#4
Thread Starter
Fanatic Member
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.
-
Jul 14th, 2008, 02:31 PM
#5
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.
-
Jul 14th, 2008, 02:38 PM
#6
Re: TimeSerial Function Overflow
But you can't use anything but Integer with TimeSerial so how useful would be CLng conversion function?
Perhaps GetTickCout or something will work for you better.
-
Jul 14th, 2008, 02:47 PM
#7
Thread Starter
Fanatic Member
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.
-
Jul 14th, 2008, 03:02 PM
#8
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")
-
Jul 14th, 2008, 03:16 PM
#9
Thread Starter
Fanatic Member
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.
-
Jul 14th, 2008, 04:44 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|