What is a Naming Convention?
A Naming Convention is simply a standard rule (could be through the use of prefixes, suffixes, typographical convention (syntax styling) such as CamelCase, …) that you apply when creating names for :
- Objects
- Tables
- Queries
- Forms
- Reports
- Macros
- Modules (See VBA Components)
- Table Fields
- Controls (Forms & Reports)
- VBA Components (Modules, Class Modules) & Variables…
Why Use a Naming Convention?
Simple, to standardize your work and make it easier to follow.
For instance, take a simple example of
Public Sub Demo()
For cars = 0 To 10
Accident = DateAdd("d", cars, Date)
Debug.Print Accident
Next cars
End Sub
what is Accident and cars? Strings, Integer, Long, …?
Now, if you implement a basic naming convention and prefix all your VBA date variables with dt for Dates and i for Interger numbers you get
Public Sub Demo()
For icars = 0 To 10
dtAccident = DateAdd("d", icars, Date)
Debug.Print dtAccident
Next icars
End Sub
and the code becomes much more legible. You don’t even need to look at the procedure’s variable declarations to understand that dtAccident is a Date and icars is a Integer number.
So by using naming conventions you
- Standardize your work
- Simplify reading your code (imagine long procedures where you need to scroll up and down to see variable declarations, by using a naming convention, you no longer need to look at the declaration to know exactly what type of variable you are working with)
- Simplify troubleshooting
- Aid in finding things when they are listed together (imaging a list of database Objects with object prefixes, it becomes very easy to distinguish forms from reports and tables form queries, …)
What are Some of the Existing Naming Conventions?
If you Google the subject of Naming Conventions you will be inundated with all sorts of conventions. In Access, or VBA in general, you will commonly hear of the following ones:
- theAccessWeb’s http://access.mvps.org/access/general/gen0012.htm
- Hungarian notation: https://support.microsoft.com/en-us/help/17…ventions-for-vb
- Reddick VBA (RVBA) Naming Convention: http://www.xoc.net/downloads/rvbanc.pdf
- Leszynski naming convention: https://en.wikipedia.org/wiki/Leszynski_naming_convention
You’ll notice that Hungarian, RVBA and Leszynski are all very similar as they are derivative of the Hungarian.
Let’s Not Get Hung Up on Specifics!
Now, I know some people will be sticklers for one notation or another, but the truth of the matter is that it truly doesn’t make any difference whatsoever! The name of the game here is consistency!
What is important is that you have some standard and you stick with it throughout any given project.
- You can use your own notation. There’s nothing wrong with that.
- You can change notations from project to project (just don’t change notations within a single project). Once you start with one, stick with it, or if you decide to change notations, update it throughout your project.
I agree 100% that naming protocols are indispensable. Within my first year of programming in VBA, way long ago, I had the unpleasant experience of MYPS (Meeting My Past Self) in code. I could not figure out what I’d written, what it did, why I wrote…embarrassing it was.
The important thing is to have one (a naming protocol) and, come hell or high water, stick to it. You will realized huge savings in time, embarrassment, frustration plus your code becomes ever-so-much-more reusable…