Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Dots and Exclamation Marks Usage with Objects in VBA


Beginner VBA enthusiasts often get confused as to how to address control on a Form or on Sub-Forms. It becomes more difficult when there is an inner-form within a Sub-Form. Specifying the address of the control on the inner sub-form correctly will only help to access the Text control's contents or to set the control's value directly through VBA. How, to join each element of the address? Join each element with a dot (.) or is it an exclamation mark (!) or both?

Let us start with the simple usages of dots and exclamation marks in object specifications.

I have designed a sample Main Form and two Sub-Forms. The second sub-form is inside the first Sub-Form. The Forms design view and normal view images are given below for reference.

Sample Form in Normal View

Dots and Exclamation Symbols.

General Usages of the dot (.) and exclamation symbol (!) in object references.

  • a dot (.) - used after an object name to access its methods or properties.
  • the exclamation mark (!) - used after an object name to refer to the sub-object/control of the Top-level Object.

Sample Forms:

Main Form: frmMain

Sub-Form of Main Form: frmSubForm

Inner Sub-Form on frmSubForm:  frmInnerSubForm

Text Control on frmInnerSubForm:  Text7

Another Unbound Text Control on frmInnerSubForm:  Text3

All three forms are designed without linking to any Table (all form's Record Source property is empty). All the Text controls on the Form are unbound.

You may design three sample forms with few unbound text boxes and set them up one inside the other and open it in a normal view. Open VBA Editing Window and open the Debug Window (Ctrl+G). Now you are ready to try out the following examples after typing the code directly on the Debug Window and pressing Enter Key.

Practical Exercises.

First, we will try to read the contents of the Text7 textbox (i.e. 'Innermost Form') from frmInnerSubForm and display the result on the Debug Window.

? Forms!frmMain!frmSubForm.Form!frmInsideSubForm.Form!Text7

Printed Result is: Innermost Form

I have written an expression ="Innermost Form" inside the Text7  Text box, that is displayed in the Debug Window.

Forms!frmMain!frmSubForm.Form!frmInsideSubForm.Form!Text3 = "ms-access"

Result: Text3 textbox of the Innermost Form is assigned with the text: ms-access.

Check on your opened Form after executing the above statement on the Debug Window.

Let us examine each part of the above command joined by an exclamation mark or dot.  I call this a command because we directly execute this program statement in the Debug Window, with the? (Print VBA command).

  • Forms: Collection of all Forms loaded in memory. All Forms opened in memory are indexed in the order in which they are loaded into memory, i.e. first form opened will have the index number 0, the second form is 1, and so on. If your main form has two more forms on it as Sub-Forms they are treated as controls of the Main-form, like any other control (Combobox, labels, textbox, etc.). They will not appear as a separate form in the Forms collection. We can display the name of the first open form in memory with the following command using zero as its index number and printing its Name Property Value.
    ? Forms(0).Name

    Result: frmMain - Form's Name property value is printed.

    ? Forms(0).Width

    Result: 21225 (you will get a number similar to the one given to the left) - the width of the form given in the measure of Twips. 1 Inch = 1440 twips. VBA converts the value you set in the Width Property of the Form in Inches or Centimeters or any other regional value it will be converted into twips.

    If you know the Form's name then the above command can be given as below:

    ? Forms("frmMain").Width 

    In this form of command Form's name can be given as a string in parenthesis.


    ? Forms!frmMain.Width

    In this command, immediately after the Forms Collection name, we have used the symbol !  to give the name of the next level of the object of the Forms collection object, not a property or method. The next item Width is a property of the frmMain object, so a dot is required, not the other symbol.

    Note: You cannot use the symbol ! in place of the dot (.) to address the Width or Name or any other property of the Form. There are over 200 properties for a form. You can display the Name of all the properties by typing the following Code, on one line, on the Debug Window and pressing Enter Key:

    For j=0 to Forms(0).Properties.count-1:? j,Forms(0).Properties(j).Name:Next

    In the above statement, we have called the Count() Method of the Forms Property to take a Count of properties of the first form open in memory and print the Name of each Property, one by one.

    Take a Listing of the Property Names.


    For j=0 to Forms("frmMain").Properties.count-1:? j,Forms("frmMain").Properties(j).Name:Next


    For j=0 to Forms!frmMain.Properties.count-1:? j,Forms!frmMain.Properties(j).Name:Next

    Note the usage of Forms("frmMain") and !frmMain, two different ways to refer to the frmMain object of the open Forms Collection Object.  In both cases, the form's name is given immediately after the open Forms collection name. But,  frmMain form is not open at the time of executing this statement it will end up with an Error.  Forms(0) refers to any form that is open first in memory. It will fail only when no form is open at the time of executing this statement.

    ? Forms.Count

    The Count() method of the open Forms Collection Object will print the count of open Forms

We will explore this topic further in the next Post.


No comments:

Post a Comment

Comments subject to moderation before publishing.

MSA GURU : Access Tips & Tricks App

  • Download Android App 'MSA Guru' Version of LEARN MS-ACCESS TIPS AND TRICKS from Google Play Store.

Want to Post Free Ads on the Web


Subscribe in a reader
Your email address:

Delivered by FeedBurner


Popular Posts

Blog Archive

Powered by Blogger.


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 Graph Charts Query VBA msaccessQuery Calculation Event List Boxes TreeView Control Command Buttons Controls Data Emails and Alerts Form ImageList Control Custom Functions Custom Wizards DOS Commands Data Type Key Object Reference ms-access functions msaccess functions msaccess graphs msaccess reporttricks Command Button ListView Control 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