<< Click to Display Table of Contents >> Navigation: Chapter 3. Entry and Navigation > Hack 25. Put a Clock on a Form |
Hack r5. Put a lock on a FormGive 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 ClockThe 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
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 HackYou 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
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")
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
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. |