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


Adding Buttons To The Toolbar

That was a long detour to cover the details of the Toolbar control. Getting back on track, it’s time to create the three buttons for the Toolbar on your MDI form. Using the techniques explained earlier, add three buttons—leaving all of their properties at the default setting, except as shown in Table 22.3.

After adding these buttons, close the Toolbar’s property sheet. Your MDI form should resemble Figure 22.6.

Table 22.3 Nondefault properties for the three Toolbar buttons.

Index Caption ToolTipText
1 Customers Edit/add customers
2 Wines Edit/add wines
3 Exit Exit program


Figure 22.6  The MDI form and Toolbar after adding the first three Toolbar buttons.

As you might guess, responding to Toolbar button clicks is accomplished in an event procedure. The Toolbar object has a ButtonClick event procedure that is called when any of the control’s buttons are clicked on. This procedure is passed a type Button argument that specifies which button was clicked on. You can query the Button argument’s properties to identify the button, and you can use any of the properties that are unique to each button, such as Index or Caption. To add code to this event procedure, right-click on the Toolbar and select View Code from the pop-up menu. A Code Editing window opens; if necessary, select ButtonClick from the Procedure list at the top of the window.

The code for this procedure is shown in Listing 22.1. Notice that you respond to clicks of the “Customers” and “Wines” buttons by displaying the appropriate form. These forms haven’t been designed yet, but you can reference them in code, as long as you don’t try to execute the code (which would cause an error message, of course). Clicking on the Exit button displays a message box first, confirming that the user wants to exit, and then uses End to terminate the program.

Listing 22.1 The Toolbar control’s ButtonClick event procedure.

Private Sub Toolbar1_ButtonClick(ByVal Button As Button)

Dim Reply As Integer

Select Case Button.Caption
    Case “Customers”
        frmCustomers.Show
    Case “Wines”
        frmWines.Show
    Case “Exit”
        Reply = MsgBox(“Quit program - are you sure?”, _
            vbYesNo + vbQuestion, “Quit?”)
        If Reply = vbYes Then End
End Select

End Sub

One last thing before you leave the MDI form: place the following line of code in the General section of the form’s code:

Public EnteringRecord As Boolean

Then, put this line in the Form_Load event procedure:

EnteringRecord = False

These lines declare the flag variable and initialize it to the proper value when the program loads.

Designing The Customers Form

The next task is to design the form to display customer information. To add a new form to the project, select Form from the Insert menu. This form will have the Name property “frmCustomers” and will be saved with the file name CUSTOMERS.FRM. You need to add the following items to this form:

  A control array of eight Text Box controls, one for each field in the Customers table.
  A control array of eight Label controls, one to identify each Text Box. The Label controls don’t have to be in a control array, but using cut-and-paste is easier than dragging each individual control when placing several controls of the same type.
  A control array of four Command Buttons.
  One ADO Data control.

When you place the Text Box controls on the form, add them in the order that a user would want to move between them—that is, in the same order as the fields are arranged in the database table. By adding the controls in this order, the tab order will be arranged similarly, and the user can Tab and Shift+Tab between Text Boxes in the most efficient order. Although you can change tab order later, after adding the controls, adding them in the correct order now saves time. The completed form is shown in Figure 22.7.

Before you set other object properties, you first need to set the ADO Data control’s properties, to connect it to the Customers table in the GRAPEVINE database. The ConnectionString property is set first. Select the ADO Data control, select the ConnectionString property in the Properties window, and then click on the button with the three dots. Visual Basic displays the General tab of the control’s Property Pages dialog box, as shown in Figure 22.8.


Figure 22.7  The completed Customers form.

Three ways exist to specify the connection between the control and a database. You will use a Connection String. Select the option for Connection String and then click on the Build button. After you are familiar with database programming, you may be able to write the connection string from scratch, but for now, let Visual Basic help you. The Data Link Properties dialog box is displayed.

This dialog box has several tabs on it; the Connection tab is shown in Figure 22.9. Here’s what you need to do:

1.  On the Provider tab, select Microsoft Jet 3.51 OLE DB Provider.
2.  On the Advanced tab, select the ReadWrite option and deselect all others.
3.  On the Connection tab, click on the button with the three dots and then locate and select the GRAPEVINE.MDB database file. Then, click on the Test Connection button to verify that the connection to the database is working.


Figure 22.8  The ADO Data control’s Property Pages dialog box.


Figure 22.9  The Data Link Properties dialog box.

When finished, click on OK. The connection string is entered into the Property Page. Click on OK again to accept this as the ADO Data control’s ConnectionString property.

The next property of the ADO Data control that you need to set is RecordSource. This property is the statement, usually expressed in SQL, that specifies which records are to be returned by the control. The specification includes both the table name and a criterion for records to be returned from that table. The statement you use is the following:

select * from customers

This is a very simple SQL statement that says, when translated into English, “select all records from the customers table.” To enter this property, display the property page for the RecordSource property (Figure 22.10) and select 1 - adCmdText as the command type. Then, enter the preceding SQL statement in the Command text (SQL) box, and click on OK.


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.