Microsoft Access VBA Macro Programming

(Tina Sui) #1

Reserved Words


You probably noticed that there are a number of keywords within VBA that make up the
language—for example,For,Next,Do, andLoop. These cannot be used within your
program for the purpose of naming variables, subroutines, or functions because they are
reserved words. This means they are part of the VBA language itself, and it would cause
enormous confusion if you were allowed to go ahead and use these for random purposes
within your own code. Fortunately, VBA checks what you are typing in instantly and puts up
an error message—usually “Expected Identifier,” which means you have used a reserved
word and VBA thinks you are entering it as a program statement. Try entering

Dim Loop as String

Loopis, of course, part of VBA and is used inDoloop statements. It is impossible to enter
this statement. Instantly an error message appears, and the line of code turns red to warn of a
problem. Of course, you can ignore the warning, but as soon as you try to run the code, you
will get an error again. Try entering

Sub ReDim()

You will get an error message, and the code will turn red becauseReDimis a keyword within
VBA.
Strangely enough, youcanuse words from the Access object model. You can call a
subroutine Application or Worksheets, and it will work. However, this is not advised. I have
seen problems of code not exiting cleanly when the application is closed down because it
used words from the object model. It certainly causes confusion within VBA and should be
avoided.
Generally, any VBA keyword or function cannot be used as a variable name, subroutine
name, or function name.

26 Microsoft Access 2010 VBA Macro Programming

Free download pdf