Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Coding Basics

As you write more powerful programs, you'll need to insert more and more of Visual Basic's programming language into your applications. The language, although one of the easiest to master, can be tricky in places. Nevertheless, if you start with the fundamentals, you'll have no trouble mastering the hard parts.

Remember that a VB program consists of the following:

  • One or more forms
  • Controls on the forms
  • Code written in the Visual Basic programming language

Although you can create great-looking applications just by dragging controls onto forms and setting properties, the applications don't really become useful until you add code to tie the controls together and to perform calculations and data manipulation when needed. No control exists to calculate inventory accounting values; you must add the code to do things like that. The program code that you write is just a detailed set of instructions that tells Visual Basic how to manipulate data, perform input and output (known as I/O), and respond to the user.

I/O stands for input and output and refers to the practice of receiving data from a control, the user, or a data source such as the disk drive and sending data from your computer to the outside world, such as the screen or printer.

Before looking at specifics, you should take a moment to consider the location of the code in a VB application. You now know that much of the code in an application is comprised of small event procedures that respond to events. The form's controls often trigger the events when the user interacts with a control. Event procedures are not the only code that appears in an application, however. Code can appear in several places. This 24-hour tutorial concerns itself with code that appears in form modules and in standard modules.

A form module is a module file that holds one or more forms and the code that goes with each form. A standard module is a file that holds code not related to a form.

A form module is code connected to a specific form. The form's event procedures appear in the form's form module as does other code that isn't directly connected to events, such as calculations and data-sorting routines. Every application has at least one form, so every application contains at least one form module. When you add a new form to an application, Visual Basic adds a new form module to go with the form.

Figure 5.1 helps illustrate the module concepts described in this section. All the application's modules reside in separate disk files, even though Visual Basic groups them together in a single project. The Project Explorer window keeps things together in an orderly manner.

05fig01.gif

Figure 5.1 One or more modules can appear in an application.

A program that supports multiple forms (and therefore, multiple form modules) is either an MDI (multiple-document interface) application or an SDI (single-document interface) application. Until version 5, VB was an SDI application. An MDI application, such as Word, can open several windows at once that contain different data documents. An SDI application, although it can contain multiple forms such as dialog boxes, supports only one data document. (The Windows Notepad application is an SDI application because when you open a new document, the current one leaves the work area.) No matter which kind of application you create, your application can contain multiple Form windows and, hence, can contain multiple form modules.

In addition to form modules, an application might contain one or more standard modules. Standard modules contain code and have no forms or controls associated with them. Although the code inside a standard module might manipulate a form or its controls, the code that you put in a standard module usually contains general-purpose code that you can use in several applications. For example, you might write some Visual Basic code that calculates wages using some special formulas that your company requires. If you need to use those calculations in several applications, store the calculations in a standard module and then add a copy of that standard module to each application instead of typing the code multiple times in multiple applications.

Fortunately, you don't have to do much to manage projects that require multiple files; the Project Explorer window keeps track of things. As you add files to or remove files from the application (by selecting from the menu that appears when you right-click over the Project Explorer window), the Project Explorer window keeps track of the bookkeeping. When you want to modify or add to one of the items in the Project Explorer window, double-click the object's icon in the Project Explorer window, and the form or code opens in the work area (see Figure 5.2).

05fig02.gif

Figure 5.2 The Project Explorer window displays the project files.

+ Share This