MetaPro Systems Inc.

Home Services Software Approach Who We Are
Resources Testimonials Contact Us Products

VB Tips & Tricks 11 - 20

Visual Basic Tips & Tricks # 11 – Modify the Tool Bar

When you compile your project, you most often want to use “Start with Full Compile” rather then “Start”.This is because “Start” only compiles the code you need to execute your project while “Start with Full Compile”compiles all your code.You can run “Start with Full Compile”by pressing Control F5 or choosing “Start with Full Compile”from the Run Pull-Down.However, just clicking on the tool bar is faster.

The above is an example of where the ability to modify the tool bar is useful.Follow these instructions to add “Start with Full Compile” ”to the “Run” Tool Bar :

Under the View pull-down, choose “Toolbars”, “Customize”, “Commands” and“Run”.Next drag “Start with Full Compile”to the “Run” Tool Bar. Choose Modify Selection, then Change Button. Next Click on the icon you want to represent “Start with Full Compile”. To eliminate the words choose Modify Selection again, then Default Style.

Visual Basic Tips & Tricks # 12 – Select Text in Controls

The Windows standard is to select the content of a control when you get focus with your mouse or by tabbing.Visual Basic doesn’t do this by default.This simple subroutine will do it for you.

Public Sub SelectControl(iobjSelectControl As Object, Optional piExtraLength As Integer)

Dim iExtraLength

If IsMissing(piExtraLength) Then

iExtraLength = 0


iExtraLength = piExtraLength

End If

iobjSelectControl.SelStart = 0

iobjSelectControl.SelLength = Len(iobjSelectControl.Text) + iExtraLength

Exit Sub

Within your control’s Gotfocus event, insert the following line of code:

Call SelectControl(Me.ActiveControl)

Visual Basic Tips & Tricks # 13 – Encapsulated Data Access

It makes sense to create a class for all your data access routines. Opening and closing the database, and creating and accessing record sets will be methods of the class. This class can be instanced once or more than once depending on your application. The advantage is that the application is easier to maintain. When Microsoft introduces a new data access method, for example, BDO (Better Data Objects), it will be much simpler to convert the application.

Visual Basic Tips & Tricks #14  - Comments Anyone?

How much internal documentation is the right amount for your project? Having the right amount of comments can make it easier for you or someone else to maintain your application weeks, months and even years in the future.

Too much documentation can be a problem because it makes it difficult to follow the code. As the program changes, there is no guarantee that the comments will agree with the code.

For each significant routine, we put in a standard template containing a summary of what the routine does. Following is an example.


'Name: vStripPhone Date: November 10, 1999

'Author: Robert M. Avallone, MetaPro Systems Inc.

'Purpose: To remove special characters from a phone number


We only document inline when the coding technique is unusual or difficult to follow.

The best documentation is a well-written program with meaningful data names.

Write your comments “as you go”. This will make it easier to understand the code while you are developing the project.

Visual Basic Tips & Tricks #15 - Installing the Calendar Control

This hint applies to Visual Basic 6.0. When you create a kit and try to install the application on a production machine, you may have a problem if your application contains the calendar control. This problem was noticed while using the Package & Deployment wizard. You may receive a similar error when using a third party installation package. The error message relates to a failure to self register a control. The control is not identified in the error message.

Here is the solution. Look for the file MSCAL.DEP on your system. Look for this line:


Remove the “Ex”. Do this now so you don’t have a problem later.

Visual Basic Tips & Tricks #16 - Handling Controls

This tip covers the use the visible, enable and locked properties for controls in Visual Basic.

It is much better to set the enable or visible property to false so that users cannot press a button, than to allow the user to click on the button and get an error message. If the buttons on a form do not change during the time the form is active, make the buttons invisible. If you hide a button in a button row or column, be sure to move to the remaining buttons so that there isn’t a gaping hole. If the button’s validity will change as a result of users’ actions, disable and re-enable the button. Changing the button from visible to invisible may be too disconcerting to users in this case.

Similarly, a text box with contents that cannot be changed should have the locked property set to true. This setup looks much better than disabling the box. A good way to identify a read-only text box is to set the back color to light gray.

Visual Basic Tips & Tricks #17 - Moving Buttons

The prior tip mentioned that whenever you hide a button in a button row or column, you should move to the remaining buttons so that there is not a hole. This routine will accomplish that feat.

The routine assumes there is a Button Array called cmdArray.

The error handling and the comments header have been removed to save space.

Public Sub MoveButtons()
Dim i As Integer
Dim intLeftPosition As Integer
Dim iSpaceBetweenButtons As Integer
intLeftPosition =cmdArray(0).Left
iSpaceBetweenButtons = cmdArray(1).Left - cmdArray(0).Left

For i = 0 To gkiLastButton ‘ from 0 to 1 less than the number of buttons

If cmdArray(i).Caption <> gksNotUsed Then

‘ assumes caption on invisible buttons is not used
cmdArray(i).Move intLeftPosition, 0
intLeftPosition = intLeftPosition + iSpaceBetweenButtons

End If


End Sub

Visual Basic Tips & Tricks #18 - Creating Icons

For a more professional looking desktop, create a unique icon for your Visual Basic applications. The creation of a special icon allows your application to stand out among the many icons that may be on the user’s desktop.

Although you can use any .ico file on your system as your icon, I encourage you to try to create your own unique icon using an icon editor.

Follow the steps below to create your custom icon:

1.Choose a form in your Visual Basic application. The main or About forms are good choices.

2. Bring up the Properties window for the form by pressing F4 while the mouse focus is on the form

3. Under Properties, click on Icon.

4. To the right of the icon property, a box with three eclipses will appear (… ). This box allows you to browse for and select the icon you want.

5. Under Project, find the Properties for your project name; e.g., MyProject Properties.

6. Choose the form you have used above from the icon combo box.

7. Click OK.

When you build .exe files or a kit, your icon will be used.

Visual Basic Tips & Tricks #19 - Third-Party Tools

Although Microsoft provides a wide variety of controls, there are many more controls that you can purchase from other vendors. But, you should avoid using these third-party tools whenever possible. Third-party tools can sometimes cause problems with upgrades, installation and moving the development environment.

Having said that, the purchase of a third-party control is sometimes the best answer to a problem. If Microsoft does not have a control to handle the functionality, the purchase of a third-party control can save you, your client or your company the expense of writing the code yourself.

Third-party controls range from the very simple and inexpensive to the very complex and expensive. When choosing a control or control package check with your friends and colleagues, find out about the company’s reputation and look for the top sellers. An important phrase to look for in the promotional literature is Royalty Free Distribution.  This means that when you purchase this application you can distribute it to an unlimited number of user machines at no additional cost. You will still need a license for each development machine. If you do not see this phrase, ask the vendor about it.

Visual Basic Tips & Tricks #20 - Resizing Forms

In most cases, your application will run on screens of different sizes and resolutions. Even if all users have the same type of terminal, they may want to resize windows created from the forms of your application. Generally, when you resize a window, you want the controls to be resized proportionally. This does not happen automatically in VB.

Although you can write code to make this happen, this is one area where a third-party control is usually the best solution. There are several to choose from. Most of them have free usage for a trial period. Download a few of them, use them, compare them, and then choose the one that works best for you. The one I use and recommend to my clients is Resize OCX by Larcom and Young ( It is inexpensive and easy to use.

Be sure to test your application with the least possible desktop area. Choose Control Panel, Display and Settings to change this setting. Some forms developed with More desktop area will not expanded properly at Less desktop area. The form will be too big to fit on the screen.