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


Editing Images

Normally the user modifies a record’s data using a bound control such as a text box. However, the user cannot enter data into an image control. PeopleWatcher must provide some method for changing the picture contained in each employee’s record.

If the user double-clicks on PeopleWatcher’s employee image control, the program begins the process of selecting a new employee picture. First it checks that the user has permission to modify employee pictures. If the user does not, the program ignores the double-click. Privileges are described in a later section.

Next the program presents a file selection dialog. The user can use the dialog to select an image file. The program loads the file into the employee image control.

Because the program, rather than the user, makes the change to the image control, the program does not automatically detect this change. The data bound image control does not have a Changed event so the program does not automatically enter editing mode. The program explicitly begins editing the record by calling the BeginEditing subroutine.

Private Sub EmployeeImage_DblClick()
Dim status As Boolean

    ‘ If the user does not have the UPDATE_BASIC
    ‘ privilege, do nothing.
    If Not TheUser.HasPrivilege(“UPDATE_BASIC”) _
        Then Exit Sub

    ‘ Prepare the file common dialog.
    FileDialog.Flags = cdlOFNFileMustExist + _
        cdlOFNHideReadOnly + cdlOFNExplorer + _
        cdlOFNLongNames
    FileDialog.DialogTitle = “Select Employee Picture”
    FileDialog.filename = “*.bmp”
    FileDialog.CancelError = True

    ‘ Present the dialog.
    On Error Resume Next
    FileDialog.ShowOpen
  
    ‘ If the user canceled, we’re done.
    If Err.Number = cdlCancel Then Exit Sub
  
    ‘ If there is some other error, say so.
    If Err.Number > 0 Then
        Beep
        MsgBox “Error” & str$(Err.Number) & _
            “ reading file name. ” & vbCrLf & _
            Err.Description, _
            vbOKOnly + vbExclamation, “Error”
        Exit Sub
    End If
  
    ‘ Save the directory for next time.
    FileDialog.InitDir = _
        Left$(FileDialog.filename, _
            Len(FileDialog.filename) - _
            Len(FileDialog.FileTitle))
  
    ‘ Load the image file.
    EmployeeImage.Picture = _
        LoadPicture(FileDialog.filename)
    If Err.Number > 0 Then
        Beep
        MsgBox “Error” & str$(Err.Number) & _
            “ loading image. ” & vbCrLf & _
            Err.Description, _
            vbOKOnly + vbExclamation, “Load Error”
        Exit Sub
    End If
  
    ‘ If we succeeded, enter editing mode.
    BeginEditing
End Sub

Accepting and Canceling Changes

Normally the user will edit a new or existing record and then select either the Cancel Changes or Accept Changes command from the Edit menu.

The Cancel Changes command sets the global variable EditStatus to EDIT_STATUS_CANCEL. This value allows the data control’s Validate event handler to know that the user wants to cancel the changes. The Cancel Changes command then calls the Recordset’s CancelUpdate method to cancel the changes to the data. Finally, it calls EndEditing to enable the commands that are appropriate when the user is not editing data.

Private Sub mnuEditCancel_Click()
    EditStatus = EDIT_STATUS_CANCEL
  
    ‘ Cancel the changes.
    TheRS.CancelUpdate
  
    EndEditing
End Sub

The Accept Changes command begins by setting EditStatus to EDIT_STATUS_ACCEPT so that the data control’s Validate event handler knows the user is accepting the changes. It then uses the Recordset’s Update method to save the changes to the database.

Private Sub mnuEditAccept_Click()
    EditStatus = EDIT_STATUS_ACCEPT
  
    ‘ Save the changes.
    TheRS.Update
  
    ‘ EndEditing is called during validation.
End Sub

Validating Data

The data control’s Validate event occurs when the user attempts to accept or cancel changes to the data. It also occurs under certain other circumstances such as when the program’s main form is about to unload. While a program can disable the Exit command in its File menu, it cannot be certain the user will finish editing before stopping the application. The user might close the program in some other way, such as using the Close command in the form’s control box. The user may even shut down the operating system.

In these cases, an editing operation may be in progress. Because the user has neither accepted nor canceled the changes to the data, the program cannot know whether to save or discard the changes. The EmployeeData_Validate event handler ensures that the data is safe before it allows the program to exit.

First it checks the global variable Editing to see if the user is editing a record. If not, there are no pending changes to save so the subroutine simply exits.

Otherwise, the subroutine checks the value of EditStatus. This variable is set to EDIT_STATUS_CANCEL by the Edit menu’s Cancel Changes command. If EditStatus has this value, the Validate event handler sets Save to false to tell the data control that it should discard changes to the data.

EditStatus is set to the value EDIT_STATUS_ACCEPT by the Edit menu’s Accept Changes command. If EditStatus has this value, the Validate event handler sets Save to true so the data control will save the changed data values to the database.

If EditStatus has neither of these values, the edit is about to end without the user’s invoking either the Cancel Changes or the Accept Changes commands. The user is probably closing the application in the middle of an edit. In this case, the program asks the user whether it should save the changes.

Finally, if the program needs to save the changes, it calls function ValidForm to validate the data. ValidForm determines whether the form’s field values are valid. For example, it checks that any date fields have valid date formats. If the data values are invalid, EmployeeData_Validate sets the Action variable to vbDataActionCancel. This will make the data control cancel whatever action was about to occur. If the program was about to exit, the data control will prevent it from exiting so the user has a chance to correct the data.

Private Sub EmployeeData_Validate(Action As Integer, Save As Integer)
    ‘ If we’re not editing, do nothing.
    If Not Editing Then Exit Sub
  
    If EditStatus = EDIT_STATUS_CANCEL Then
        ‘ The user pressed Cancel.
        Save = False
    ElseIf EditStatus = EDIT_STATUS_ACCEPT Then
        ‘ The user pressed Accept.
        Save = True
    Else
        ‘ Ask the user what to do.
        Save = (MsgBox(“You have modified this record. Do you want to save the changes?”, _
            vbYesNo, “Save Changes?”) = vbYes)
    End If
  
    ‘ If the user wants to save, validate the form.
    If Save Then
        ‘ If the form is invalid, cancel the
        ‘ operation until the user fixes the data.
        If Not ValidForm() Then
            Action = vbDataActionCancel
            EditStatus = EDIT_STATUS_UNKNOWN
            Save = False
        Else
            EndEditing
        End If
    Else
        EndEditing
    End If
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.