AfraLISP - Learn AutoLISP for AutoCAD productivity

An Introduction to VB and VBA

by Kenny Ramage

Visual Basic is a descendant of the BASIC programming language first developed in the 1960's. BASIC (which is short for "Beginner's All-purpose Symbolic Instruction Code") is probably well known to most computer programmers.

Microsoft Visual Basic allowed programmers to create stand-alone Windows applications which previously required the use of complex C programming languages. Visual Basic for Applications is a subset of Visual Basic, designed specifically for certain applications such as MS Word, Excel, Access, etc.

With the release of AutoCAD R14, AutoCAD has included VBA as part of the AutoCAD package. VBA for AutoCAD includes most of the functions and controls available in the stand-alone version of Visual Basic alongwith specific functions and controls specifically designed for AutoCAD.

If Visual Basic is the first experience you, as a programmer, have had with "event driven" programming, then read on and enter the world of Visual Basic…

Event Driven Applications

An event is an action recognized by a form or control. Event driven applications execute Visual Basic code in response to an event. Each form and control in VBA has a predefined set of events. If one of these events occurs, VBA invokes the code in the associated event procedure.

Although objects in VBA automatically recognize a predefined set of events, you determine if and how they respond to a particular event. When you want a control to respond to an event, you write code called an event procedure for that event.

Many objects recognize the same event, although different objects can execute different event procedures when the event occurs. For example, if a user clicks a userform, the Userform_Click event procedure executes; if a user clicks a command button named CommandButton1, the CommandButton1_Click event procedure executes.

Here's what happens in a typical event driven application:

  1. The application starts and a form or control receives an event. The event can be caused by the user (for example a keystroke), or by the system (for example a timer), or indirectly by your code (for example, a Load event procedure when your code loads a form.
  2. If there is an event procedure corresponding to that event, it executes.
  3. The application waits for the next event.

Note : Many events occur in conjunction with other events. For example, when the DblClick event occurs, the MouseDown, MouseUp and Click events also occur.

Event Driven vs. Traditional Programming

In a traditional or 'procedural' application, the application itself rather than an event controls the portions of code that execute. Execution starts with the first line of executable code and follows a predefined path through the application, calling procedures as needed.

In event driven programmes, a user action or system event executes an event procedure. Thus, the order in which your code executes depends on which events occur, which in turn depends on what the user does. This is the essence of graphical user interfaces and event driven programming: The user is in charge, and your code responds.

Because you can't predict what the user will do, your code must make a few assumptions about 'the state of the world' when it executes. When you must make assumptions (for example, that a text box has text in it before a command button is pressed), you should try to structure your application so those assumptions are always valid (for example, disabling the command button and enabling it only in the Change event procedure for the text box).

Your code might trigger additional events as it performs additional operations. For example, loading a Userform causes the Userforms Load event to occur, and changing the Text property of a text box causes the text box's Change event to occur.


Each object in VBA is defined by a "class". To understand the relationship between an object and it's class, think of cookie cutters and cookies. The cookie cutter is the class. It defines the characteristics of each cookie - for instance, size and shape. The class is used to create objects. The objects are the cookies.

Two examples of the relationship between classes and objects in VBA may make this clearer.

  • The controls on the Toolbox in VBA represent classes. The object known as a control doesn't exist until you draw it on a Userform. When you create a control, you're creating a copy or "instance" of the control class. That instance of the class is the object you refer to in your application.
  • The Userform you work with at design time is a class. At runtime, VBA creates an instance of the Userforms class.

All objects are created as identical copies of their class. Once they exist as individual objects, their properties can be changed. For example, if you draw three command buttons on a Userform, each command button object is an instance of the CommandButton class. Each object shares a common set of characteristics and capabilities (properties, methods and events), defined by the class. However, each has it's own name, can be separately enabled and disabled, can be placed in a different location on the form, and so on.

Working with Objects

Visual Basic objects support properties, methods, and events. In Visual Basic, an object's data (settings or attributes) are called "propertie"s, while the various procedures that can operate on an object are called it's "methods". An "event" is an action recognized by an object, such as clicking a mouse or pressing a key, and you can write code to respond to that event.

You can change an object's characteristics by changing it's properties. Consider a radio: One property of a radio is its volume. In Visual Basic, you might say that a radio has a 'Volume' property that you can adjust by changing its value. Assume you can set the volume of a radio from 0 to 10. If you could control a radio with Visual Basic, you might write code in a procedure that changes the value of the 'Volume' property from 3 to 5 to make it play louder :

Radio.Volume = 5

In addition to properties, objects have methods. Methods are part of objects just as properties are. Generally, methods are actions you want to perform, while properties are the attributes you set or retrieve. For example, you dial a telephone to make a call. You might say that telephones have a "Dial" method, and you could use this syntax to dial a seven digit number 3334444:

Phone.Dial 3334444

Objects also have events. Events are triggered when some aspect of the object is changed. For example, a radio might have a "VolumeChange" event. A telephone might have a "Ring" event.

As it would be an impossibility for me to try and explain to you all the characteristics of VB, I strongly urge you to invest in a good VB reference book. There are many on the market and it would make your life a whole lot easier.