Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2013
    Location
    Chesapeake, VA
    Posts
    30

    Unanswered: Best way to handle cancel / escape ??

    Hello, what is the best way to handle a cancel in the beginning, middle or end of a form?

    Is it best to save any data collected with VBA code in variables and write it all "afterUpdate" in the very last field. Or handle potential escapes or cancels while entering data in each field?

    Not sure what is best because what I am doing is not working well and corrupting my Data.

    If you can help please let me know and I will provide more data.

    Thank you

  2. #2
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Using Unbound Forms, which is what you're talking about, really does away with the basic function of Access, which is to facilitate RAD (Rapid Application Development) and should only be attempted by very experienced Access developers, and then only when/if a legitimate purpose requires it, and this situation doesn't require it. With Bound Forms Access does the vast majority of the heavy lifting; with Unbound Forms the developer has to write code for everything, even the most mundane tasks.

    The best way is probably to simply ask the user what they want to do! If a Form has been Dirtied, i.e had data entered or data edited, when they try to leave the Record or close the Form or Access, itself, the Form_BeforeUpdate event fires. So ask them at this point if they want to save or dump the New Record or the changes to an existing Record:
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Not (Me.NewRecord) Then
      If MsgBox("Would You Like To Save The Changes To This Record?", vbQuestion + vbYesNo + vbDefaultButton1, "Save Changes to Record ???") = vbNo Then
       Me.Undo
      End If
    Else
      If MsgBox("Would You Like To Save This New Record?", vbQuestion + vbYesNo + vbDefaultButton1, "Save This New Record ???") = vbNo Then
       Me.Undo
      End If
    End If
    End Sub


    Or, for a Command Button to dump a New Record or changes to an Existing Record:

    Code:
    Private Sub DumpRecord_Click()
    If Not (Me.NewRecord) Then
     If MsgBox("Are You Sure You Want to Undo the Changes You Have Made?", vbQuestion + vbYesNo + vbDefaultButton2, "Dump Changes to Record ???") = vbYes Then
      Me.Undo
     End If
    Else
     If MsgBox("Are You Sure You Want to Dump this New Record?", vbQuestion + vbYesNo + vbDefaultButton2, "Dump This New Record ???") = vbYes Then
      Me.Undo
     End If
    End If
    End Sub

    Linq ;0)>
    Last edited by Missinglinq; 03-21-13 at 18:50.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    Join Date
    Jan 2013
    Location
    Chesapeake, VA
    Posts
    30
    Thank you Linq, it is not a problem to abort or cancel the record creation of the current open form however: the data used to populate this forms fields comes from foreign tables. I am using the strSQL command to update those fields in the foreign table. If the user cancels I need to go back and correct them but putting the old unchanged values back. I am having two issues:

    1) my on error handler is not handling the user "e****** so I can't correct the changes before I leave the SUB.

    2) When I am no longer in the Sub (ie., in the 2nd or 3rd field of the form) and the user cancels

    Thank again
    Last edited by back2basics; 03-22-13 at 08:46.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •