Introduction
You probably have several timekeeping devices around you—clocks, wristwatches, or even your computer—to check the current date and time. But why not add a digital clock to your project’s Main Switchboard form? This way, users can check the date and time at a glance, right in the middle of their work, without interrupting their workflow.
Beyond practicality, an animated digital clock also adds a touch of style to the Main Switchboard. All it takes is a label control on the Form and just a few lines of VBA code.
The clock automatically pauses whenever another form is opened over the Main Switchboard, and it restarts with the current time as soon as the Switchboard becomes active again.
If you’ve never used VBA in your databases and aren’t sure where to begin, this is the perfect opportunity to start with something simple, useful, and fun. Simple Clock Design.
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 (a button with the letter "A" on it).
Draw a Label where you would like the Digital Clock to appear on the Form.
For this example, I used a copy of the Main Switchboard form from the Microsoft Access sample database Northwind. The image below shows the form in Design View, with a Label control inserted for displaying the digital clock.
Type at least one character (any character) in the Label control; otherwise, the Label control will disappear when you click elsewhere.
While the Label Control is still in the 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
Next, we need just two lines of VBA code to start the digital clock. The first line goes in the Form_Load() event procedure of the Switchboard form. This code starts the form’s IntervalTimer immediately after the Switchboard opens, ensuring the clock begins running as soon as the form is displayed.
Click the form selector at the top-left corner of the form, where the horizontal and vertical rulers intersect, to select the entire form. The Property Sheet will now display the form-level properties. If the Property Sheet is closed, follow Step 7 above to reopen it and view the form’s properties.
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 to open up the VBA Module with an empty skeleton of the VBA Sub-Routine as given below:
The Form_Load() Event and Code.
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 tells Access to pass program control to the form’s Timer subroutine (which we will write next) at one-second intervals. In other words, whatever code we place in the Timer subroutine will be executed once every second, sixty times per minute.
In the Timer subroutine, we will add a single line of code that retrieves the system date and time and updates the caption of the label we created earlier. As a result, the label will display a continuously updating digital clock, refreshing 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 inserting spaces and other punctuation 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 can copy and paste all three lines of the VBA Code anywhere within the Form Module.
Close and Save the Form.
Open the Form in Normal View.
Your digital clock will show the Current Date and Time, and the time change is updated every second.
When other forms are opened or different programs and macros are running, the Main Switchboard Form becomes inactive. In such events, the digital clock can be temporarily turned off until the Switchboard becomes active again. This prevents unnecessary updates to the clock’s label and allows other programs to run more efficiently without interruptions from the clock’s timer.
We will write two more lines of code for the On Deactivate() and On Activate() Event Procedures to turn off the Timer (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
Trial Run of Form Activity.
Save the Form and close it.
Open it in the normal view.
Open some other Forms over the Main Switchboard from your database.
Click on the Title Area of the second Form and drag it 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 becomes active, and the Clock will start updating the Date and Time again.
Nicely written, well illustrated.
ReplyDeleteSteps 15 to 17 don't work. They generate an error.
If you have changed the Name Property Value of the Label Control to lblClock correctly then it should work without errors. Check and re-confirm this. You may copy and paste the following Code into the VBA Module of your Form and try again:
ReplyDeletePrivate Sub Form_Timer()
Me.lblClock.Caption = Format(Now(), "dddd dd, mmm-yyyy hh:nn:ss")
End Sub
Pretty nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case Ill be subscribing to your feed and I hope you write again soon!
ReplyDeleteNice brief and this enter helped me alot in my college assignement. Say thank you you seeking your information.
ReplyDeleteWell illustrated, thanks . However the label flashes all the time which is annoying. Can i do something to correct it.
ReplyDeleteTry slowing down the frequency of updating the label, like let it update the label once in every two seconds interval.
ReplyDeleteChange the line : Me.TimerInterval = 1000
To: Me.TimerInterval = 2000
wherever it is appearing on the Form Module.
If you don't need the Seconds segment in the Time format then you can modify the format string: "dddd dd, mmm-yyyy hh:nn:ss" to "dddd dd, mmm-yyyy hh:nn" and the Timer Inverval to 60,000 to update the label every minute.
Thanks a lot, really appreciate your feedback.
ReplyDeleteThis Good thing but the style is looking so odd.
ReplyDeletePlease could anyone help me out with my problem? i know this isn't difficult for you to give me idea how to print a report with a serial number like 1,2,3,4,5,6,7,8,9....etc.after applying a filter like date wise, i want the serial number to appear as 1,2,3,4,5,6....etc not like 4,6,5,1,2,4. For further example, today i have entered 10 delivery notes dated 11/12/2010 and then the following day ive entered 20 delivery notes dated 12/12/2010 after filtering from 11/12/2010 to 12/12/2010 i want those numbers to appear like 1,2,3,4,5,6,....etc since in my report if i filtered two days it shows a random number like 4,3,6,1,2,5!!please help me out of this! its really a terrible report if that would seem to appear in my report.
ReplyDelete1. Insert a Text Box in the Detail Section of the Report where you want the Serial Number to appear.
ReplyDelete2. Display the Property Sheet of the Text Box (View - -> Properties)
3. Enter the expression =1 in the Control Source Property.
4. Set the Running Sum Property value to Yes.
5. Save the Report and Open it in Print Preview and check the Text Box Value.
If you want the Serial Numbers to appear in a Query Column itself then check the following Link: Auto Numbering in Query Column