Visual Basic Symbolic Constants
• Many times in Visual Basic, functions and objects require data arguments that
affect their operation and return values you want to read and interpret. These
arguments and values are constant numerical data and difficult to interpret
based on just the numerical value. To make these constants more
understandable, Visual Basic assigns names to the most widely used values -
these are called symbolic constants. Appendix I lists many of these
constants.
• As an example, to set the background color of a form named frmExample to
blue, we could type:
frmExample.BackColor = 0xFF0000
or, we could use the symbolic constant for the blue color (vbBlue):
frmExample.BackColor = vbBlue
• It is strongly suggested that the symbolic constants be used instead of the
numeric values, when possible. You should agree that vbBlue means more
than the value 0xFF0000 when selecting the background color in the above
example. You do not need to do anything to define the symbolic constants -
they are built into Visual Basic.
Defining Your Own Constants
• You can also define your own constants for use in Visual Basic. The format
for defining a constant named PI with a value 3.14159 is:
Const PI = 3.14159
• User-defined constants should be written in all upper case letters to
distinguish them from variables. The scope of constants is established the
same way a variables’ scope is. That is, if defined within a procedure, they
are local to the procedure. If defined in the general declarations of a form,
they are global to the form. To make constants global to an application, use
the format:
Global Const PI = 3.14159
within the general declarations area of a module.
Visual Basic Branching - If Statements
• Branching statements are used to cause certain actions within a program if a
certain condition is met.
• The simplest is the single line If/Then statement:
If Balance - Check < 0 Then Print "You are overdrawn"
Here, if and only if Balance - Check is less than zero, the statement “You are
overdrawn” is printed.
• You can also have If/Then/End If blocks to allow multiple statements:
If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"
End If
In this case, if Balance - Check is less than zero, two lines of information are
printed.
• Or, If/Then/Else/End If blocks:
If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"
Else
Balance = Balance - Check
End If
Here, the same two lines are printed if you are overdrawn (Balance - Check <
0), but, if you are not overdrawn (Else), your new Balance is computed.
• Or, we can add the ElseIf statement:
If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"
ElseIf Balance - Check = 0 Then
Print "Whew! You barely made it"
Balance = 0
Else
Balance = Balance - Check
End If
Now, one more condition is added. If your Balance equals the Check amount
(ElseIf Balance - Check = 0), a different message appears.
• In using branching statements, make sure you consider all viable possibilities
in the If/Else/End If structure. Also, be aware that each If and ElseIf in a block
is tested sequentially. The first time an If test is met, the code associated with
that condition is executed and the If block is exited. If a later condition is also
True, it will never be considered.
Key Trapping
• Note in the previous example, there is nothing to prevent the user from typing
in meaningless characters (for example, letters) into the text boxes expecting
numerical data. Whenever getting input from a user, we want to limit the
available keys they can press. The process of interecepting unacceptable
keystrokes is key trapping.
• Key trapping is done in the KeyPress event procedure of a control. Such a
procedure has the form (for a text box named txtText):
Private Sub txtText_KeyPress (KeyAscii as Integer)
.
.
.
End Sub
What happens in this procedure is that every time a key is pressed in the
corresponding text box, the ASCII code for the pressed key is passed to this
procedure in the argument list (i.e. KeyAscii). If KeyAscii is an acceptable
value, we would do nothing. However, if KeyAscii is not acceptable, we
would set KeyAscii equal to zero and exit the procedure. Doing this has the
same result of not pressing a key at all. ASCII values for all keys are
available via the on-line help in Visual Basic. And some keys are also defined
by symbolic constants. Where possible, we will use symbolic constants; else,
we will use the ASCII values.
• As an example, say we have a text box (named txtExample) and we only
want to be able to enter upper case letters (ASCII codes 65 through 90, or,
correspondingly, symbolic constants vbKeyA through vbKeyZ). The key
press procedure would look like (the Beep causes an audible tone if an
incorrect key is pressed):
Private Sub txtExample_KeyPress(KeyAscii as Integer)
If KeyAscii >= vbKeyA And KeyAscii <= vbKeyZ Then
Exit Sub
Else
KeyAscii = 0
Beep
End If
End Sub
• In key trapping, it's advisable to always allow the backspace key (ASCII code
8; symbolic constant vbKeyBack) to pass through the key press event. Else,
you will not be able to edit the text box properly.
Example 2-2
Savings Account - Key Trapping
1. Note the acceptable ASCII codes are 48 through 57 (numbers), 46 (the
decimal point), and 8 (the backspace key). In the code, we use symbolic
constants for the numbers and backspace key. Such a constant does not
exist for the decimal point, so we will define one with the following line in the
general declarations area:
Const vbKeyDecPt = 46
2. Add the following code to the three procedures: txtDeposit_KeyPress,
txtInterest_KeyPress, and txtMonths_KeyPress.
Private Sub txtDeposit_KeyPress (KeyAscii As Integer)
‘Only allow number keys, decimal point, or backspace
If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or
KeyAscii = vbKeyDecPt Or KeyAscii = vbKeyBack Then
Exit Sub
Else
KeyAscii = 0
Beep
End If
End Sub
Private Sub txtInterest_KeyPress (KeyAscii As Integer)
‘Only allow number keys, decimal point, or backspace
If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or
KeyAscii = vbKeyDecPt Or KeyAscii = vbKeyBack Then
Exit Sub
Else
KeyAscii = 0
Beep
End If
End Sub
Private Sub txtMonths_KeyPress (KeyAscii As Integer)
‘Only allow number keys, decimal point, or backspace
If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or
KeyAscii = vbKeyDecPt Or KeyAscii = vbKeyBack Then
Exit Sub
Else
KeyAscii = 0
Beep
End If
End Sub
(In the If statements above, note the word processor causes a line break
where there really shouldn’t be one. That is, there is no line break
between the words Or KeyAscii and = vbKeyDecPt. One appears due to
page margins. In all code in these notes, always look for such things.)
Rerun the application and test the key trapping performance. Save the
application