Require Variable Declaration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32661

    Require Variable Declaration

    Problem Description :
    In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private; Public; Global; etc) will cause an error, either at compile time or when attempting to execute the procedure it's referred to from within. With this unset, any unregognised references will be treated as a previously unknown and unset variable of type Variant.

    Why This Can be a Problem :
    Whenever there are problems with code, having this option missing means there are more possible problems to find by yourself. Misspelled variable names are a common cause of problems which disappear almost entirely when this option is set. Another frequent problem when running in this mode, due to the very flexible and loosely defined nature of the language, is that you can write code intending to use a built-in feature but, because of a misspelling, it doesn't recognise the code as the intended feature so treats it simply as a previously undeclared variable instead. There are many and varied situations where this problem can bite you unexpectedly.

    As with any problems posted on this site, you won't make many friends if you post code without compiling and testing it first. This goes for Variable Declaration too. If people offer time to help with your problem only to find that you're asking about something the compiler is perfectly capable of catching, they're likely to feel insulted and ill-used.

    Why is it Possible to Run With This Unset :
    Visual Basic for Applications was developed with non-developers definitely in mind. One of the driving forces was to provide a language which was very easy to pick up and get something running in. More professional languages have always (certainly since the 1970s) insisted on variable definition and declaration. At its most basic, VBA can produce a "Hello World" procedure (using a variable) simply as :
    Code:
    Public Sub HWorld()
      Var = "Hello World"
      MsgBox Var
    End Sub
    Variable definitions can be left to the next chapter of the tutorial to be introduced.

    How to Set Variable Declaration :
    1. Go to the VBA Window (Alt-F11 from Access).
    2. Open the Options page (Tools / Options).
    3. Select Require Variable Declaration from the Editor tab.

    When this is set all newly created modules will have the line "Option Explicit" included automatically. If you ever see a module without this line, treat it with mistrust!
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    You wouldn't believe the state of some of the "other people's code" I've been forced to correct when the Option Explicit was missing. How any of those applications functioned in any way at all I don't know.

    Even after all these years (no jokes please) I still make sure Option Explicit is set before I start developing. I would never find some of the errors in my own code otherwise.
    Last edited by NeoPa; May 17 '17, 02:52 PM. Reason: Grammar (rofl) -Guess.

    Comment

    Working...