Previous Page TOC Next Page


1 — Visual Basic 4.0: New and Improved

by Greg Perry

With the introduction of Visual Basic 4.0, Microsoft set another milestone. Not only did Micro-soft create Visual Basic 4.0 to take advantage of both Windows 95's and Windows NT's 32-bit operating systems, to add development and support tools such as a code profiler and an improved image editor and resource compiler, and to support improved data access and OLE, Microsoft improved virtually every feature already found in previous versions of Visual Basic. Visual Basic 4.0 is now the development system of choice for Windows 95 and Windows NT applications.

This chapter introduces you to Visual Basic 4.0 by exploring some of its new features and features from earlier editions that have been revised in the new version. Although this chapter is not a tutorial of those new and improved features, it does present an introductory overview so that you will have a good feel for the capabilities found in Visual Basic 4.0. This chapter presents you with the big picture—a road map—of your future work with Visual Basic 4.0 so that you can feel comfortable with the new version's feature set and begin to tackle the specifics found in the rest of the book.


Note

Despite the plethora of new features in Visual Basic 4.0, all programs you've created using earlier versions of Visual Basic run without any problems under Visual Basic 4.0. In addition, programs you've written using Visual Basic for Applications version 1.0 (introduced in Microsoft Excel 5.0 and Microsoft Project 4.0) run under Visual Basic 4.0.

Visual Basic 4.0 comes in the following three editions:

The Standard Edition's New Features

The following pages present an overview of all three versions of Visual Basic 4.0. No matter which edition you use, you will spend most of your development time using the tools in the Standard Edition (and therefore in all three editions). Most of this chapter is devoted to the new and improved features of the Standard Edition because of that edition's importance for the Visual Basic 4.0 programming community.

The Interface

The Visual Basic 4.0 interface looks and acts much like previous versions of Visual Basic. You do not face a huge learning curve when moving from an earlier version, such as 3.0, to Visual Basic 4.0. The Visual Basic 4.0 interface contains the standard Windows 95 interface elements including support for long filenames in Open dialog boxes and the Windows 95 window control buttons. If you select File | Open to open a new Visual Basic 4.0 project, you see the 32-bit Windows 95 Open dialog box shown in Figure 1.1.


Figure 1.1. Visual Basic 4.0 supports Windows 95 dialog boxes.

Of course, the file-related controls in Visual Basic 4.0 (such as the directory list box and the file list box controls) all support long filenames and the Windows 95 Explorer-like interface. In addition, when you use the common dialog box control for file-related support in your programs, the common dialog box control also supports the extended Windows 95 file interface.

Windows 95 supports extensive use of the right mouse button. Although some Windows 3.1 applications (such as Borland's Quattro Pro) have supported the right mouse button for several years, the only Microsoft products to use the right mouse button in a uniform fashion are those written for Windows 95. Visual Basic 4.0 is no exception. Click the right mouse button over a form, control, or property box and Visual Basic 4.0 displays a context-sensitive menu listing common tasks you may want to do with that item.

Microsoft expanded Visual Basic 4.0's online help system to support the full Windows 95-like help engine. When you request help, Visual Basic 4.0 displays a tabbed property sheet such as the one shown in Figure 1.2. The property sheet provides a Windows 95-like contents list, an index to common features, and an extensive find feature that searches across all topics for a given topic.


Caution

The first time you select the Find tab of the Help property sheet, Visual Basic 4.0 runs the Find Setup Wizard that lets you build the help database of find topics. If you've already had a chance to use Windows 95, you've probably been requested to build such a database in other applications. If not, you have to tell the Find Setup Wizard how you want the database built. If you have ample room on your hard disk (three or more megabytes is sufficient), select the option to maximize search capabilities. By maximizing the help system's find database, you ensure that you get the most complete help searches. If you minimize or customize the find database, you save some disk space but lose some details in the searching capabilities.


Figure 1.2. Visual Basic 4.0 provides full Windows 95 help support.

When placing controls on a form, you can now nudge controls to size or place them one pixel or grid unit at a time. Ctrl+arrow key moves a selected control one grid or pixel unit (if no grid is showing); Shift+arrow key resizes a selected control one grid or pixel unit.

If you click the Lock Controls toolbar button (the button with the padlock icon), Visual Basic 4.0 prevents you from moving controls elsewhere during form design. As long as the controls are locked, both current and subsequent controls placed on the form stay put; you cannot move those controls with the mouse by dragging them elsewhere. You can move the controls only by unlocking them, changing placement property location values inside the Property window, or by changing the property location values inside Visual Basic 4.0 code when you run the program.


Caution

All controls are locked or unlocked at the same time. You cannot lock individual controls on a form. The sizing handles on locked controls change color from black to white to indicate that you cannot move the controls. If you unlock them, the color reverts back to black.

Code-Related Support

Although little more has changed in the Visual Basic 4.0 interface, Visual Basic 4.0 has added a feature that programmers have been requesting since Visual Basic 1.0: a line-continuation character for the Visual Basic 4.0 text editor. When entering long lines into the editor in earlier versions, you had to keep typing the lines across the editor window no matter how long the lines got. Some Visual Basic expressions can get tediously long—but before Visual Basic 4.0, there was no way to break long lines into separate lines inside the editor.


Note

When you break a long Visual Basic 4.0 statement into two or more separate lines inside the text editor, that single logical line spans several physical lines in the editing window.

In pre-Visual Basic 4.0 releases, the following expression spanned a single long logical line and also a single physical line:

HisAnswer = "Joseph said to Carol, " + Chr$(34) + "I do!" + Chr$(34) + " Then... he kissed her."

The line-continuation character is actually an underscore (_). You must always precede the underscore with a space; therefore, the line-continuation character actually requires that you type two characters (including the space). Visual Basic 4.0 considers the following two physical lines identical to the preceding long line:

HisAnswer = "Joseph said to Carol, " + Chr$(34) + "I do!" _

            + Chr$(34) + " Then... he kissed her."

This is still one logical line of code but the long physical line now spans two physical lines. Notice how these two lines eliminate the single long physical line; inside the editing window, these shorter lines make editing and debugging easier because you have to scroll the editing window left and right less often. You can continue across as many physical lines as needed to complete long logical lines of code.


Caution

Do not try to continue a line in the middle of a string literal using the underscore. If you do, Visual Basic 4.0 thinks that the underscore is part of the string literal and you get an error when Visual Basic 4.0 attempts to interpret the second physical line.

As you enter your Visual Basic 4.0 code, you see that the editor's scroll bars become proportional to display the relative amount of scrolling you have done. These proportional scroll bars are further enhancements to Visual Basic 4.0 and support a complete Windows 95 environment.

The Visual Basic 4.0 language is fully compatible with Visual Basic for Applications. (Earlier versions of Visual Basic were almost compatible, a fact that often caused confusion for those who programmed in both Visual Basic for Applications and Visual Basic!) To help maintain compatibility between the two similar languages, Visual Basic 4.0 supports several new language statements.

Property procedures are new kinds of procedures you can write to execute when other parts of the code access, change, or use the Set statement to modify a control property in some way. Here is the format of a property procedure:

[Public|Private][Static]Property {Get|Let|Set} PropertyName [(Arguments)][As Type]

  One or more Visual Basic 4.0 statements

End Property

If you want specific code executed when a property is set, use property procedures instead of public variables. The procedures execute automatically just as other event procedures are executed; your programming overhead is decreased.


Tip

If you want one or more properties to be read-only throughout a program's execution, you can use the Property Get property procedure without any corresponding Property Let or Property Set procedure. Property Get returns the value of a control's property; Property Let changes the property value and Property Set changes the property using the Set-like mechanism.

Three new language features take advantage of collection objects you can create. A collection is actually a new data type added to Visual Basic 4.0 to augment integers, strings, and the other data types in the language. (The keyword Collection defines occurrences of collection data types in statements such as Dim and ReDim.)

A collection object lets you create a group of Visual Basic 4.0 objects, such as databases or directories, and then enumerate over that collection. For example, suppose that an application works with 25 database objects. You can store those database objects in a collection, which acts like an array just for those objects. The new command For Each...Next lets you step through (or enumerate) each object within the collection. Here is the format of the For Each...Next command:

For Each Element In ObjCollection

  One or more Visual Basic 4.0 statements

Next [Element]

The For Each statement cannot work with user-defined data types because Visual Basic 4.0 must be able to enumerate over variant data types; user-defined data types can never be variant.


Tip

Using OLE automation, your Visual Basic 4.0 application can easily manipulate objects from other applications such as Microsoft Excel or Microsoft Project. Visual Basic 4.0's new and improved OLE features are discussed later in this chapter and throughout this book. Part III of this book, entitled "Unleashing Component-Based Programming," explores OLE 2.0 in more detail.

Visual Basic 4.0 simplifies your work with objects not only by letting you enumerate over objects using the For Each statement. You can also use the With statement to simplify your work with both objects and user-defined data types; the format of the With statement is shown here:

With Object

  One or more Visual Basic 4.0 statements

End With

You could set five form properties using a qualified form name like the following:

frmTitle.DragMode = 0

frmTitle.Enabled = False

frmTitle.Height = 575

frmTitle.TabIndex = 3

frmTitle.Width = 1100

Alternatively, you can eliminate the typing of the form name by using With:

With frmTitle

   .DragMode = 0

   .Enabled = False

   .Height = 575

   .TabIndex = 3

   .Width = 1100

End With

As do many of Visual Basic 4.0's enhancements, the With statement simplifies your programming requirements and decreases development time. If you have to modify five or more property values for a control, the With statement is easier to use than having to qualify the object name at each assignment.

Although Visual Basic 4.0 is not a true object-oriented programming (OOP) language, you can see that each version has come closer to working with true objects. Because of the support Visual Basic 4.0 gives to objectlike programming, the new Object Browser window, shown in Figure 1.3, lets you scan through all classes, methods, and properties and jump directly to occurrences of such objects in code. If you want additional information on any of the objects being displayed, you can jump directly to those specific help topics for more documentation and description.


Figure 1.3. The hierarchical Object Browser window lets you jump directly to an object in code.

Forms themselves can now act as objects. For the first time, forms contain public properties and methods. You can call these form properties and methods from outside the form. In earlier versions of Visual Basic, forms did not support public methods. Suppose that a form named frmIntro contained a method you wrote named SetUpFields(). Any other module can contain the following statement to call that form's method:

frmIntro.SetUpFields

The way you add new properties to forms is to declare public variables inside the form's module:

Public DivCode As String

Any subsequent reference from any module to frmIntro.DivCode—whether on the left or right side of an assignment-like statement (called an lvalue or rvalue in C terminology)—accesses the frmIntro's new property.

Better OLE Support

Windows 95 is an operating system built on a solid OLE foundation. Table 1.1 lists many of the numerous OLE-related enhancements featured in Visual Basic 4.0. Not only do the applications you create have more OLE capabilities but the Visual Basic 4.0 environment itself supports OLE and advanced drag and drop capabilities to make application development easier than ever:

Feature


Description


OLE custom controls

Visual Basic 4.0 supports both the 16-bit VBX custom controls found in earlier versions of Visual Basic as well as 16-bit OLE controls. The 32-bit version of Visual Basic 4.0 supports OLE custom controls exclusively. An OLE custom control uses the filename extension OCX and always comes with a related OCA file that describes the control and resides in the same directory.

Automatic replacement of VBX controls with OLE controls

If you choose, you can request that Visual Basic 4.0 automatically replace all VBX custom controls with OLE custom controls so that you gain compatibility between 16-bit and 32-bit versions; this replacement also gives other applications (such as Microsoft Access and Microsoft FoxPro) access to the controls.

Error objects

When Visual Basic 4.0 encounters errors from OLE servers, you are better informed about the extent and specifics of those errors because of the more advanced error objects supported.

Visual Basic for Applications OLE support

You can easily program objects from other applications that support the Visual Basic for Applications (VBA) language from within your Visual Basic 4.0 application. Visual Basic 4.0 objects are also available outside the Visual Basic 4.0 development system from VBA-supported programs.

IDE extensibility

You can extend the functionality of Visual Basic 4.0's interactive development environment (the IDE) through the OLE automation interface. (You can also use the Add-Ins menu item to add items to Visual Basic 4.0's menu.)

Menu and toolbar negotiation

A form can contain compound-document OLE objects that support full, in-place activation.

OLE automation

You can create in-process OLE servers that execute as DLLs from inside the same process as a calling application. This arrangement improves performance over calling an out-of-process server that must reload each time it executes.

Some new and improved Professional Edition OLE-related features are discussed in the next section.

New Controls

Newcomers to Visual Basic 4.0 find several new controls on the toolbar. The Visual Basic 4.0 toolbar can contain any of these three kinds of tools:

The tools you see on your toolbar depend on your edition of Visual Basic 4.0 and the controls you or others have added or removed in the past or during installation. The standard controls available to all users of Visual Basic 4.0 are the pointer (on the toolbar but not considered a true control), picture box, label, text box, frame, command button, check box, option button, combo box, list box, horizontal scroll bar, vertical scroll bar, timer, drive list box, directory list box, file list box, shape, line, image, data, and OLE control.

Standard Edition users also get these custom controls: the common dialog box, the data-bound list, the data-bound combo box, the data-bound outline, and the data-bound grid control. These last four controls are now bound to data (they were not bound in earlier versions).

Many users of Visual Basic 4.0 will find additional controls. Select Tools | Custom Controls to see a list of the custom controls available with your edition of Visual Basic 4.0. "The Professional Edition's New Features," later in this chapter, describes additional controls.

Database Improvements

The Visual Basic data controls have been around for several years. However, Visual Basic 4.0 now includes an improved method for accessing external data: the Data Access Object (DAO) model. The DAO model is a new and complete interface that provides database control you can use as a supplement or even as an improved replacement over the older data controls.

The Microsoft Jet database engine appears in all three editions of Visual Basic 4.0. Data Access Objects are any data-related objects defined by the Jet database engine. DAOs can be tables, record sets, queries, or entire databases themselves.


Caution

Although Standard Edition users can access DAOs, they cannot use the Jet engine to create new databases or DAOs. The Professional Edition is required to create new databases or DAOs.

Compilation Improvements

Starting with Visual Basic 4.0, Microsoft borrowed a successful concept from C-based and C++-based Windows programs: resource files. Resource files contain Windows resources (Windows program elements) such as menus, strings, and control layouts. If you change an item inside a resource file, you do not have to recompile the Visual Basic 4.0 code that uses that resource file. One of the major advantages of resource files is that you can put every string literal in a program inside the resource file instead of inside the code. By doing so, you can easily translate the strings to other languages when you sell your application to foreign vendors. All you need to do is translate the strings inside the resource file instead of searching for and translating every string inside the program for each country.


Unicode: Visual Basic 4.0 Thinks Globally!

Visual Basic 4.0 supports the Unicode data definition, which allows for the globalization of character sets. Although 7-bit ASCII is fine for representing standard English and many non-English languages, some language extensions (such as technical symbols and common worldwide scripts) require too many bits to fit in a 7-bit range.

Unicode uses a 2-byte representation of data to provide enough width for those additional scripts and technical symbols. Microsoft was part of the Unicode Consortium that dev-eloped the Unicode standard; many of Microsoft's products—including the Visual Basic 4.0 runtime module—support the reading of Unicode data so that you can process data read from Unicode codes. Once read, Visual Basic 4.0 converts the Unicode into a series of 7-bit ASCII representations to work with the data internally.

Microsoft made three compilation enhancements to Visual Basic 4.0: background compilation, demand compilation, and background project loading. When you want to compile and run an application, Visual Basic 4.0 initially compiles only the initial form. The rest of the compile takes place in the background while you begin working with the running application. This background compilation feature should speed your development and debugging sessions.


Note

Beginning with Visual Basic 4.0, Microsoft changed the Visual Basic 4.0 project filename extension from MAK to VBP.

Compile on Demand lets Visual Basic 4.0 decide exactly what needs to be compiled and makes a decision that is best for you. When you choose to compile a project, Visual Basic 4.0 compiles only enough of that project to get the project up and running. Compile on Demand works just like background compilation but goes one step further. Suppose that you select an option from the running application that runs code in an entirely different module and loads a form that is not sequentially next in line to be compiled. Using Compile on Demand, Visual Basic 4.0 looks at the demand you are placing on the program and compiles and runs that needed portion of the code even though that portion was not up next for typical background compilation.

In the past, when you ran a compiled project, Visual Basic 4.0 loaded the entire project before displaying the project's first form. With version 4.0, Visual Basic now loads only the forms and files needed for you to begin running a requested project.

The Professional Edition's New Features

Most new features in all three editions of Visual Basic 4.0 were described in the first part of this chapter. The most extensive improvements were made to features found in the Standard Edition so that all users benefit from the upgrade. The new and improved features described so far in this chapter work with all versions of Visual Basic 4.0. The Professional Edition adds a few extras, however. The following short sections describe some of the new features owners of the Professional Edition can expect.

Additional Controls

The Windows 95-specific controls are available as OLE custom controls in the Professional Edition. The Windows 95 controls provide support for the image list, list view, rich text, tab strip, toolbar, tree view, progress bar, status bar, and slider controls.

If you do not see these Windows 95 controls on your toolbar, select Tools | Custom Controls to add them. Select both the Microsoft Rich Textbox and Microsoft Windows Common Controls to add these Windows 95 controls to your toolbar.

Advanced OLE Support

The Visual Basic 4.0 Professional Edition supports both the use and creation of OLE automation servers. Not only can you define your own servers so that you can take advantage of OLE to share code between applications, you can force an application to expose its internal objects—including elements of the Visual Basic 4.0 programming language.

Using OLE automation, you can create an OLE server to write a code package such as a compound interest calculation you can use in several different applications. As long as the other task is also written to support OLE automation, that application does not have to understand the Visual Basic 4.0 language.

Professional Database Features

As mentioned in "The Standard Edition's New Features," earlier in the chapter, the Professional Edition lets you use the Jet engine to create new databases and Data Access Objects (DAOs). Visual Basic 4.0 introduces the Jet engine version 3.0, which offers this Professional Edition database-creation feature and improves all the following database-related issues:

In addition to making these improvements over the Jet engine version 2.5, Visual Basic 4.0 also adds several database-related properties and methods to give your data-related applications more power.

Additional Documentation

When you use Visual Basic 4.0's Professional Edition, you get additional documentation not available with the Standard Edition. You get bound, printed copies of the Language Reference, Professional Features, and Crystal Reports for Visual Basic User's Manual (the last two are not needed by Standard Edition users because the Standard Edition does not contain the Professional features or the Crystal Reports application).

The Visual Basic Books Online is also added to the Professional Edition and provides you with the following titles on CD-ROM:

All online documentation supports hyperlink cross-references.


Note

Visual Basic Books Online is available by selecting Help | Visual Basic Books Online. The screen that appears (see Figure 1.4) has Explorer-like properties that let you view the contents of the online documentation in the left window and read the specific details in the right window.


Figure 1.4. The Visual Basic Books Online opening screen.

Crystal Report Writer

When you install Visual Basic 4.0, you have the option of installing the Crystal Report Writer system. Creating reports without the Crystal Report Writer is difficult because of the cumbersome nature of the Visual Basic Print command and the other language options that output data to printers. The Crystal Report Writer lets you easily design reports with headers, footers, page numbers, titles, detail lines, and additional and more fancy output features without tedious programming effort on your part.

The Crystal Report Writer works a lot like a report wizard. It presents a Report Gallery from which you can select the style of report you want Crystal Report to create. Once Crystal Reports designs the report to your specification, you can preview the report and make adjustments as needed. Refer to Chapter 8, "Reporting Mechanisms," for more information on creating reports.

The Enterprise Edition's New Features

Users of the Enterprise Edition have all the features of the Standard and Professional Editions of Visual Basic 4.0. In addition, the following support is given to the client/server environment:

For example, once you create an OLE automation server using Enterprise Edition, you can use the remote automation feature of the Enterprise Edition to execute an OLE server on a centralized network server located far from the computer that needs the actual automation. Although a programmer may change the code inside the server, your remote application does not have to change to incorporate the updates made to the OLE automation code.

If you work with client/server technology using Visual Basic 4.0, you can use the Enterprise Edition's new Data Source Control to access and manage your remote ODBC (Open DataBase Connectivity) client/server databases from within your Visual Basic 4.0 applications.


Note

The Microsoft manual entitled Building Client/Server Applications with Visual Basic describes how the Enterprise Edition supports a multiuser client/server environment.

Summary

This chapter gave you a glimpse of the big picture. By reviewing all the new and improved Visual Basic 4.0 features, you are now ready to begin your real education as a Visual Basic 4.0 programmer. The rest of this book details the features this chapter only introduced; the book also explores several advanced ways you can make Visual Basic 4.0 work for you.

Previous Page TOC Next Page