Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Time on a Form

Posted on 2000-03-15
8
Medium Priority
?
154 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 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
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
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!

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 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…
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…
Suggested Courses

636 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