You may find several pieces of gadgets like Clocks, Wrist Watches and so on around you to check the Date and Time. How about inserting a Digital Clock into your Project's Main Switchboard Form so that your User can check the Date and Time with a casual glance in the middle of their normal work without interrupting what they are doing?
Besides all that it is a matter of style to put an animated Digital Clock on the Main Switchboard. It takes only a few lines of VBA Code and a Label Control on the Form.
We can stop the Clock when you open other Forms over the Main-Switchboard and re-start the Clock with current time when the Main-Switchboard become active again.
If you have not started using VBA in your Databases and don't know where to begin then this is the time to learn something interesting and simple.
Let us do it together.
- Open one of your existing Databases.
- If you have a Control Screen (Main Switchboard) in your database then open it in Design View. You can open any Form in Design View to try this out.
- Display the Toolbox (View – ->Toolbox) if it is not visible.
- Click on the Label Tool (button face with the letter Aa on it).
- Draw a Label where you would like the Digital Clock to appear on the Form.
I have used a copy of the Main-Switchboard Form from Microsoft Access Sample Database – C:\Program Files\Microsoft Office\Office11\Sample\Northwind.mdb. An image of the Form in Design View, with a Label Control inserted, is given below:
- Type at least one character (any character) in the Label control otherwise the label control will get deleted when you click somewhere else.
- While the Label Control is still in selected state display its Property Sheet (View – ->Properties).
- Change the following Property Values as given below:
- Name = lblClock
- Width = 1.5938"
- Height = 0.3125"
- Border Style = Transparent
- Font Size = 8
- Font Weight = Bold
- Text Align = Center
Now, we need two lines of VBA Code to start running our Digital Clock. One line of Code to start running the IntervalTimer of the Form through Form_Load() Event Procedure, immediately after the Switchboard Form is open.
- Click on the top left corner of the Form, where the Horizontal and Vertical Rulers meet, to select the Form. Now you will see the Form-level Property Values in the Property Sheet you have displayed earlier. If you have closed the Property Sheet follow Step-7 above to display the Property Sheet of the Form.
- Find the On Load Property and click on it to select it.
- Select [EventProcedure] from the drop down list box.
- Click on the build (. . .) button at the right edge of the Property Sheet to open up the VBA Module with an empty skeleton of VBA Sub-Routine as given below:
Private Sub Form_Load() End Sub
- Write (or copy) the following line of VBA Code in the middle of the above lines of Code:
Me.TimerInterval = 1000
This line of Code says that the program control should be passed to the Form’s Timer Sub-Routine (we will write the code for that next) at every one second interval. So whatever program we will write in the Timer Sub-Routine will be executed sixty times per minute or once every second. We will write a one line Code in the Timer Sub-Routine to take the System Date and Time and update the Caption of the Label we have created at the beginning. So, we will be seeing a continuous time change every second.
- Select Timer from the drop-down control at the top of the VBA Module Window.
The opening and closing lines of the Timer Sub-Routine will be inserted into the VBA Module. You must write the line given in the middle by giving spaces and other punctuations correctly between double-quotes (date/time format string).
Private Sub Form_Timer() Me.lblClock.Caption = Format(Now(), "dddd dd, mmm-yyyy hh:nn:ss") End Sub
Alternatively you may copy and paste all the three lines of VBA Code anywhere within the Form Module.
- Close and Save the Form.
- Open the Form in normal View.
Your digital clock will show Current Date and Time and change of time is updated every second.
When you open other Forms or run different programs or macros the Main Switchboard may become inactive and we can turn off the clock temporarily till the Main Switchboard become active again. This will help other programs to run faster without interruption from the digital clock taking time to update the label on the Main Switchboard.
We will write two more lines of Code for the On Deactivate() and On Activate() Event Procedures to turn-off (when the Main Switchboard is inactive) and to turn-on (when the Main Switchboard is active again) respectively.
- Open the Form in Design View.
- Display the VBA Module of the Form (View – ->Code).
- Copy and paste the following VBA Code into an empty area of the Module.
Private Sub Form_Activate() Me.TimerInterval = 1000 End Sub Private Sub Form_Deactivate() Me.TimerInterval = 0 End Sub
- Save the Form and Close it.
- Open it in normal view.
- Open some other Form over the Main Switchboard from your database.
- Click on the Title Area of the second Form and drag away from the Main Switchboard Form so that you can see the Digital Clock on it.
You can see that the clock is not getting updated.
- Close the second Form.
Now the Main Switchboard Form become active and the Clock will start updating the Date/Time again.