VBATrain.com

    VBATrain Home    |    Samples TOC    |    Previous    |    Next    


Key Words: DefTypes -- variable declaration -- variable types

Deftypes and Option Declarations

In a previous lesson, we set a VB Editor option "Require Variable Declaration" (see Visual Basic Editor Setup). This means that we cannot use a variable in our project that has not been declared. We will declare all our variables as either local, procedure-level variables or as public variables. Additional explanation pertaining to variable declaration is provided in the main course (see Declaring Variables).

In a declaration, there are three parts to the declaration (see the figure below):
  • the Dim or Public statement which tells the VB Editor "I am about to declare a variable",
  • the name of the variable (this is just a name you make up), and
  • the type of variable.

declare syntax

Variable declaration can be simplified using DefTypes (also discussed in the main course, see Deftypes). By so doing, we eliminate the third step above. We specify that all variables that start with a particular letter will be of a particular variable type. For example, all variables that start with a letter "s" will be, by our own definition, string type variables. In this way, we can shorten the previous declarations (see following figure).

revised declarations

You probably also noticed two additional options are also included in the declarations section in the figure, Option Compare Text and Option Base 1. Please see the main course for an explanation of these options (Two Useful Options). We will include the DefTypes and the options shown above in our project modules.

Put DefTypes in the modules

  1. Open your workbook (if it is not open) and go to the VB Editor.
  2. Type the lines shown below as the top two lines in one of your modules (it doesn't matter which one). Type the DefType statements, e.g. defstr, all in lower case. If you typed them correctly, they should automatically capitalize when your cursor leaves the line of code you are typing.
  3. type deftypes

  4. After you have the lines entered, check your work by verifying that the DefType keywords automatically capitalized and that the DefType keywords turned blue (blue is the default color for VB Editor keywords).
  5. Select the top two lines of code you just typed, and copy them (Ctrl + C)
  6. Double-click on another code module in the Project Explorer pane to activate that code module window.
  7. pick module

  8. Move the Option Explicit statement down a line, and then select the blank top line in the module.
  9. option explicit moved down

  10. Paste the code you just copied.
  11. In a similar fashion, paste the copied statements to the declarations sections of the other code modules in the project.
  12. Save your work
When you are done, you should have DefType statements at the top of all your modules.

If you don't want to type the code, you can copy it from the text area at the bottom of this page. However, we recommend that you type the code since typing code helps you learn how to write code. If you just copy and paste the code, you do not experience VB Editor help features, nor do you make mistakes. Mistakes are an important part of learning VBA or almost anything else.



    VBATrain Home    |    Samples TOC    |    Previous    |    Next    

Click here for feedback and questions about this chapter.

Copyright © 2006-2008 J. Donald McClenagan, PhD, ALL RIGHTS RESERVED