Learn Access 2003 VBA with The Smart Method
276
www.LearnAccessVBA.com
1: General rules
1-1
Apart from prefixes and spaces exactly the same name is used for the field name, caption (for the
form control that maintains the data), report column header and code variable name. We call this
the “Cradle to the Grave Naming Convention".
Good:
Field name: CustomerFirstName (table name prefix)
Field caption: First Name (space added for clarity)
Report column header: First Name
Form caption: First Name
Code variable:
strFirstName
(data type prefix, no spaces)
Bad:
Field name: CustFName
Field caption: Christian Name
Report column header: Given Name
Form caption: Name
Code variable:
strFNm
Believe it or not the second example is, almost universally, what you will find in the real world of
commercial applications. Badly informed managers often erroneously believe that hacking together
low-quality applications will result in them being completed faster. A very important responsibility
of your job as a development professional is to educate and inform so that this doesn’t happen!
1-2
There must only be one exit in any sub or function.
Code that has multiple exits is confusing to read and causes problems with cleanup code (such as
closing recordsets, de-referencing objects, destroying objects and ensuring that warnings are always
re-enabled).
Good:
Function CodeToName( strCategory as string) as string
Dim strReturn as string
IF strCategory = “MAN" Then
strReturn = “Manchester"
Elseif strCategory = “LHR" Then
strReturn = “London Heathrow"
Else
strReturn = “Unknown"
End If
CodeToName = strReturn
End Function
(Error handling code not shown)
Bad:
Function CodeToName( strCategory as string) as string
IF strCategory = “MAN" Then
CodeToName = “Manchester"
We can trace the footsteps of St. Patrick almost from his cradle to his grave by the names of
places called after him.
E. Cobham Brewer 1810–1897. Dictionary of Phrase and Fable.