home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


The variable Bookmarks is a global collection. When LoadNameOutline creates an entry for an employee record in the outline control, it saves the record’s bookmark as a string in the Bookmarks collection. It also stores the index of the bookmark in the collection using the corresponding ItemData property in the outline control. For example, if the 10th entry in the NameOutline control corresponds to a database record and not a last name initial, then NameOutline(10).ItemData is the index of the record’s bookmark in the Bookmarks collection.

Later, when the user clicks on an entry in the outline control’s list, the program uses the following code to find the item’s bookmark and quickly reload the corresponding record. The code uses the outline control’s Indent property to decide if the selected entry is an employee name rather than an initial. Employee names are indented two levels in the outline control’s display.

Private Sub NameOutline_Click()
Dim i As Integer
Dim bm As Integer

    i = NameOutline.ListIndex
    If NameOutline.Indent(i) = 2 Then
        bm = NameOutline.ItemData(i)
        TheRS.Bookmark = Bookmarks.Item(bm)
    End If
End Sub

When the user selects a record using the outline control, the NameOutline_Click subroutine updates the data control so it displays the selected record. There are also occasions when the data control moves to a new record of its own accord. For example, if the user deletes the current record, the data control moves to a new record. In cases such as this, the program must update the outline control so that it shows the correct record as selected. It does this in the data control’s Reposition event handler. If the data control has a current record, the event handler calls subroutine SelectName to highlight the corresponding name in the outline control.

Private Sub EmployeeData_Reposition()
    If TheRS.EOF Then
        ‘ Select no name.
        NameOutline.ListIndex = -1
    Else
        ‘ Select this name in the list.
        SelectName
    End If
End Sub

SelectName searches through the outline’s list until it finds an item with a bookmark that matches the bookmark of the current record. It then selects that item in the list.

Private Sub SelectName()
Dim mark As String
Dim i As Integer
Dim bm As Integer
Dim level As Integer
Dim parent As Integer

    ‘ If there is no record selected, do nothing.
    If TheRS.EOF Then Exit Sub
  
    ‘ Find the outline entry.
    mark = TheRS.Bookmark
    For i = 0 To NameOutline.ListCount - 1
        level = NameOutline.Indent(i)
        If level = 1 Then
            parent = i
        Else
            bm = NameOutline.ItemData(i)
            If mark = Bookmarks.Item(bm) Then
                ‘ Expand the parent.
                NameOutline.Expand(parent) = True
                ‘ Select this name.
                NameOutline.ListIndex = i
                Exit For
            End If
        End If
    Next i
End Sub

Displaying Appropriate Commands

An application should allow the user to invoke only the commands that make sense at any given time. For example, when editing a database record, the program should not allow the user to delete a different record. If the program provides too many unrelated commands, the user may become confused or forget that an edit operation is in progress.

The data control attempts to avoid these issues by automatically saving changes whenever the user moves to a new record. For example, if the user clicks the data control’s next record button (right arrow), the control automatically saves any pending changes. Similarly, if the user closes the application, the data control automatically saves any changes.

This strategy is a what-you-see-is-what-you-get (WYSIWYG, pronounced wizzy-wig) interface. When the user makes a change to the data, that change is reflected in the database as far as the user is concerned.

Unfortunately, this approach causes a few problems. First, the data control does not really update the database when the user modifies the data, but only later when the user moves to a new record. This what-you-see-is-almost-what-you-get interface can cause some very subtle problems if several users access the database at the same time. For example, suppose one user opens a records and makes some changes. If another user opens the same record, the changes will not be visible because the first user has not yet moved to a new record and the changes have not yet been saved to the database.

The data control’s default behavior also uses a poor user interface style. Ideally, an application will allow the user to undo changes made to the application’s data. Failing that, the program should at least give the user the chance to confirm or cancel major changes that cannot be undone. By default, the data control saves changes with no chance to confirm or cancel, and it does not allow the changes to be undone.

PeopleWatcher requires the user to explicitly accept or cancel any changes to the database. When the user edits a record, PeopleWatcher uses the SetControlsEditing subroutine to enable only the commands appropriate for editing and to hide the others. The EndEditing subroutine does the opposite. It is so similar to SetControlsEditing that it is not presented here. By allowing only the commands that are appropriate at a given moment, PeopleWatcher helps the user stay focused on the task of editing a record.

Sub SetControlsEditing()
    mnuFileLogon.Enabled = False
    mnuFileChangePassword.Enabled = False
    mnuFileExit.Enabled = False
    mnuEditRefresh.Enabled = False
    mnuEditNew.Enabled = False
    mnuEditEdit.Enabled = False
    mnuEditCopy.Enabled = False
    mnuEditDelete.Enabled = False
    NameOutline.Enabled = False
    EmployeeData.Enabled = False
    mnuEditAccept.Enabled = True
    mnuEditCancel.Enabled = True
End Sub

Editing Records

To edit PeopleWatcher’s current record, the user can begin typing in any of the data bound controls. The program detects the change and calls subroutine BeginEditing. For example, the FirstNameText control’s Change event is invoked when the user changes the data in the first name field. Visual Basic sets the text control’s DataChanged property to true if the value of the bound control has been modified other than by retrieving a new record. In particular, DataChanged will be true if the user has changed the data.

Private Sub FirstNameText_Change()
    If FirstNameText.DataChanged Then BeginEditing
End Sub

Subroutine BeginEditing first checks to see if the application is already editing the record. If not, it uses the Recordset’s Edit method to prepare the database for changes to the data. It then uses the SetControlsEditing subroutine described in the previous section to enable the commands that are appropriate for editing. No other work is necessary until the user decides to accept or cancel the changes.

Sub BeginEditing()
    If Editing Then Exit Sub
    Editing = True
    EditStatus = EDIT_STATUS_UNKNOWN
  
    ‘ Start editing (if we are not adding a new
    ‘ record).
    If TheRS.EditMode = dbEditNone Then TheRS.Edit

    ‘ Set the controls for editing.
    SetControlsEditing
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-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.