Hack #25. Put a Clock on a Form

Give users the time and date, even for more than one time zone.

By combining a form's On Timer event, its timer interval, and the system clock, you can place a functional clock on a form.

Figure 3-22 shows a form with a clock in the header. Its placement in the header, and not in the details section, makes sense because it is unbound and isn't specific to any record in the detail.

You can go about your business moving through records, editing data, and so on; the time will just keep ticking away undisturbed.

Creating the Clock

The clock is easy to create. First, place a label control on the form. Then, set the form's timer interval to 1,000 (which equals one second). Finally, place a single line of code in the form's On Timer event:

	Me.lblClock.Caption = Format(Now( ), "hh:mm:ss AMPM")
A form that tells the time

Figure 3-22. A form that tells the time

This assumes the label control is named lblClock. The Now function returns the system time, and the Format function gives the time a desirable look. The format isn't necessary, though. If you don't use it, the full date and time is returned. The format, as applied here, displays just the time; hh:mm:ss is a format for hours (hh), minutes (mm), and seconds (ss).

Hacking the Hack

You can do a lot to boost the clock's appeal and functionality. One idea is to display the time for cities in different time zones. Figure 3-23 shows a form ...

Get Access Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.