The magazine of the Melbourne PC User Group

Seeing the Basics: Tips and Tricks
Tony Stevenson

Visual Basic 4.0, like any other programming language, has its share of tricks, techniques and trips that can save you both time and effort. Let's have a look at what's available.

For and against the variant data type

A variant data type can hold a variety of data types - numeric, date/time, or string values. However, such flexibility comes at a cost - variant data types require more memory and they are slower (because VB handles any necessary conversions).

Populating a list at design time

Instead of populating a list box at run time using the AddItem method, VB 4.0 allows you to add entries to a list at design time.

After double-clicking the list box control in the toolbox (this puts a list box control in the centre of the form), press function key F4 to bring up the properties. Click on the "List" property, then the down arrow that appears, and start entering the list items. After each item, use the keyboard combination of Ctrl + Enter keys to position the insertion pointer ready for the next entry. If you just press the Enter key, the box displaying the list items closes.

Experimenting with the Windows API

The Windows API (Application Programming Interface) is a set of functions that are part of Windows which can be called from your VB application. It allows you to extend the power of VB. However, if you don't use it correctly, for example, by making a typing mistake, you can hang your application. So when developing your application, make sure that you set the "Save Before Run" option, with or without a prompt (to set it, select the menu options: "Tools", "Options", and the "Environment Tab"). Doing this will ensure that any code that you have entered will not be lost due to a catastrophe.

The quick way to set a breakpoint

A breakpoint is a location in your code where you want VB to pause your application so you can do some investigating. To set a breakpoint quickly, open the code window, find the line where you want VB to pause, and click it using the right mouse button. When the pop-up menu appears, click on the "Toggle Breakpoint" option. Repeating this process on a line already nominated as a breakpoint will remove the breakpoint.

A cluttered form?

To preserve screen real estate on a busy form (that is, one with lots of controls) use combo boxes with the "Style" property set to "Dropdown Combo" or "Dropdown List". If you use the latter option, your user can only select an entry from the list rather than enter a new one.

Word processors and forms

If you want to look at a textual description of a form and the controls it contains, you use a word processor such as Windows 95 "WordPad". When opening a form using WordPad, remember to change "Files of type" from its default setting of *.DOC to "All Documents", otherwise WordPad will not be able to locate the forms that have a suffix of .FRM.

The reason a form can be viewed using a word processor is that a form is internally represented using ASCII (American Standard Code for Information Interchange). When you look at a form in this way, you will see the VB version number, all the properties of the form and the properties of any controls contained on the form, and all of the code associated with the form.

The flexibility of using a word processor in this way provides you with an easy, flexible and attractive way of documenting your VB project.

However, there is another way of printing the ASCII representation of a form without using a word processor. From the File menu in design mode, simply select the "Print" option. You are then presented with a form containing print options. So for example, you can choose to print the form's image, or a textual description of the form, or the code associated with the form (or all three). You also have the option of printing these to a file for later use.

Unfortunately, there is now no excuse for having a poorly documented VB project!

Carriage return and line feed constant

To improve the look and readability of your message boxes, use the VB provided constant "vbCrLf" to insert carriage returns and line feeds at the appropriate places. For example:
Dim warningMsg as String

warningMsg = "Be careful!" & vbCrLf & vbCrLf & "Do not delete these files" MsgBox warningMsg

Other useful predefined constants are:

  • vbNullChar, the Null character
  • vbBack, Backspace
  • vbTab, Tab
  • vbVerticalTab, Vertical Tab
  • vbFormFeed, Formfeed.
The VBA (Visual Basic for Applications) library contains these and other constants. To look at this library, use VB's new Object Browser by clicking its icon on the toolbar (or alternatively, by clicking on the menu options "View", "Object Browser"). By using the Object Browser, it is easy to paste objects into your code.

How to improve response time

You can use VB's form Load statement to load a form into memory, but not display it to the user. Then, at the appropriate time, you can use VB's Show method to make the form appear. You can use such a procedure to improve response times. For example, if one of your forms contains a list box with a lot of entries, you can load that form into memory when your application first starts. Most users are prepared to wait a reasonable time when an application is starting, but they are less tolerant of delay when moving from one form to another in the course of doing their job.

However, be careful using this technique, because any form that is loaded is taking up its share of memory.

The required syntax is as follows:
Load nameOfTheForm

nameOfTheForm.Show

To see the effectiveness of using a Load statement, set up a sample application containing two forms (Form1 and Form2) as follows.

On the first form, have three command buttons with the captions:

  • "LoadShow"
  • "Load"
  • "Show"
The second form only contains a list box.

For Form1, put the following code into the command button's click events:
Private Sub cmdLoadShow_Click ()
Screen.MousePointer = vbHourglass
Form2.Show
Screen.MousePointer = vbDefault
End Sub

Private Sub cmdLoad_Click ()
Screen.MousePointer = vbHourglass
Load Form2
Screen.MousePointer = vbDefault
End Sub

Private Sub cmdShow_Click ()
Form2.Show
End Sub

Note the use of the VB defined constants "vbHourglass" and "vbDefault" for setting the mouse cursor to the familiar Windows hourglass and default shapes respectively.

In the Form2 load event, put in the following code to populate the list box with the numbers from one to 10,000.

Private Sub Form_Load ()
Dim i as Integer
For i = 1 to 10000
List1.AddItem Str$(i)
Next i
End Sub

This sample application employs a simple technique that can be used to noticeably alter the user's perception of your application.

Reprinted from the April 1996 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]