VBA CODING CONVENTIONS

In addition to object naming conventions, LNC proposes several standardized coding conventions for Visual Basic procedures.

Code Comments

There are as many inline Visual Basic code commenting styles as there are Basic coders. Whatever convention you use, the keys are to be terse, yet descriptive and consistent.

LNC suggests placing the following minimum set of comments at the beginning of each procedure:

  • Purpose. Briefly describe the purpose of the procedure.

  • Arguments. List the arguments to a function and how they're to be used.

  • Returns. Describe what the return value of a function signifies.

  • Authors. Name the creator, the date created, the last editor, and the date last edited.

Comments placed on the same line as code should be separated from the code by at least two spaces. Comments placed on their own line should be no longer than 60 characters, so they're displayed in full in Access's default module design view size. Some developers like to keep a change log in procedures as a comment block; if so, I suggest keeping it at the bottom rather than the top because it's less frequently accessed than the code it displaces.

Handling Errors

Every procedure that can fail—which is virtually every procedure with more than a few lines—should have an error trap. Error traps are created by placing this line at the beginning of the procedure, after the header comments but before any other statements:

On Error GoTo procname_Err

The marker procname should be replaced with the full procedure name. The error handler is placed at the bottom of the procedure, denoted with the label procname_Err:. At the end of the error handler, control is returned somewhere in the procedure, usually to a line label name procname_Exit that precedes a block of code immediately above the error handler.

So you can turn off error handling during program debugging, LNC suggests that you place the On Error statement inside a conditional compilation directive, like this:

#If pcccDebug Then
  On Error Goto 0
#Else
  On Error Goto procname_Err
#Endif

Before running an application, you can enable or disable error handling by setting the value of pcccDebug to –1 (true) or 0 (false) in the Conditional Compilation Arguments text box on the General page of the Project Properties dialog in the VBA project.

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

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