Hack 25. Put a Clock on a Form
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.
3.8.1. 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")
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).
3.8.2. 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 with two clocks. One displays the time in New York, and the other displays the time in Chicago.
Figure 3-23. Two clocks on a form
Chicago is one hour behind New York. You account for the one-hour difference by applying the DateAdd function. Here is the updated On Timer event:
Me.lblClockNewYork.Caption = Format(Now( ), "hh:mm:ss AMPM") Me.lblClockChicago.Caption = Format(DateAdd("h", -1, Now( )), _ "hh:mm:ss AMPM")
DateAdd can add or subtract time. In this case, a value of -1 subtracts one hour.
Here's another idea: give the user a way to change the format. You accomplish this by using a public variable and the label control's DblClick event. When the form is opened, a public variable, named format_type in this example, is given a value of 1. Each time a user double-clicks the clock, the format_type variable increments. When it hits 4, it is set back to 1. The On Timer event tests the format_type variable and applies the particular format. Here is the full code behind the form that takes care of this:
Option Compare Database Public format_type As String Private Sub Form_Open(Cancel As Integer) format_type = 1 End Sub Private Sub Form_Timer( ) Select Case format_type Case 1 Me.lblClock.Caption = Format(Now( ), "hh:mm:ss AMPM") Case 2 Me.lblClock.Caption = Format(Now( ), "hh:mm AMPM") Case Else Me.lblClock.Caption = Format(Now( ), "mm/dd hh:mm AMPM") End Select End Sub Private Sub lblClock_DblClick(Cancel As Integer) format_type = format_type + 1 If format_type = 4 Then format_type = 1 End Sub
Now, a user can double-click the clock until it displays the date and time in a way that suits him. Of course, you can easily increase the number of available formats.