![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Use the Insert Form command to add a new form to the project. Set the forms Caption property to Select Wine and its name to frmSelectWine. The BorderStyle property should be Fixed Single. Then, add the following controls to the form and set their properties as indicated: ADO Data control:
DataList control:
Text Box:
Command Button (in a control array):
Command Button (in a control array):
For the DataList control, you set the ListField property to Description, so that the control displays the wine descriptions. You also set the BoundColumn property to StockNo, so that the BoundText property returns the StockNo value for the selected wine. Finally, you need to add two label controls to identify the DataList control and the Text Box. When you finish designing this form, it should look something like Figure 23.2. The only initialization step required for this form is to set the value in the Quantity Text Box; this is done in the forms Load event procedure. You use a default value of 12, because that is the most commonly ordered quantity of wine. As a convenience, when the user selects a wine by clicking on it in the DataList control, you will move the focus to the txtQuantity box. These procedures are shown in Listing 23.5. Listing 23.5 The Select Wine forms Load and DataList1_Click event procedures. Private Sub Form_Load() txtQuantity.Text = 12 End Sub Private Sub DataList1_Click() txtQuantity.SetFocus End Sub Finally, this form needs a procedure for the Command Buttons. If the user selects the Cancel button, code sets the value in the Quantity Text Box to zero. This signals the calling function that the user canceled (as youll see later). If the user selects a wine and then clicks on OK, several steps must be performed:
The code for the Select Wine forms Command Buttons Click event procedure is presented in Listing 23.6. Listing 23.6 The Click event procedure for the Select Wine forms Command Buttons. Private Sub Command1_Click(Index As Integer) Select Case Index Case 0 OK Be sure data has been entered. If DataListWines.BoundText = Or txtQuantity.Text = Then MsgBox (You must select a wine and specify a quantity.) txtQuantity.SetFocus Exit Sub End If Copy the data for the selected wine to the Invoices form. frmInvoice.txtStockNo.Text = DataListWines.BoundText frmInvoice.txtQuantity.Text = txtQuantity.Text Enable the Save button on the Invoices form. frmInvoice.Command1(0).Enabled = True Hide the SelectWine form. Hide Case 1 Cancel, as indicated by this Text Box being 0. frmInvoice.txtQuantity.Text = Hide End Select Connecting To The Invoices TableBecause the main purpose of this form is to enter orders or invoices, it must be connected to the Invoices table in your database, which is your next step. To illustrate how to access a database without the ADO Data control, you will use ADO objects and code only for this task. You need to do two things with the Invoices table. First, when a user starts a new invoice, you need to add a new record to the table representing the new invoice. In addition, you need to get the InvNo value that is assigned to this record automatically by the database engine. This value is needed for new records in the Items table, to associate each item (wine) with the corresponding invoice. Second, you need to consider what happens if the user cancels the entry of a new invoice. Good database design requires that the associated record in the Invoices table be deleted. I wrote a single function to perform both of these tasks. Called CreateNewInvoice, it takes a single argument named Delete. If Delete is zero, the function adds a new record to the Invoices table, using values for CustID, CustPO, and Date that are obtained from the Invoices form. The InvNo value of the newly added record is then saved in the global variable InvoiceNumber, so that it is available to the code that adds new records to the Items table. If passed a non-zero value as its argument, the function deletes the record in the Invoices table that has that InvNo value.
|
![]() |
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. |