Visual Basic

Using the Me statement

The Me statement should be used whenever possible in the code. Where code in an object module (class or form) refers to objects in the same module, use Me rather than the object's name to make the reference implicit, and be assured the code will work with multiple instances of the object.

NOTE
It is not possible to use Me when setting an instance of a class or form object to Nothing.

Optional parameters

Visual Basic's typed optional parameters are faster than untyped (Variant) optional parameters. However, note that IsMissing does not work with typed optional parameters, only with optional Variant parameters.

In-Line Documentation

One of the best sources of documentation is the code itself. When you follow a specific coding format and thoroughly comment your code, you're making your program more readable and providing valuable information to yourself and to other programmers who might have to make modifications to the code later.

Formatting

Follow these rules when formatting your code:

  • Punctuate your code with blank lines.

  • Keep your procedures to a manageable size-preferably not much more than a single screen.

  • Keep indentation under control. If you're having difficulty remaining within the right margin, you're probably nesting too deeply.

  • Use parentheses to enhance the readability of your code. No one reading your code should have to wonder what you meant when you wrote it.

  • Preserve screen real estate wherever possible. For example, when creating a long string, it might be better to build up the string with multiple assignments:

      Dim sMsg As String
      sMsg = "This is a paragraph that is to appear "
      sMsg = sMsg & "in a message box. The text is "
      sMsg = sMsg & "broken into several lines of code "
      sMsg = sMsg & "in the source code, making it easier "
      sMsg = sMsg & "for the programmer to read and debug. "
      MsgBox sMsg, vbOkOnly, "Text"
      Dim sQRY As String
      sQRY = "SELECT Customer.CustomerName, Orders.OrderValue "
      sQRY = sQRY & "FROM Customer, Orders "
      sQRY = sQRY & "WHERE Customer.CustomerId = Orders.CustomerId"
      ReportQry1.SQL = sQRY