Saturday, May 14, 2011

Control structures - 1.Branching - 1. IF Statement ;- Example


Savings Account - 



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.)

3.      Rerun the application and test the key trapping performance.
.

No comments:

Post a Comment