Results 1 to 7 of 7
  1. #1
    rwahdan@gmail.com is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2015
    Posts
    28

    timer - how to calculate hours?

    Hi,



    I have the below code, it is showing 65:00 and counting down, I need it to show 01:05:00 instead so can you help?

    Code:
    Option Compare Database
    Public Loops As Integer
    
    
    Private Sub Form_Load()
    
    
       Me.TimerInterval = 1000
    
    
    End Sub
    
    
    Private Sub Form_Timer()
    Static StartTime As Date
    
    
    Dim SecondsToCount As Integer
    
    
    SecondsToCount = 3900 'Set this variable to the total number of seconds to count down
    
    
    If Loops = 0 Then StartTime = Time
     
     Min = (SecondsToCount - DateDiff("s", StartTime, Time)) \ 60
     Sec = (SecondsToCount - DateDiff("s", StartTime, Time)) Mod 60
     Me.Label2.Caption = Hrs & ":" & Format(Min, "00") & ":" & Format(Sec, "00")
     Loops = Loops + 1
    
    
     If Me.Label2.Caption = "0:00" Then
     timeout = -1
       'Call cmdSubmit_Click
       MsgBox timeout
       Me.TimerInterval = 0
       Exit Sub
     End If
    
    
    End Sub

  2. #2
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,002
    Divide the total minutes by sixty, take that as your hour value.
    Multiply that hour value by 60 and subtract that from your total minutes. that is your remaining minutes.

    And as advice moving forwards;
    Add Option Explicit to the top of all your code modules.
    Declare all your variables,.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    It looks like where you are setting your label2.caption, you are only supplying formatted minutes and seconds, not hours.

    And here you are only getting minutes and seconds,
    Min = (SecondsToCount - DateDiff("s", StartTime, Time)) \ 60
    Sec = (SecondsToCount - DateDiff("s", StartTime, Time)) Mod 60

  4. #4
    rwahdan@gmail.com is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2015
    Posts
    28
    Thanks for the reply but I am doing a count down and if I do that it keeps the minutes and Hours the same number and seconds only counts. Please help?

  5. #5
    rwahdan@gmail.com is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Nov 2015
    Posts
    28
    Quote Originally Posted by Gina Maylone View Post
    It looks like where you are setting your label2.caption, you are only supplying formatted minutes and seconds, not hours.

    And here you are only getting minutes and seconds,
    Min = (SecondsToCount - DateDiff("s", StartTime, Time)) \ 60
    Sec = (SecondsToCount - DateDiff("s", StartTime, Time)) Mod 60
    Thanks for the reply, I took this from a post and I don't know how to get the hours so I can add the label to that? Can you please help me add the hrs to the code?

  6. #6
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,002
    There must be more code to this than you have shown.
    Loops isn't declared anywhere, and as such you don't loop around ? However if it does work -which I doubt ;
    Code:
    
        Min = (SecondsToCount - DateDiff("s", StartTime, Time)) \ 60
    
    
        Hours = (Min \ 60) Mod 60
        'Debug.Print Hours, Min
    
    
        MinsLeft = Min - (Hours * 60)
        
        Sec = (SecondsToCount - DateDiff("s", StartTime, Time)) Mod 60
        'Debug.Print Hours, Min, Sec, DateDiff("s", StartTime, Time)
        Me.Label2.Caption = Hours & ":" & Format(MinsLeft, "00") & ":" & Format(Sec, "00")
        loops = loops + 1
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  7. #7
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Loops is declared at the top as a global variable
    Code:
    Option Compare Database
    Public Loops As Integer
    
    
    Private Sub Form_Load()
    Also note that "Min" (also "Minute") is a reserved word in Access!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Calculate working hours
    By Mark256 in forum Queries
    Replies: 3
    Last Post: 09-09-2014, 08:17 PM
  2. how to calculate hours
    By ultra5219 in forum Queries
    Replies: 2
    Last Post: 05-23-2013, 04:22 PM
  3. Replies: 1
    Last Post: 05-01-2013, 10:53 AM
  4. calculate rate * hours worked
    By hamish mather in forum Queries
    Replies: 1
    Last Post: 12-22-2011, 06:14 AM
  5. How to calculate duration in hours & minutes
    By joypanattil in forum Access
    Replies: 0
    Last Post: 11-25-2009, 04:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums