Using Visual Basic 6

Previous chapterNext chapterContents


- 5 -
Working with Projects in VisualBasic 6


What Is a Visual Basic Project?


A project isn't the same as an application

A project is the thing you use to create an application, such as a "traditional" program, a dynamic link library, or an ActiveX control. Think of creating a Visual Basic program like baking a cake: you mix ingredients together, bake them, and pull a cake out of the oven. Similarly, you put forms, modules, and controls together, compile them, and get a Visual Basic application.


In Visual Basic, a project is the group of all the files that make up your program. These might include forms, modules (blocks of code not attached to a form), graphics, and ActiveX controls. Other types of files may also be part of a project, but don't worry about that now.

The first thing to keep in mind about a project is that as you create your program, each form, module, graphic, and ActiveX control is saved as an individual file (see Table 5.1).

TABLE 5.1  Common file types in a Visual Basic 6 project

File Type Description
FRM Form
BAS Module
FRX Automatically generated file for every graphic in your project
OCX ActiveX control
CLS Class module
VBP Visual Basic project

Detailed information about all your program's files is stored in the project file. The project file lists all the files your program uses, as well as details such as the project name and how the IDE should be configured while you work on this particular project. Project files have the extension VBP.

Using the Project Explorer

You can keep track of all the files in your project with the Project Explorer, a window found on the right side of the Visual Basic IDE (see Figure 5.1). The Project Explorer provides a method to organize your project's files and allows you to access them individually in form or code view.

FIGURE 5.1 Use the Project Explorer to keep track of files in your project. Note that OCX and FRX files aren't listed in the Project Explorer.

Most of your interaction with the Project Explorer will be with its context menu. When you right-click one of your project's files, you'll see a menu similar to that shown in Figure 5.2.

FIGURE 5.2 Most of the interaction with your project will be through the Project Explorer's context menu.

The context menu allows you to

Creating a New Project

Every time you run Visual Basic, you'll see the New Project dialog (see Figure 5.3). From this window, you can simply select the type of project you want to create and click Open.

FIGURE 5.3 Use the New Project dialog to create a project.

It's also possible to start a new project while Visual Basic is already running.

Starting a new project

1. From the File menu, choose New Project. (You may be prompted to save the current project. If you need to save it but aren't sure how, see the later section "Saving and Naming a Project.")

2. In the New Project dialog, select the type of project you want to create and click OK.

Changing a Project's Properties

Many pieces of information are required to describe a project, including the project name, the version number, and the title that will appear in the title bar of the finished application. Many others can be accessed via the Project Properties dialog (see Figures 5.4 and 5.5).

FIGURE 5.4 You set your project's type, name, and help file on the General page. You can also add a project description.

FIGURE 5.5 On the Make page, you can set your project's version number and version-specific information, as well as the project title and icon.

Access the Project Properties dialog

1. In the Project Explorer, right-click the project file (the first file listed).

2. From the context menu, select ProjectName Properties. The Project Properties dialog appears. (Or, from the Project menu, choose ProjectName Properties.)

Saving and Naming a Project

When you save your project, you actually save each individual part of your project (every form, module, graphic, and so forth) as an individual file, and then save the project file itself. When you save your project for the first time, Visual Basic automatically suggests a name for each file. This filename will be the same as the name of the form (or module, graphic, and so on), with an appropriate extension for the type of file you're saving (refer to Table 5.1). Thus, a module named modStart would be named modStart.bas because modules have the extension .bas.

Save a project

1. From the File menu, choose Save Project.

2. If this is the first time you've saved this project, or if you've added a form or module since the last time you've saved it, the Save File As dialog appears for each unsaved file (see Figure 5.6).

FIGURE 5.6 Visual Basic automatically suggests the filename frmFirst.frm for a form named frmFirst.


Changing filenames

You don't have to use the filename VB suggests, but you should be careful to use a name that allows you to remember what the file does. If you've already named your files with friendly names, following the VB convention will help you avoid confusion when your project contains large numbers of files. You also should always use the filename extension that Visual Basic suggests, to ensure that all your files can easily be found when you open a file from within Visual Basic.


3. Enter a name for the file and click the Save button.

4. The last file to save is the project file. If you've assigned a name to your project with the Project Properties dialog (refer to the section "Changing a Project's Properties"), VB automatically suggests Project_name.vbp as the file name. If you haven't assigned a project name, VB suggests a default name, such as Project1.vbp. When this happens, you should change the default name to something more friendly, such as SaveTest.vbp.

Opening an Existing Project

If you've worked on the file recently, open the File menu. At the bottom of this menu is a list of the files you have most recently worked on. Select the appropriate file to open it. If the desired project doesn't appear in this list, you'll have to look for it.


Opening files when you start Visual Basic

When you first start Visual Basic, the New Project dialog usually appears. You can save yourself some time by selecting a recent or existing project directly from this dialog. If you don't want this dialog to appear each time you start Visual Basic, deselect the check box at the bottom of this window.


Opening an existing project

1. From the File menu, choose Open Project. The Open Project dialog appears (see Figure 5.7).

FIGURE 5.7 Open existing or recent projects with the Open Project dialog.

2. On the Existing page, switch to the folder in which you've saved your project. (If you've recently worked on the project, select your project from the list on the Recent page.)

3. Select the project and click Open.

Adding Files to a Project

Often, your programs will require more than one form or module. For example, if you want to have an About dialog in your program, it will require that you add a form.

You can add new files to your project in two ways: by selecting the file type (such as form or module) that you want to add from the Project menu, or by adding files from the Project Explorer.

Adding new files from the Project Explorer

1. Right-click inside the Project Explorer.

2. From the context menu choose Add, and then select the type of file you want to add.

Whichever method you use, you may be prompted with a dialog that allows you to pick a specific type of form or module to add (see Figure 5.8). Visual Basic provides templates for commonly used files, such as an About dialog, to save you the time and effort of creating it yourself.

FIGURE 5.8 Visual Basic contains many templates for creating commonly used forms.

You can also add existing files to your project, which comes in handy if you want to use a form or module that you previously created for a different project. Adding an existing file is very similar to adding a new file, except that you choose Add File from the Project menu or from the Project Explorer's context menu (see Figure 5.9).

Removing Files from a Project


Why should you remove or delete a file?

If you don't remove an unnecessary file from your project, it will be included in your program after you compile it. Also, when you remove a file from a project, you don't actually delete the file from your hard drive. To delete the file completely, use Windows Explorer.


If there's a file in your project that you don't want (for example, a form you added accidentally or a module that contains a form you no longer need), you can remove it from the project list.

Removing a file from a project

1. In the Project Explorer, right-click the file you want to remove.

2. From the context menu, select Remove filename.

FIGURE 5.9 Add existing files to your project by selecting Add File.

You also can highlight the file in the Project Explorer and choose Remove filename from the Project menu.

Using Project Groups

Visual Basic 6 allows you to work with more than one project at a time. As you might imagine, working with multiple forms and modules that are used in different projects can get pretty confusing. To keep track of everything, you can use the Project Explorer and a project group, which is a collection of projects (see Figure 5.10). Project Groups can be saved as a file, just like a form, module, or project. They have the extension .vbg.

Add a project to a project group

1. Choose Add Project from the File menu.

2. In the Add Project dialog, highlight the type of new project you want to add from the New tab, or select an existing or recent project from the appropriate page of this dialog.

3. Click OK. Visual Basic automatically creates a project group and adds the new project to it, which you can see in the Project Explorer.

FIGURE 5.10 The Project Explorer allows you to track multiple projects at one time by using project groups. The project in boldface type is the project that runs when you press F5.


Project groups don't really become important until you start creating ActiveX controls, which require you to have multiple projects open at the same time. So don't worry about project groups right now. Just keep in mind that it's possible to have more than one project open at the same time.


Previous chapterNext chapterContents

© Copyright, Macmillan Computer Publishing. All rights reserved.