|Home||Services||Software Approach||Who We Are|
Visual Basic Tips & Tricks #1
When you first load Visual Basic onto your machine, you should tailor VB to your needs by choosing Options under the Tools pull-down.
Under Editor Tab:
Auto Syntax Check If this box is checked, whenever you leave a line of code, VB will do a syntax check. If a compile error exists, a message box will pop up. If you do not check this box, VB will still check the syntax when you leave the line. The error message will not appear but the line will turn red. The Auto Syntax is useful when you are learning VB but after a while it involves too much clicking when the error is obvious.
Require Variable Declaration If this is checked, the line Option Explicit is added to every new procedure and function you create.This will force you to declare all variablesand assign a data type.If not checked, the data type will be defined when set equal to a literal or another variable. We strongly recommended that you always have this box checked. If you dont, your code will be difficult to understand and debug. And worst your variable will sometimes be declared in ways you were not intending.
More on Options to come in future editions.
Visual Basic Tips & Tricks #2 Dont Lose Your Changes
Choose Options under the Tools pull-down. Under Environment tab there is a frame labeled When a Project Starts. The choices are : Save Changes, Prompt To Save Changes and Dont Save Changes.
Dont Save Changes is the default. This means that any changes you make to your project are not saved until you manually save them by choosing save project (or save module, class or form) from the File pull-down. If VB or your system crashes you will lose all your changes since the last save. Unless you remember to constantly save, you could lose a lot of work.
Prompt To Save Changes means that you will be prompted to save your changes whenever you try to compile your project. VB5 and VB6 work a little differently. VB5 prompts you to save your changes after the compile is successful. VB6 prompts you before trying to compile. This is definitely the most flexible option. The only draw-back is that the constant clicking can slow you down.
Save Changes saves your changes whenever you try to compile in VB6 or successfully compile in VB5. This is the faster and safest option. The drawback of this option is that you may save changes you dont want.
Visual Basic Tips & Tricks #3 Conventions for Naming Variables & Constants
Using a simple naming convention can self-document your code, help you avoid bugs, and make debugging easier. There are several that are in common use, here is the one we use
First Letter - Describes the scope :
g Global or Public
<none> - Local
Next letter - tells whether this is variable or constant:
<none> - Variable
Next one to three letters describes the data type:
And so on .
All these characters should be lower case.
Next is the name describing the variable or constant. Use mixed case, capitalizing each word. Avoid abbreviation whenever possible.
Global Const gksOurName As String = "MetaPro Systems Inc."
Dim miNumberOfOffices As Integer
Public Sub Main()
Dim bIsItTrue As Boolean
Const ksCity As String = "Lexington"
Visual Basic Tips & Tricks #4 Save Yourself Keystrokes
There is a feature in VB that is not well documented which can save you many keystrokes. It goes like this--Partially type the name of any variable or constant, then press <control><space bar>. If there is only one name within scope, that name will be inserted in your code. If there is more than one name, a list box will appear allowing you to choose among all names within scope.
This tip saves you typing time and this feature can also be useful as a search. For example, if you use the naming convention described in tip #3, type gks then press <control><space bar>. You will get a list box of all global string constants.
Visual Basic Tips & Tricks #5 - Use of the Me keyword
The Me keyword substitutes for the name of the current object. For example, it can substitute for the name of the form in any procedure or function within that form.
If your form name is frmMain, instead of
frmMain.Caption Main Menu
You can type
Me.Caption = Main Menu
In addition to saving typing time, you can copy a form to make another form and you wont have to change these lines.
Visual Basic Tips & Tricks #6 Error Handling
In Visual Basic, it is important to use the On Error statement in every significant subroutine (and function). This will allow you to handle potentially fatal errors without causing your application to crash. An example of a fatal error is trying to divide by zero.
When a subroutine which is called by another subroutine that does not have error handling, control reverts to the error handling on the calling routine. For this reason you can leave error handling out of the simpler low level subroutines. The following is sample code that can be the basis of your subroutines.
Public Sub AnySub()
'Name: Any Sub Date/Time: September 20, 1999
'Author: Robert M. Avallone, MetaPro Systems Inc.
'Purpose: Describe the routine here
On Error GoTo ProcError
Your Code Here
On Error Resume Next
Exit Sub ' AnySub
Call DisplayError(Err) This is a routine you have to write
Visual Basic Tips & Tricks #7 Windows API Calls
Using Windows API Calls can extend the power of Visual Basic by allowing you to interface with the Windows operating system. The trick is finding the right call to solve your problem.A good source for this is Visual Basic Programmers Guide to the Windows API by Dan Ap pleman.
The following simple example retrieves the Computer Name:
Private Declare Function GetComputerName& Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long)
Public Function sGetComputerName() As String
Dim lSize As Long
Dim sComputerName As String
Dim lMaxLength As Long
lMaxLength = 32
sComputerName = Space$(lMaxLength)
If GetComputerName(sComputerName, lMaxLength) Then
sGetComputerName = Left(sComputerName, lMaxLength)
sGetComputerName = ""
Visual Basic Tips & Tricks # 8 Using the With Statement
The With Statement allows you to perform a series of statements on an object without repeating the object name.You should use With whenever possible.Not only will this save you typing time and make maintenance easier, it is also more efficient. VB doesnt have to resolve the object for each statement.
Here is an example:
Call mrsLocalHistoryTable.Find("HI_SEQ_NO = '" & psSequenceNumber & "'")
If mrsLocalHistoryTable.EOF Then
Call gcloMessage.lDisplayError(100005, psSequenceNumber)
Call .Find("HI_SEQ_NO = '" & psSequenceNumber & "'")
If .EOF Then
Call gcloMessage.lDisplayError(100005, psSequenceNumber)
Visual Basic Tips & Tricks # 9 Programming Structure
Using standards can make your code easier for someone else to follow or even yourself when you come back to it six months later. We talked about name conventions in a past tip. This tip covers programming structure. Here are some of the guidelines we use:
||Limit Gotos to exiting a routine or error handling.|
||Have only one Exit Sub or Exit Function per routine.|
||Dont nest your ifs more than three levels depth, create another routine instead.|
||Create a routine for any logic that has more than three lines and is repeated more than once.|
Visual Basic Tips & Tricks # 10 Data Access
While Null is a valid value for a field in most databases, null cannot be move to text box in VB. The following statement will convert null to an empty string, while leaving any other value alone:
txtLastName.Text = mrsEmployee!LastName & ""
The reverse is also true, you cannot store an empty string is most databases. The follow routine is what we use to solve this problem:
Public Function vMissingIsNull(sString As String) As Variant
If sString = "" Then
vMissingIsNull = Null
vMissingIsNull = sString
You can use vMissingIsNull as follows:
mrsEmployee!LastName = vMissingIsNull(txtLastName.Text)