Difference between revisions of "Microsoft Access VBA Naming Conventions"
Line 70: | Line 70: | ||
|+ align="bottom" | Tag: Access Object | |+ align="bottom" | Tag: Access Object | ||
! style="text-align:left;" | Data Type !! style="text-align:left;" | Tag | ! style="text-align:left;" | Data Type !! style="text-align:left;" | Tag | ||
− | |||
− | |||
|- | |- | ||
| Table || tbl | | Table || tbl | ||
Line 88: | Line 86: | ||
|- | |- | ||
| Class || cls | | Class || cls | ||
− | | | + | |} |
− | | Related Objects | + | |
+ | {| class="wikitable" | ||
+ | |+ align="bottom" | Tag: Related Objects | ||
+ | ! style="text-align:left;" | Data Type !! style="text-align:left;" | Tag | ||
|- | |- | ||
| Field || fld | | Field || fld | ||
Line 113: | Line 114: | ||
| Tab Control || tab | | Tab Control || tab | ||
|- | |- | ||
− | | Tab Control Page || tpg | + | | Tab Control Page || tpg, pag |
|- | |- | ||
| Frame || fra | | Frame || fra | ||
Line 128: | Line 129: | ||
|} | |} | ||
− | + | The following tags differ slightly from tags proposed by Reddick and Leszynski | |
− | + | and should be explained: | |
− | + | ||
− | + | ; cmb vs cbo | |
+ | : The control type name is "Combo Box". If you apply the tag rules you will end up | ||
+ | : with cmb. There is no good reason to call this control type tag "cbo". | ||
+ | |||
+ | ; btn vs cmd | ||
+ | : The nature of a "Command Button" is not the command, but the button. The tag | ||
+ | : "cmd" is also well known for the Windows Shell as well as for the extension | ||
+ | : of batch files. Although both usually do not appear in Visual Basic code very | ||
+ | : often it is still a bad habit to "abuse" acronyms. | ||
+ | |||
+ | ; tpg/pag vs pge | ||
+ | : The tag "pge" is less phonetic than "pag". A good tag would be "pg", but as we | ||
+ | : are trying to stick with three-letter tags, we need to find something suitable. | ||
+ | : Pages do not exist without their container, the "Tab Control", thus it is okay | ||
+ | : to see this control as "Tab Page" ending up with the tag "tpg". | ||
+ | ; tgl vs btn | ||
+ | : You may decide on your own whether you should use a different prefix for a toggle | ||
+ | : button. In the end it is still a button although it rather behaves like a check box. | ||
=== Data Type === | === Data Type === |
Revision as of 15:42, 1 April 2011
General
As novice developer you usually define names for constants, variables and methods as they occur; you look at a problem, think of a function, add some parameters and that's it.
After a few years of coding—especially once you are forced to refactor some of your legacy coding—you will realize that the code is hard to read; it somehow feels "inconsistent".
Also during normal development you will notice that you keep stumpling upon the same issues again and again: you're trying to access variables, which are not accessible, you try to use a variable for a text value and run in to an error as it has been defined for numbers: the list of little annoyances which are implied when not sticking to consistent conventions is long—add your favourite annoyance here.
The naming conventions depend on the language you are using, but for VBA it is common conviction to use either the Reddick VBA Naming Conventions or the Leszynski VBA Naming Conventions, which are both versions of the Hungarian notation.
The main idea is to prefix everything with a tag which indicates the type of the variable.
Name
All names will be in singular form. A table, for example, represents many uniformed records. Do we want to talk about many records or do we talk about the entity that table has been designed for? It is the entity.
Let's look at it with a semantic focus: We have a table "Books" which has a field "ISBN". When we are talking about "Books.ISBN", do we mean all ISBNs of all Books? Or the ISBN of a certain Book? It is usually the latter. It also makes SQL statements and code much more readable besides the fact that mixing plural ("Books") and singular ("ISBN") does not make sense. Example:
SELECT Book.Isbn
FROM Book
INNER JOIN Stock
ON Book.Isbn = Stock.BookIsbn
Another advantage of generally applying singular names is the option to denote collections and arrays with a plural form, which leads to easily understandable code like
For Each varBook In varBooks
varBook.Isbn = "unknown"
Next varBook
Tag
A tag is a usually three letter long lowercase mnemonic for the variable type. As there is not a tag for every kind of variable, you sometimes need to "invent" a tag of your own. The usual steps for finding an appropriate tag are:
- Take the name of the object type
- Remove all vowels from the name; you may keep the first vowel if necessary
- Take the first three letters
As soon as you found a tag you should consider the following questions too:
- Is the tag already taken by another object type?
- Does it phonetically sound like the object type?
If you should answer one of these questions with "yes", you should adjust the tag name accordingly.
Access Object
Data Type | Tag |
---|---|
Table | tbl |
Query | qry |
Form | frm |
Sub Form | sfr |
Report | rpt |
Sub Report | srp |
Module | mod |
Class | cls |
Data Type | Tag |
---|---|
Field | fld |
Control | ctl |
Label | lbl |
Text Box | txt |
Combo Box | cmb |
List Box | lst |
Check Box | chk |
Button | btn |
Option Group | grp |
Option Button | opt |
Tab Control | tab |
Tab Control Page | tpg, pag |
Frame | fra |
Frame bound | frb |
Frame unbound | fru |
Image | img |
Line | lin |
Page Break | brk |
The following tags differ slightly from tags proposed by Reddick and Leszynski and should be explained:
- cmb vs cbo
- The control type name is "Combo Box". If you apply the tag rules you will end up
- with cmb. There is no good reason to call this control type tag "cbo".
- btn vs cmd
- The nature of a "Command Button" is not the command, but the button. The tag
- "cmd" is also well known for the Windows Shell as well as for the extension
- of batch files. Although both usually do not appear in Visual Basic code very
- often it is still a bad habit to "abuse" acronyms.
- tpg/pag vs pge
- The tag "pge" is less phonetic than "pag". A good tag would be "pg", but as we
- are trying to stick with three-letter tags, we need to find something suitable.
- Pages do not exist without their container, the "Tab Control", thus it is okay
- to see this control as "Tab Page" ending up with the tag "tpg".
- tgl vs btn
- You may decide on your own whether you should use a different prefix for a toggle
- button. In the end it is still a button although it rather behaves like a check box.
Data Type
Data Type | Tag |
---|---|
Boolean | bln |
Integer | int |
Long | lng |
Single | sng |
Double | dbl |
Currency | cur |
String | str |
Date | dat |
Variant | var |
Array | arr, var |
Type | typ |
Object | obj |