Solved

Time on a Form

Posted on 2000-03-15
8
147 Views
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 ?
0
Comment
Question by:kennys
8 Comments
 
LVL 6

Expert Comment

by:Marine
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
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 50 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
0
 
LVL 1

Expert Comment

by:PatrickVD
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")
   DoEvents

End Sub


That's all...

Hope this is what you're looking for !
0
 
LVL 1

Expert Comment

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

Sorry.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 32

Expert Comment

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

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

While
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
0
 
LVL 14

Expert Comment

by:wsh2
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>.
0
 
LVL 15

Expert Comment

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

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
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

706 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

15 Experts available now in Live!

Get 1:1 Help Now