Results 1 to 9 of 9

Thread: Problems with count down

  1. #1

    Thread Starter
    Addicted Member clownboy's Avatar
    Join Date
    Aug 2008
    Location
    Wellington, NZ
    Posts
    137

    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

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Problems with count down

    this is simple method

    vb Code:
    1. HourCount = Thour - Hour(Now)
    2.         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

  3. #3

    Thread Starter
    Addicted Member clownboy's Avatar
    Join Date
    Aug 2008
    Location
    Wellington, NZ
    Posts
    137

    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?

  4. #4

  5. #5

    Thread Starter
    Addicted Member clownboy's Avatar
    Join Date
    Aug 2008
    Location
    Wellington, NZ
    Posts
    137

    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.

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

    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.

  7. #7

    Thread Starter
    Addicted Member clownboy's Avatar
    Join Date
    Aug 2008
    Location
    Wellington, NZ
    Posts
    137

    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?

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

    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.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
  •  



Click Here to Expand Forum to Full Width