Solved

Time on a Form

Posted on 2000-03-15
8
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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!

 
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
 
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 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
0

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

696 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