CREATING VBA OBJECT BASE NAMES

When creating Visual Basic object base names, remember that the base name must be descriptive even in the absence of its tag. For many programmers, the syntax Dim I As Integer for a loop variable is quite acceptable. Within LNC, however, the variable named I would become iintLoop. Single-character variable names—especially without tags—aren't allowed. Instead, create a list of short and standardized work variables to handle common daily needs. Table D.10 suggests the LNC approach to commonly used variables.

Table D.10. Standardized LNC Work Variables
Variable Description
blnRet Captures a true/false return value from a function call
cccDebug Creates a conditional compilation constant for turning conditional debugging on and off
dbsCur Sets a database object variable for the current Access database (CurrentDB)
intErr Preserves the value of Err
iintLoop Provides a counter for For...Next loops
intMsg Captures a return value from a MsgBox() function call
intResult Holds the result of math operations (also dblResult, lngResult, and so on)
intRet Captures a numeric return value from a function call (also dblRet, lngRet, and so on)
intWork Used for any temporary work (also dblWork, lngWork, and so on)
strMsg Used to build long message box strings
strOrder Used to build long SQL ORDER BY strings
strSQL Used to build long SQL strings
strWhere Used to build long SQL WHERE clauses

Rules for VBA Object Base Names

Creating Visual Basic object base names involves following the same rules listed earlier in the section “Creating Database Object Base Names.” Briefly, those rules are as follows (refer to the earlier section for full descriptions):

  • Spaces aren't allowed in any object name.

  • Object names begin with a letter and should include only letters, digits, and underscore characters.

  • Object names use mixed upper- and lowercase to add readability to the name.

  • The only non-abbreviated syntax element that can have multiple capital letters is the base name.

  • Object names are usually singular rather than plural.

  • An object's base name should include the base names of any objects it's built on, if practical.

Note

The last rule is an expanded version of the corresponding rule for database objects, which stated that table base names should propagate into names of dependent objects. In Visual Basic, that rule expands to require a reference in variable names to objects of any type that they relate to. For example, a RecordSet variable created on tblCust should be named rstCust. Also, if a string array variable of part numbers, astrPartNum, had an Integer index variable, it should include the array's base name in its own: iaintPartNum.


VBA Object Base Name Lengths

There's no LNC rule limiting variable name length, but common sense dictates that variable names longer than 15 or 20 characters waste a lot of keystrokes at each use. For procedure names, the VBA module editor by default shows just more than 20 characters of a procedure name, so this number is suggested as the target maximum procedure name length.

Abbreviate VBA object base name elements wherever possible by using a standardized abbreviation table such as the one explained later in the section “Standardized Abbreviations.” You can extend LNC by creating your own standard abbreviations as well. You should create and use standardized terminology in your applications wherever possible; for examples, see the later section “Standardized Terminology.”

Compound VBA Object Base Names

Procedure base names follow the construction ObjectVerb, where the Object portion describes the primary object type affected (often the same as the primary argument), and Verb describes the action. This style sorts functions and subs by their target object when shown in ordered lists, as follows:

FormCtlHide
FormCtlShow
FormPropAdd
FormPropGet
FormPropSet

We find this sort order much more appealing than the commonly used alternative with VerbObject construction:

AddFormProp
GetFormProp
HideFormCtl
SetFormProp
ShowFormCtl
						

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.208.117