Time on a Form

Posted on 2000-03-15
Medium Priority
Last Modified: 2012-05-04
I would like to display the time on a form
which updates in real time as long as the form
is open.
Can someone show me how ?
Question by:kennys

Expert Comment

ID: 2620606
put the code in a timer and just use Time function. This can work . Set its interval to 1.

and set a variable or soemthing or a label which will show this.
Private Sub Timer1
timer1.interval = 1
Lable1.Caption = Time()
End Sub
LVL 32

Accepted Solution

Erick37 earned 200 total points
ID: 2620613
Put a Timer control and a Label control on your form:

Option Explicit

Private Sub Form_Load()
    Timer1.Interval = 300
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Label1.Caption = Format(Time, "hh:nn:ss AM/PM")
End Sub

Expert Comment

ID: 2620632
Hi Kennys,

Basically, you can do this very simple.
All you need is a Timer control and a label on the form you want to show the time on. (It might be interesting to encapsulate this in a custom control in order to reuse this easely on different forms...)

Here's what you need to do to show Time on a form.
1. Create a new Form
2. put a Label on it (resize to fit the time in there) and change the name to 'TimeLabel'
3. put a timer control on the form and set its name to 'TimeUpdater'. Make sure to set the Enabled property to 'True' and the interval = 1000.
4. In the TimeUpdater_Timer event(assuming that the name of the timer control is 'TimeUpdater'), you simply add the following code:

Private Sub TimeUpdater_Timer()

   TimeLabel.Caption = Format(Now,"Hh:Nn:Ss")

End Sub

That's all...

Hope this is what you're looking for !
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Expert Comment

ID: 2620646
Sorry guys... It seems I was typing my solution at the same time as the others...

LVL 32

Expert Comment

ID: 2620697
I guess the only point of difference is if you want 24 hour time or AM/PM time.

Label1.Caption = Time
Label1.Caption = Format(Time, "hh:nn:ss AM/PM")
both return AM/PM time

Label1.Caption = Format(Time, "hh:nn:ss")
will display 24 hour time.

Another point:  The timer has a minimum resolution of 55 (milliseconds).  Set your timer Interval to be less than 1000 (1 second) ans greater than 55.  I used 300, which updates the display every 0.3 seconds.  Anything faster is not needed and will take more CPU time to update.

You could also update the Label only when the time changes like this:

Private Sub Timer1_Timer()
    Static tPrevTime
    If (tPrevTime <> Time) Then
        Label1.Caption = Format(Time, "hh:nn:ss")
        tPrevTime = Time
    End If
End Sub
LVL 14

Expert Comment

ID: 2620851
What is with setting the Timer interval to 300?.. ie.. 3 Events per second, 2 of which we already know will go wasted. Erik37's comment of a possible 55 millisecond error is a good one.. therefore we should set our interval to (1000 (one second) - 55) - 1 = 944.. to avoid wasting cycles and to still maintain accuracy.. <smile>.

By the way.. (although this is far from being the best solution).. the Statusbar control contains builtin clock support.. <duh, way tago WSH2.. LOL>.
LVL 15

Expert Comment

ID: 2622883
If your app is busy,
you'll need < 900 ms period, just in case... Also, add 'Label1.Refresh' for immediate update
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2623331
If you have a Statusbar control already on your form, you may define one of the panels to display the time (or the date). The time is updated automatically.
If you don't want that control, use the proposed solutions above

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

587 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