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


You can also specify that a Basic module be executed first when the program runs. To do this, select Sub Main from the Startup Object list and then put the code you want executed when the program starts in a procedure named Main in the Basic module. This technique is not commonly used, but it can be valuable when initialization code in the Basic module must be executed before any forms are loaded. Once the initialization is complete, code in the Basic module displays the program’s first form.

Chameleon Command Buttons

Let’s take a few minutes to think about the program’s eight Command Buttons. Four of them navigate among the records: forward or back one record, and to the first or last record. What other commands might the user need to enter? An Exit button is necessary, of course, and a List button for displaying the alphabetical list. These two buttons will be available at all times. This brings our total, so far, up to six buttons, but there are still more commands to consider. The program will always be in one of two states: browsing and editing records, or entering a new record (indicated by the EnteringNew Boolean variable). If the user is entering a new record, Save and Cancel commands are needed. If the program is in the browsing state, New (to begin entering a new record) and Delete (to delete the current record) commands are needed. This adds up to four commands for only two remaining Command Buttons. Do we need to add two more buttons?

No. We can use a programming trick that allows a single Command Button to serve two or more purposes. By changing the button’s Caption property, and by using a flag variable to determine what the program does when the button is clicked, two of the Command Buttons can serve double duty. Here’s how it works:

  When the program starts, the buttons will display the New and Delete captions that we assigned during form design. The EnteringNew flag variable is set to False. The button’s Click event procedure tests the value of EnteringNew and directs execution accordingly.
  If the user clicks on the New button, EnteringNew is set to True, and the button captions are changed to Save and Cancel. Code in the Click event procedure can now direct execution differently, because EnteringNew has a different value.
  When the user clicks on Save or Cancel, the process is reversed: EnteringNew is set to False again, and the button captions are set back to New and Delete.

Some of this action goes on in the Click event procedure for the array of Command Buttons. You’ll find this code in Listing 19.4.

Listing 19.4 The Command Buttons’ Click event procedure.

Private Sub cmdAction_Click(Index As Integer)

‘ For the control array of four “action” buttons.

Dim Reply As Integer

If EnteringNew Then     ‘ If we’re entering a new record, the first two
    Select Case Index   ‘ buttons have different meanings.
        Case 0      ‘ Save.
            Call SaveCurrentRecord
            EnteringNew = False
            cmdAction(0).Caption = “New”
            cmdAction(1).Caption = “Delete”
        Case 1      ‘ Cancel.
            CurrentRecord = OldRecord
            EnteringNew = False
            cmdAction(0).Caption = “New”
            cmdAction(1).Caption = “Delete”
            Call DisplayRecord(CurrentRecord)
        Case 2      ‘ List.
            frmList.Show
        Case 3      ‘ Exit.
            Call ByeBye
End Select
Else                 ‘ If we're not entering a record.
    Select Case Index
        Case 0      ‘ New.
            EnteringNew = True
            Call AddNewAddress
        Case 1      ‘ Delete.
            Reply = MsgBox(“Delete this address?”, vbYesNo + vbQuestion)
            If Reply = vbYes Then
                txtFName.Text = “”
                txtLName.Text = “”
                txtAddress.Text = “”
                txtCity.Text = “”
                txtState.Text = “”
                txtZip.Text = “”
                Call SaveCurrentRecord
            End If
        Case 2      ‘ List.
            frmList.Show
        Case 3      ‘ Exit.
            Call ByeBye
    End Select
End If

End Sub

Two items in Listing 19.4 require special mention:

  If the user selects the List button, this line of code is executed:
     frmList.Show

The first part, frmList, is the name of the form where the records will be listed. The Show method does just what it implies: Shows the form. What happens next is determined by code in that form module, which we will deal with soon.
  If the Exit button is selected, the program calls the ByeBye procedure, closing the file and using the Unload statement to unload the program’s forms (see Listing 19.5).

Listing 19.5 The ByeBye procedure ends the program.

Public Sub ByeBye()

‘ Close the data file and end the program.

Close #FileNum
Unload frmAddress
Unload frmList

End Sub
Table 19.1 Possible values for UnloadMode.

Constant Value Meaning
vbFormControlMenu 0 The user chose the Close command from the form’s Control menu.
vbFormCode 1 The Unload statement is invoked from code.
vbAppWindows 2 The current Windows session is ending.
vbAppTaskManager 3 The Windows Task Manager is closing the application.
vbFormMDIForm 4 An MDI child form is closing, because the MDI form is closing.

Why not execute the End statement to end the program? We would like to let the user confirm, just in case the Exit button was clicked by accident. End does not permit this, because it terminates the program immediately. In contrast, unloading a form triggers its QueryUnload event procedure. Code in this procedure can query the user to confirm the desire to end the program. The skeleton of this procedure is shown here:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

End Sub

The UnloadMode argument specifies how the Unload event was triggered. Table 19.1 shows the possible values.

In our case, the UnloadMode argument will have the value one. Because we don’t really care why the form is being unloaded, however, we will just ignore this argument. The Cancel argument, on the other hand, is central to the purpose of this procedure. If code in the procedure sets Cancel to any nonzero value, the Unload event is canceled and the program does not terminate. If Cancel is left at its initial zero value, the program terminates. In our program, we’ll display a message box with Yes and No buttons asking if the user really wants to exit, then set the value of Cancel accordingly. This procedure is shown in Listing 19.6.

Listing 19.6 The QueryUnload event procedure.

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

‘ If the user tries to exit, confirm.

Dim Reply As Integer

Reply = MsgBox(“Exit - are you sure?”, vbYesNo + vbQuestion)

If Reply = vbYes Then
    Call ByeBye
Else
    Cancel = True   ‘ Setting Cancel to a non-zero value
End If              ‘ cancels the exit.

End Sub


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.