Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 134
  • Last Modified:

Probably easy to do :)

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

  • 2
2 Solutions
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")
Éric MoreauSenior .Net ConsultantCommented:
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
gecko_au2003Author Commented:
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 ??
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..  

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now