![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Note that the error-handling code deals specifically with the duplicate stock number error and simply reports any other errors that occur. If you find that the Wines form tends to generate other ADO errors, you can modify the error-handling code to deal with them also. In the Wines form, you also want to verify that the entry in each of the price fields contains only numbers and, optionally, a decimal point. For this validation task, use the Text Box controls KeyPress event, to ensure that only the desired charactersin this case, numbers and the period or decimal pointcan be entered in the Wines forms three price fields. The KeyPress event procedure for the WINES.FRM Text1 control is shown in Listing 23.1. Listing 23.1 The Text1_KeyPress event procedure in WINES.FRM. Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer) Filter only for the 3 price fields, which have indexes 6, 7, and 8. If Index < 6 Or Index > 8 Then Exit Sub 46 = period, 8 = Backspace If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 _ And KeyAscii <> 8 Then Beep KeyAscii = 0 End If End Sub Validating Data In The Customers FormFor the Customers table, the following two verifications are most crucial:
First tackle the ZIP code field. Youll use the LostFocus event, which is triggered whenever the control loses the focus. During data entry or editing, the control loses focus when the user tabs to the next Text Box. It also occurs if the focus is on the ZIP code field when one of the Command Buttons is clicked on. Listing 23.2 shows the code to place in the Text1_LostFocus event procedure. Because this is a control array, it is necessary to identify the ZIP code field by the Index property. Listing 23.2 The LostFocus event procedure for the Text Box array in CUSTOMER.FRM. Private Sub Text1_LostFocus(Index As Integer) Verify that entry in ZIP field has 5 characters. The index of that TextBox is 5. If Index = 5 Then If Len(Text1(5).Text) <> 5 Then MsgBox (ZIP code must have 5 characters) Text1(5).SetFocus End If End If End Sub For the next validation task, youll use the Validate event. You want this event to fire during data entry whenever the focus moves from one of the Text Boxes to another control, except if the user clicks on the Cancel button. Therefore, the first step is to set the CausesValidation property of the Cancel button (Index 1 in the control array) to False, leaving this property at its default value of True for all the other controls on the form. Next, place the code from Listing 23.3 in the Validate event procedure for Text1. Heres how it works:
Listing 23.3 Using the Validate event to verify data. Private Sub Text1_Validate(Index As Integer, _ KeepFocus As Boolean) Validate that the Company, Address, City, and State boxes are not empty. Dim msg As String If Index < 1 Or Index > 4 Then Exit Sub If Len(Text1(Index).Text) > 0 Then Exit Sub msg = Select Case Index Case 1: msg = company Case 2: msg = address Case 3: msg = city Case 4: msg = state End Select If msg <> Then KeepFocus = True MsgBox (The & msg & field may not be left blank.) End If End Sub After entering the validation code presented in the previous sections, you can run the program again. The functionality of the program will not appear to be different than before, unless you try to enter invalid data that is caught by one of the validation methods that you have added. Im sure that you can think of other aspects of data entry that would benefit from validation. Now that you know the validation methods available, try writing the code yourself. Entering OrdersThe last steps in your database development project involve entering orders and generating invoices. After all, thats the main job of this database: to permit the entry of order information. Take a moment to think about what the user of the program needs to do for each order:
You can already see, perhaps, that the preceding tasks are the most complex part of the project. All four database tables are involved: the Customers and Wines tables, to permit selection; the Invoices table, to add a new invoice record; and the Items table, to add one or more new records. Juggling all of these tables and keeping everything straight is not a trivial task, but as we design this form youll see that Visual Basic provides many tools that greatly simplify the challenge.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |