well create a simple User form to introduce the different parts of the “BA IDE and some VB concepts. Select insert > User Form, or click the Insert user Form button on the tool bar. A new User Form window opens in your work space. along with a VB toolbar called the Toolbox, which contains form controls.
A control is an object, such as a button or a text field, that you add to a form and that can modify the form’s behavior. For example, the OK buttons that show up in most dialog boxes are controls.
Attached to (or,”behind”) each control, you create code that makes the control do what you wish. If the control you placed on your form was a Command Button control, you could, for example, program the button to open a Save dialog box to automatically let you choose a folder and save the current document. Or, you could have the button print the current view, close the application window, or any other task within VB’s universe of programmability. The Command Button is just a button to which you can attach code.
An event is a user- or system-generated action, such as the user clicking on a button or a CD-ROM activating the AutoPlay software. Visual Basic is called an event-driven programming language because it responds to events. You place control objects on your forms and then program them to activate in response to specific events. In the case of a command button, you would program it to respond to a Click event: the event that happens when a user mouses over to and clicks the button.
Each control on a form has its own capabilities and can be coded to operate independently of everything else on the for~. As an example, we’ll look at the OK button that you find in almost every dialog box. To create an OK button in VByou would simply click the Command button tool in the Toolbox, drag the cursor to the form, and drop. This procedure creates a button called Command Button, as shown in Figure 24.2. Note that the Project Explorer window is now to your new User Form, and the Properties window is displaying properties for the Command Button you haw just added
Understanding Visual Basic Code
You can think of writing code as writing a list of instructions for the computer to use. In the IDE, we would begin creating the instructions by double-clicking the Command- Button control to open the Code Editor, which automatically adds the following code (which, in the world of programmers, is called a snippet or a stub):
The IDE assumes that you want to add code to the control you just double-clicked, which was automatically named Command Button 1 when it was placed on the form. This snippet is a procedural framework, a structure that defines the beginning and the end of a Visual Basic procedure
Sub is short for subroutine. The terms subroutine and function both refer to types of procedures in VB. Independent blocks of VB code are called procedures. There are three kinds of procedures in VBA functions, subroutines, and property procedures. You probably already know what a function is from your work with Excel functions (such as AVERAGE and SUM)or Project calculated fields: a function is code that returns a value as the result of one or more calculations, comparisons, or other operations. For example, when you SUM a column of numbers in Excel, the result (a total) is returned to the cell containing the SU1 formula. A subroutine is code that performs an operation, but doesn’t return a value. For example, a subroutine can open or close a form or report, launch an
application, or write values in a table.
To get the command button to close the window when it is clicked, we just need to one command between the.,Private Sub and End Sub lines: Unload Me.The whole completed procedure looks like this
Now that we’Ve added a command in language that VB can understand, it will perform the action we ask of it. Clicking the small blue arrow button on the toolbar that looks like the Play button of a CD or DVD player (or pressing FS) causes the form to run. This is the same as selecting Run> Run Sub/User Forms. If you click the Command button on the form, the form disappears .
Making Use of Visual Basic
In the previous section, we took a quick look at what VB is and the parts of the VBA IDE. Now, we’re going to examine what VB can be used for. To begin, Jet’s further examine what VB can do as a language.
VB is a complete and powerful event-driven language, which means that each component, like our example button, can operate independently of the rest of the program, hen an event occurs, VB responds by running the procedure that was defined for that specific event, if any, If no procedure was defined for the event, nothing happens, These event procedures are tied to the controls that you place on your forms for user interaction or to use Project 2000 or Windows features: for example, the Common Dialogs control adds the capability to use system-based Open and Save dialog boxes with only a few lines of code. .
In our example in the previous section, we told Command Button 1 to execute the code related to it when it was clicked. The click was the event and the code executed is the event procedure.
Generally, VB is considered a Rapid Application Development (RAD)tool, with which custom solutions can be photo typed, debugged, and built in a much shorter period of time than with other languages. One of the more common reasons VB is deployed is to provide fast development of database front end applications. Here are three uses that specifically relate to Project
• You can automate forms and data handling, create new data, or access existing data in a project file from another application.
• You can launch and use other Office 2000 applications from within Project.
• You can customize Project to better meet your needs by creating customized forms and personalized applications.
Introducing The Visual Basic IDE
Project 2000 macros are.stored in Visual Basic modules. So, the macros you’ve already created with the Macro Recorder are written in VB.To examine or edit a macro, choose Tools> Macros to open the Macros dialog box, select the macro you want to examine, and then click the Edit ton to open the Visual Basic Integrated Development Environment (IDE), also referred to as the VB Editor: To go directly to the VB Editor from Project, choose Tools> Macro> Visual Basic.Editor
In Figure 24.4, the Project Explorer and Properties window are open to the left of the Code window, The Project Explorer shows all the open VBA projects. Every document can have one project associated with it the project is stored with the document. The project in Figure 24.4 has three types of project components: project objects, forms and modules. A module is a container for VBcode. Each macro you create with the Macro Recorder is placed in a new module, so this project contains three macros created with the recorder. Projects can include two other types of project components: classes and references to templates or other documents.
frmFormatDurati on. cboourunt t identifies the object within its parent object: cbo DurUnit is a combination box object on the form frmFormatDuration. SetFocus is a method of the object. Methods are actions for objects. Methods are discussed later in this
When the VB interpreter hits the End Sub line, it’s finished running the macro. It returns focus (control) to the host application, Project 2000