2. The Visual Basic Language
Example 2-2: Savings Account - Key Trapping
- 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
- 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
Else
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
Else
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
Else
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.
Counter Hit
This Homepage is special brought to you by CK Tan