Naming Objects and Controls

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Objects and controls, and variables that represent objects and controls, should be named with a prefix that identifies the item and a mixed-case name that clearly identifies the item's purpose. In this context, the term objects refers to object variables that represent items such as documents, workbooks, forms, reports, recordsets, the application itself, and other items exposed through a Microsoft® Office XP application's type library.

When you create a new module or form or add a control to a document, form, or report, the Visual Basic Editor creates a default name for the object, such as Module1, Form3, or TextBox5. You should avoid using these default names in your code. Develop the habit of specifying a meaningful name for an object as soon as you add it to your project. That way, you will not have to revise your code to rename objects later. The name should include a prefix that specifies what the object is and a name that identifies its purpose. For example, you could use modDataAccessCode, frmCustomers, and txtLastName to represent a module, a form, and a text box control. A three-character prefix is preferred, but the important point is that the prefix should be adequate to clearly specify the control type.

Note   When you are designing custom object models, you should use object names without prefixes and instead use names that indicate the purpose of the objects in the model. Custom objects are designed to be used by other developers and are exposed through the Object Browser; therefore, prefixes do not make sense in this context.

When you create HTML objects and controls, you must specify a name by using the object's ID parameter. If you use a tool to add controls to an HTML page, the tool often will insert a default name for an object or control, the same way that the Visual Basic Editor does. For example, if you add a Microsoft® Forms 2.0 CommandButton control to an HTML page by using the Microsoft® ActiveX® Control Pad, the control's ID parameter is given the name CommandButton1 by default. These objects and controls should always be renamed according to the guidelines discussed in this section.

HTML element names (tags) should be entered in all capital letters. Although HTML is not case-sensitive, using this convention will help create a visual distinction between HTML elements and other items on the page. You might think of this technique as being equivalent to Microsoft® Visual Basic® for Applications (VBA) keywords being highlighted in the Visual Basic Editor. For example, if you examine the HTML code in the following example, the use of uppercase HTML element names clearly distinguishes them from the other items on the page:

<HTML>
<HEAD>

<TITLE>
   Developing Office Deveoper VBA and Workflow Solutions, Formatting HTML Elements
</TITLE>

<STYLE>
   .CenterThisRed   {position:absolute; left:40%; top:220; font:bold; color:red}
   .BoldAndBlue    {font:bold; color:blue}
</STYLE>

<SCRIPT LANGUAGE="VBSCRIPT">
<!--
   Option Explicit
   Dim strMessage

   Sub ShowAMessage(strMessage)
      ' Display strMessage in a message box.
      If Len(strMessage) = 0 Then
         strMessage = "You need to enter some text in the " _
            & "'Enter Text Here' text box before you can " _
            & "see it displayed here!"
      End If
      MsgBox strMessage
   End Sub

   Sub cmdMessage_OnClick()
      ShowAMessage(frmSampleForm.txtMessage.Value)
      frmSampleForm.txtMessage.Value = ""
   End Sub
-->
</SCRIPT>

<BODY>
   <CENTER>
   <H1>Enter HTML Elements Using 
   <BR>
   <SPAN CLASS = "BoldAndBlue">
   ALL CAPS
   </SPAN>
   </H1>
   </CENTER>

   <HR>

   <DIV ID="ItemsList" CLASS="CenterThisRed">
      <OL>
         <LI>Item One</LI>
         <LI>Item Two</LI>
         <LI>Item Three</LI>
         <LI>Item Four</LI>
      </OL>
   </DIV>

   <CENTER>
   <FORM NAME="frmSampleForm">
      <DIV ID="divTextBoxLabel" 
         STYLE="font:bold; 
                color:green">
         Enter Text Here:
      </DIV>

      <INPUT TYPE="Text" NAME="txtMessage" SIZE=50>
      <BR>
      <INPUT TYPE="Button" NAME="cmdMessage" VALUE="Display Text">
   </FORM>
   </CENTER>
</BODY>
</HTML>

See Also

Using a Naming Convention | Naming Variables and Constants | Naming Functions and Subroutines | Custom Classes and Objects