LESSON SUMMARY: How to use the 'VB Editor' and understanding VBA structure.
The VB Editor
The 'VB Editor' is accessed from the 'Visual Basic' Toolbar, and opens in a separate window. You would normally switch between the application window (WORD, EXCEL or ACCESS) and the VBE window using the Windows 'Task bar' or the 'Alt' + 'Tab' key combination.
If the 'Visual Basic' toolbar is not visible in your applictaion, you might need to display it by selecting 'View | Toolbars | Visual Basic'.
The three controls you can see on the 'Visual Basic' Toolbar shown above are ...
Visual Basic Editor [ VB Editor ]
Control Toolbox
Design Mode
You can leave this toolbar to 'float', but it is best to move it into the main toolbar section below the main menu bar.
VISUAL BASIC EDITOR [Description]
The VB Editor is where you enter your VBA Code. You can also enter the VB Editor by double clicking on a control if you are in design mode. [See below]
In this view of the VB Editor, you can see the 'Project Explorer' window on the left with the 'Code' window on the right. There are other windows you can open, such as the 'Property' window and the 'object Browser' window. Use the 'View' menu to select which windows you wish to view.
CONTROL TOOLBOX
This is where you select the type of control to use. It could be ...
a simple 3D Button, called a "Command Button"
a Radio Button
a Picture Button
... or a more complicated control such as a Combo Box.
DESIGN MODE
Use this to switch between being able to edit a button, and being able to use a button.
For example, in edit mode, if you click on an object such as a 'CommandButton', you will be automatically taken to the objects 'click' procedure in the VBE. If you right click on a object in edit mode, a menu will appear where you access its various properties.

If you turn edit mode off, when you click on the object, you will run its 'click' procedure if one has been created.
ERROR HANDLING !
Before you start using the VBE you must know how to handle errors in your VBA code. At this stage, you will most likely have a 'Compiler Error', often refered to as a 'Development Error', where there is a spelling or expression error.

The line of code containing the error will be highlighted in red and you will be given a hint as to the cause of the error in the Error Message dialog box.
If your code has a 'Run-Time Error', the VBA code will be halted, and will not restart until you reset the project using the 'reset' button on the VBA toolbar. You will be given a hint as to what the problem is as well by an error code.

Handling 'run-time' errors will be looked at in 'Lesson 8' and 'debugging' your code will be looked at in detail in 'Lesson 18'.
MY FIRST VBA PROGRAM !
STEP ONE: Start a new word document and save it as 'VBA Basics.doc'.
STEP TWO: Open the control toolbox and select a 'Command Button'. [ It will be pasted into your document ]
STEP THREE: Copy the following lines ...
Dim Msg as String
Msg = "Hello!"
MsgBox Msg
STEP FOUR: Double click on the button to open the 'VB Editor'.
STEP FIVE: Paste the copied text in between the following two lines which will have been created when you double clicked on the control.
( This code will NOT be created if you enter the 'VB Editor' via the Toolbar! ).
Private Sub CommandButton1_Click()
End Sub
STEP SIX: Exit the 'VB Editor', turn off Design Mode , then click on the CommandButton. A message should appear saying "Hello!"
EXPLANATION:

A VBA project will react to a defined 'Event', such as a keypress, a mouse action, or a document 'Event' such as being opened, closed or saved.
The defined 'Event' will start a VBA code procedure, called a sub , which is a 'sub-routine' within a larger program body.
The VBA code within that 'sub' will initiate action(s) on the properties of OFFICE application Objects.
In our example we displayed a dialog box 'Object' using the 'MsgBox' function where we set its 'prompt' property value to "Hello!".
For example ....
A WORD Document has many 'Objects': characters, words, paragraphs, tables, footers, headers etc.
OFFICE applications have many 'Objects': Menus, ToolBars, Controls etc.
All of these 'Objects' have 'properties' that can have actions performed upon them by VBA code.
So, the ' BASIC VBA CONCEPT ' is that ....
anything in a OFFICE application is an 'object' that has 'properties' that can have actions performed on it by VBA code!
EXERCISE
Before you continue, have a good look at the Help contents from within the 'VB Editor'. Look especially at the sections on 'Visual basic User Interface Help' and 'Visual Basic How-To Topics'. Using the 'Help' provided in the 'Visual Basic Editor' is the best way to expand your knowledge of VBA after gaining knowledge of some computer programming concepts. Look at the screen shot below to see how comprehensive the available help is!
|