Microsoft Office 2000/Visual Basic Programmer's Guide   

Naming Objects and Controls

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 what the item's purpose is. 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 an Office application's type library. In HTML, the term objects refers to HTML intrinsic forms and controls, ActiveX controls, Microsoft Scripting Components (scriptlets), and other objects designated by using the <OBJECT> tag.

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 never use 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 won't 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 don't make sense in this context. For more information about creating and using custom objects, see Chapter 9, "Custom Classes and Objects."

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 will often insert a default name for an object or control just as 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 case-insensitive, 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 VBA keywords being highlighted in the Visual Basic Editor. For example, if you examine the HTML code in the following example (ScriptNaming.htm in the ODETools\V9\Samples\OPG\Samples\CH03 subfolder on the Office 2000 Developer CD-ROM), the use of uppercase HTML element names clearly distinguishes them from the other items on the page:

<HTML>
<HEAD>

<TITLE>
   Office Programmer's Guide, Chapter 3: 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>