Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


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 control’s KeyPress event, to ensure that only the desired characters—in this case, numbers and the period or decimal point—can be entered in the Wines form’s 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 Form

For the Customers table, the following two verifications are most crucial:

  That the ZIP code field contains 5 characters
  That the Company, Address, City, and State fields all contain data

First tackle the ZIP code field. You’ll 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, you’ll 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. Here’s how it works:

1.  Verify that the focus has left one of the Text Boxes that you are interested in (Company, Address, City, or State), which have Index properties of one through four. If not, exit the sub procedure.
2.  See whether the Text Box is empty. If not, the validation has succeeded, so exit the sub procedure.
3.  Construct an error message that is appropriate for the specific Text Box that is being tested.
4.  Display the message and set KeepFocus to True.

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. I’m 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 Orders

The last steps in your database development project involve entering orders and generating invoices. After all, that’s 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:

  Specify the customer who is placing the order. To minimize the chance for errors, you’ll design the program so that the customer is selected from a list of names that is already in the Customers table. If it’s a new customer, the customer data has to be entered using the Customers form, before the order can be entered.
  Enter the customer’s purchase order (PO) number. Because you have no way of knowing the customer’s PO number ahead of time to generate it in code, this item of information has to be entered manually at the time of the customer’s order.
  Enter the order date. You can let the computer enter the order date, retrieving the date from its clock and entering it in the proper database field.
  Select one or more wines. For each wine, the quantity being ordered must be specified, too. Again, simplify the operator’s task and minimize errors by requiring that each wine be selected from a list of wines that is already entered in the Wines table.

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 you’ll see that Visual Basic provides many tools that greatly simplify the challenge.


Previous Table of Contents Next


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.