Visual Basic Lec 2
Visual Basic Lec 2
• 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.
frmExample.BackColor = 0xFF0000
or, we could use the symbolic constant for the blue color (vbBlue):
frmExample.BackColor = vbBlue
• 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
Here, if and only if Balance - Check is less than zero, the statement “You are
overdrawn” is printed.
In this case, if Balance - Check is less than zero, two lines of information are
printed.
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:
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.
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.
• 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
Const vbKeyDecPt = 46
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