Probably easy to do :)

Posted on 2005-05-14
Last Modified: 2010-05-01
I would like some way of displaying in the forms caption how long the application has been running for in this format :


If someone could give me an idea of what I need to do, thanks in advance !

Kind regards

Question by:gecko_au2003
    LVL 7

    Accepted Solution

    first you need to add a timer control in your form, with properties set to:
    enabled = True
    interval = 1000
    Note: 1000 is for 1000 miliseconds  or 1 second

    declare a  variable in the general Declaration  
    Dim   DatStartTime as Date

    then  on your  Form_Load procedure  of your form
    DatStartTime = now()

    then on the _timer()  event of your timer add
    me.caption =  format(now()-DatStartTime, "dd:hh:nn:ss")
    LVL 69

    Assisted Solution

    by:Éric Moreau
    Something like this:

    Private mdtmStartTime As Date

    Private Sub Form_Load()
        mdtmStartTime = Now
        With Timer1
            .Interval = 1000
            .Enabled = True
        End With
    End Sub

    Private Sub Timer1_Timer()
        Me.Caption = CalculateElapsedTime(mdtmStartTime, Now)
    End Sub

    Public Function CalculateElapsedTime(ByVal pdtmStart As Date, _
                                         ByVal pdtmEnd As Date) As String
    Dim lngDays As Long
    Dim lngHour As Long
    Dim lngMinute As Long
    Dim lngSecond As Long

        lngSecond = DateDiff("s", pdtmStart, pdtmEnd)
        'Calcule les heures
        lngHour = Fix(lngSecond / 3600)
        If lngHour < 24 Then
            lngDays = 0
            lngDays = Fix(lngHour / 24)
            lngHour = lngHour - (lngDays * 24)
        End If
        lngSecond = lngSecond - (lngDays * 24 * 3600) - (lngHour * 3600)
        lngMinute = Fix(lngSecond / 60)
        lngSecond = lngSecond - (lngMinute * 60)
        CalculateElapsedTime = CStr(lngDays) & "d" & _
                               CStr(lngHour) & "h" & _
                               CStr(lngMinute) & "m" & _
                               CStr(lngSecond) & "s"
    End Function
    LVL 23

    Author Comment

    Thanks for the fast replys :) I can see what I was doing wrong now :P I didn't declare the start time in general declarations.

    Any chance you can comment or explain that function to me ??
    LVL 7

    Expert Comment

    well..   anything you declare in the general declarations  is a variable that is available for all the procedures, events on the same modules.  you must be aware that  variable have something  like their  lifecycles.  if you declare a variable within an event for example in the Timer_Timer()  event ,  one good example will be  lngSecond  on emoreau's code, when the  timer event is fired it's initial value is 0,  then  as the  code progress a value will be  given to it, only to disappear   when the End Sub of Timer_Timer() was me.. on the next  running  it will still be set to 0 initially.  Unlike  those declared in general declarations, wherein it's "lifecycle" ends when you close the current form..  
    hope you can understand  my explanation..

    you can also  do the the program without declaring a variable..  

    But it will be this way

      Timer1.tag= now()
    end sub

     me.caption =  format(now()-cdate(timer1.tag), "dd:hh:nn:ss")
    end sub

    this second code uses the tag property, a property which all controls have, that you can use as module variables..  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now