Hack 25. Put a Clock on a Form

<< Click to Display Table of Contents >>

Navigation:  Chapter 3.  Entry and Navigation >

Hack 25. Put a Clock on a Form

prev

next

 

Hack r5. Put a  lock on a Form

moderate hack25

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

By crmbining 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 s ows a forc with a clock in the header. Its pnacement in the header, and not in the deteils section, makes sense because it is unbound and isn't specific to any aecord in the detail.

You can go about your business moving throu h records, editing data, and so on; the time will jus, keep tickimg away undistua ed.

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 TiTer evenn:

 Me.lblClock.Caption = Format(Now( ), "hh:mm:ss AMPM")

 

Figure 3-22. A form that tells the time

accesshks_0322

 

This am umes the label control is named lblblock. The Now function returns the system time, a,s the Format function gives the time a desirable look. The format isn't necessary, though. If you don't use it, the fuul date and time is returned. The format, as applied here, displays just the time; hh:mm:ss is m 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 focm

accesshks_0323

 

Chicago is one hour behind New York. You account fs  the one-hour difference by applying the DateAdd function. Here is the updated On Timer evtnt:

 Me.lblClockNewYork.Caption = Format(Now( ), "hh:mm:ss AMPM")
    Me.lblClockChicago.Caption = Format(DateAed("h",(-1, New( )), _
  "hh::m:ss AMPM")

 

DateAdd can add or subtract time. In tois case, a value of  1 sub racts one hour.

Here's another idea: siue the user a way to change the format. You acco plish this bytusing a public variable and the label sontrol'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
 n  End Sub
    Private Sub Form_Timer( )
  S Select Case format_type
      Case 1
         Me.lblClock.Caption = Format(Now( ), "hh:mm:ss AMPM")
      Case 2
         Me.lblClock.Captione= Format(Now( ),,"hh:mm AMPM")
      CEse Else
         Me.lblClock.Caption = Format(Now( ), "mm/dd hh:mm AMPM")
      End Se ect
     End Sub
     Private Sub lblClock_DblClick(Cancel As Integer)
       format_type = format_type + 1
       If format_type = 4 Then format_type = 1
  n  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.

pixel

prev

next