Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Digital Clock on Main Switchboard

Introduction

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 the current time when the Main Switchboard becomes 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.

Simple Clock Design.

Let us do it together.

  1. Open one of your existing Databases.

  2. 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.

  3. Display the Toolbox (View - ->Toolbox) if it is not visible.

  4. Click on the Label Tool (button face with the letter Aa on it).

  5. 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 - Northwind. An image of the Form in Design View, with a Label Control inserted, is given below:

  6. Type at least one character (any character) in the Label control otherwise the label control will get deleted when you click somewhere else.

  7. While the Label Control is still in the selected state display its Property Sheet (View - ->Properties).

  8. 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.

  9. 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 displayed earlier. If you have closed the Property Sheet follow Step-7 above to display the Property Sheet of the Form.

  10. Find the On Load Property and click on it to select it.

  11. Select [EventProcedure] from the drop-down list box.

  12. Click on the build (. . .) button at the right edge of the Property Sheet 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
  13. 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.

  14. 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 three lines of the VBA Code anywhere within the Form Module.

  15. Close and Save the Form.

  16. Open the Form in Normal View.

Your digital clock will show the Current Date and Time and the 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.

  1. Open the Form in Design View.

  2. Display the VBA Module of the Form (View - ->Code).

  3. 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.

  4. Save the Form and Close it.

  5. Open it in the normal view.

  6. Open some other Forms over the Main Switchboard from your database.

  7. 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.

  8. Close the second Form.

Now the Main Switchboard Form becomes active and the Clock will start updating the Date/Time again.

Technorati Tags:
Share:

10 comments:

  1. Nicely written, well illustrated.

    Steps 15 to 17 don't work. They generate an error.

    ReplyDelete
  2. 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: 

    Private Sub Form_Timer()
    Me.lblClock.Caption = Format(Now(), "dddd dd, mmm-yyyy hh:nn:ss")
    End Sub
     

    ReplyDelete
  3. 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 I’ll be subscribing to your feed and I hope you write again soon!

    ReplyDelete
  4. Nice brief and this enter helped me alot in my college assignement. Say thank you you seeking your information.

    ReplyDelete
  5. Well illustrated, thanks . However the label flashes all the time which is annoying. Can i do something to correct it.

    ReplyDelete
  6. Try slowing down the frequency of updating the label, like let it update the label once in every two seconds interval.

    Change 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.

    ReplyDelete
  7. Thanks a lot, really appreciate your feedback.

    ReplyDelete
  8. This Good thing but the style is looking so odd.

    ReplyDelete
  9. Please 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.

    ReplyDelete
  10. 1. Insert a Text Box in the Detail Section of the Report where you want the Serial Number to appear.
    2. 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

    ReplyDelete

Comments subject to moderation before publishing.

PRESENTATION: ACCESS USER GROUPS (EUROPE)

Translate

PageRank

Post Feed


Search

Popular Posts

Blog Archive

Powered by Blogger.

Labels

Forms Functions How Tos MS-Access Security Reports msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Class Module External Links Queries Array msaccess reports Accesstips WithEvents msaccess tips Downloads Objects Menus and Toolbars Collection Object MsaccessLinks Process Controls Art Work Property msaccess How Tos Combo Boxes Dictionary Object ListView Control Query VBA msaccessQuery Calculation Event Graph Charts ImageList Control List Boxes TreeView Control Command Buttons Controls Data Emails and Alerts Form Custom Functions Custom Wizards DOS Commands Data Type Key Object Reference ms-access functions msaccess functions msaccess graphs msaccess reporttricks Command Button Report msaccess menus msaccessprocess security advanced Access Security Add Auto-Number Field Type Form Instances ImageList Item Macros Menus Nodes RaiseEvent Recordset Top Values Variables Wrapper Classes msaccess email progressmeter Access2007 Copy Excel Export Expression Fields Join Methods Microsoft Numbering System Records Security Split SubForm Table Tables Time Difference Utility WScript Workgroup database function msaccess wizards tutorial Access Emails and Alerts Access Fields Access How Tos Access Mail Merge Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Common Controls Conditional Formatting Data Filtering Database Records Defining Pages Desktop Shortcuts Diagram Disk Dynamic Lookup Error Handler External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Message Box Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar updating upload vba code