Software Development

Make use of Visual Basic's predefined constants

VB6 provides a set of predefined symbolic constants that you can use anywhere in your program. This article takes a look at a few of the more useful constants.

Visual Basic 6 provides a set of predefined symbolic constants that you can use anywhere in your program. They're useful because the constant name, which always starts with the letters vb, is descriptive of what the constant is. This article takes a look at a few of the more useful constants.

The constant I use most often is vbCrLf, which is a combination of a carriage return and a line feed. In other words, it starts a new line when you're outputting text. For example, you can break Message Box text over two or more lines when you have a long message to display:

MessageBox "This is a long message that should be broken over
" & vbCrLf & _
    "two lines to prevent the message box from being too wide."

If you have need for a carriage return or a line feed alone, you can use vbCr and vbLf.

The constant vbNullChar is the character with the value 0; it's equivalent to Chr(0). It's useful in various situations such as calling external procedures that use C calling conventions, in which strings must be terminated with a null character.

The constant vbNullString represents a string with the value 0. This isn't the same as vbNullChar or an empty string (""). It too is used for calling external procedures such as those in the Windows API.

vbObjectError represents the highest error code used by VB. When you're defining your own errors, you should always use values greater than this. For example:

Err.Raise vbObjectError + 500

Finally, two text-related constants are vbTab for the tab character, and vbBackspace for the backspace character.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

0 comments

Editor's Picks