|
 |
 |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98
Chapter 23 Wrapping It Up: Validation Code And The Invoices Form
The final steps in our database project are to write data validation code and to design the form for creating invoices.
A well-written program does everything it can to prevent errors. Certain types of errors are impossible to preventyou cant control when a disk drive is going to crash, for example. Other errors can be avoided, particularly errors in data entry. Knowing something about the data that belongs in a particular field allows you to verify that new or edited data meets the required criteria. You start this chapter by writing validation code to handle this data-verification task, and then you begin creating a more complicated form for the database program: the Invoice Entry form. You then will have a functionalthough admittedly bare-bonesrelational database.
Validation Code
You can take any one of several approaches to validating data in a database front end. In some cases, only one of these approaches will work, while at other times, you can use whichever approach you prefer. Remember, the important thing is not how you validate the data, but that you do validate the data.
Blocking Keystrokes
One validation method simply prevents the entry of inappropriate keystrokes into a Text Box, thus preventing the entry of invalid data in the first place. For example, in a ZIP code field, you know that only the 10 numerical digits are permitted (plus a hyphen if 9-digit ZIP codes are allowed). You can use the Text Boxs KeyPress event procedure to filter keystrokes. This procedure receives a type Integer argument named KeyAscii, which contains the numerical code of the key that was pressed. Code in the procedure can examine this value and determine whether it is one of the allowed keys. If it is not permitted, setting the KeyAscii argument to zero cancels the keystroke, preventing it from reaching the underlying control. For example, the following code permits entry of the digits zero through nine only, beeping if any other key is pressed. In all cases, you must pass through the Backspace character (KeyAscii value of eight), or else users will not be able to use the Backspace key toerase characters. Heres the code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then Exit Sub
If KeyAscii < 48 Or KeyAscii > 57 Then
Beep
KeyAscii = 0
End If
End Sub
TIP: ANSI Codes
Keyboard and other characters are represented internally by ANSI codes (also known as ASCII codes), with numeric values in the range of 0 through 255, representing each character. The KeyPress event procedure receives as its argument the ANSI code of the p
|