|
-
Aug 2nd, 2009, 02:11 AM
#1
Thread Starter
Addicted Member
Problems with count down
It's that "time" again, where one cannot make head or tail of this situation. The problem i'm having is, is once this code is run through, i am being returned a negative number for hours as opposed to the correct amount of time left. in the database being called the Due hour is 3, and the hour(now) for example is 18, however it is throwing the value of -15 at me as opposed to 9 or such.
the first msgbox returns the values "hour now 18, due hour is 3"
clearly hour now is greater than due hour, and it is not running it throught the if statement "if hour(now) > Thour then" etc etc etc.
Thour being the due hour
so this has me stumped :S
any ideas?
(My explaination might not be that clear, if anything needs clarifying i will do so with gusto, or such)
Code:
If Minute(Now) > Tminute Then
HourCount = HourCount + 1
MinuteCount = (Tminute - Minute(Now) + 60)
Else
MinuteCount = Tminute - Minute(Now)
End If
MsgBox "Hour now is" & Hour(Now) & vbCrLf & "Due hour is: " & Thour
If Hour(Now) > Thour Then
DayCount = DayCount + 1
HourCount = HourCount + Thour - Hour(Now)
MsgBox HourCount & "hour is greater than due hour"
Else
HourCount = Thour - Hour(Now)
End If
-
Aug 2nd, 2009, 04:40 AM
#2
Re: Problems with count down
this is simple method
vb Code:
HourCount = Thour - Hour(Now) If Hour(Now) > Thour Then hourcount = 24 + hourcount
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 2nd, 2009, 02:20 PM
#3
Thread Starter
Addicted Member
Re: Problems with count down
[Update on code]
this is what i have so far after shortening it a little
Code:
Do Until InfoCount = Intcount
For prum = 0 To Intcount - 1
InfoCount = InfoCount + 1
If InfoCount = Intcount Then
For d = Intcount To 15
Picture4(d).Visible = False
Next
End If
Thour = .Fields("TimeHourDue").Value 'Due Hour
Tminute = .Fields("TimeMinuteDue").Value 'Due Minute
HourCount = 0
MinuteCount = 0
MinuteCount = Tminute - Minute(Now)
If Minute(Now) > Tminute Then
MinuteCount = MinuteCount + 60
HourCount = HourCount + 1
End If
HourCount = Thour - Hour(Now)
If Hour(Now) > Thour Then
HourCount = HourCount + 24
End If
Next
TDue = HourCount & " Hours and " & MinuteCount & " Minutes."
Label3(prum).Caption = TDue
Still it comes up with a negative value. Can't seem to detect the hour(now) is greater than the due hour. Perhaps in my for next... loop?
-
Aug 2nd, 2009, 03:39 PM
#4
Re: Problems with count down
You should use the DateDiff function to compare times.
-
Aug 3rd, 2009, 03:25 AM
#5
Thread Starter
Addicted Member
Re: Problems with count down
i found a tutorial on the DateDiff function, but!! can you show me how to assimilate the data from my database into the function, to compare with the time now?
Print DateDiff("h", #1/1/2001#, Now)
*wouldn't be something like
Print DateDiff("h", THour, Now)
^^'
Last edited by clownboy; Aug 3rd, 2009 at 03:28 AM.
-
Aug 3rd, 2009, 05:44 AM
#6
Re: Problems with count down
I'm not sure what you mean, because your post contains the answer.
The only potential problem is that Print may not be the output you want.
-
Aug 3rd, 2009, 06:00 AM
#7
Thread Starter
Addicted Member
Re: Problems with count down
i don't think datediff is what i'm after. Thour is an hour value i.e. 20 = 8pm as such. Am being returned a value of somethings around 960046 (as shown in msgbox showing datediff value). what have i missed?
-
Aug 3rd, 2009, 06:32 AM
#8
Re: Problems with count down
Ah, I see the problem - THour (and your database field) do not contain a time at all, just a number which represents the hour of the time. Due to the way VB stores dates, it treats that number as midnight on a particular day (early 1900 I think).
In that case you should use the TimeSerial function to create a time based on that number - and compare it to just Time instead of Now.
-
Aug 10th, 2009, 07:05 AM
#9
Re: Problems with count down
you should combine the hours and minutes, then calculate the time difference in minutes, then recalc back to hours and minutes to display
i still didn't figure why you are using a loop for this, i must have missed something, it appears to only use results from last iteration of loop
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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
|