Results 1 to 13 of 13
  1. #1
    benjammin is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Nov 2010
    Posts
    60

    do NOT save record on close (x button)

    Hi,



    This should be simple as I'm stumped easily...

    User clicks the X (close) button on a form in the upper-right hand corner and it closes. If the user has entered anything (dirty?) into the form then it writes a record to the table.

    When the user closes the form by the x button in the upper right how do i make the form NOT save the record?

    Thanks

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,545
    Hi

    If you stop the edits from being saved, How will you be able to save changes when they are needed?
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    benjammin is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Nov 2010
    Posts
    60
    Well i have buttons to save and add another, save and close and a cancel (undo) button but if the user clicks the close (x) button, alt + f4, etc... it will save what they've partially done and I don't want that.

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,545
    Hi

    Why not remove the red cross from your form?
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    benjammin is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Nov 2010
    Posts
    60
    red cross?

    I don't want to remove the white/black X because it confuses users when there is no X up there. Plus the issue of people alt-f4'ing/ctrl-w'ing etc... will still produce the same result.

  6. #6
    benjammin is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Nov 2010
    Posts
    60
    nevermind, i just give the user the option to save record if dirty.

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.Dirty Then
    If MsgBox("Would you like to save this record?", vbOKCancel, "Save?") = vbCancel Then
    Me.Undo
    End If
    End If
    End Sub
    The biggest problem with this is it asks the user to save? even if they press the save and add new record button I have, or the save and close button, essentially it's redundant...

    What do I do! I only want this question asked when they close the form with alt-f4, x button, ctrl-w, etc...

  7. #7
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by benjammin View Post
    nevermind, i just give the user the option to save record if dirty.

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.Dirty Then
    If MsgBox("Would you like to save this record?", vbOKCancel, "Save?") = vbCancel Then
    Me.Undo
    End If
    End If
    End Sub
    The biggest problem with this is it asks the user to save? even if they press the save and add new record button I have, or the save and close button, essentially it's redundant...

    What do I do! I only want this question asked when they close the form with alt-f4, x button, ctrl-w, etc...

    The solution is to use a flag. Create a boolean flag at the top of the form's module:
    Code:
    Private blnSave As Boolean
    Then, in the click event of the buttons to save or add new record use:
    Code:
    blnSave = True
    and then in the BeforeUpdate event (The If Me.Dirty is redundant because the BeforeUpdate event will not fire unless the form is dirty):
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer) 
       If Not blnSave Then
         If MsgBox("Would you like to save this record?", vbOKCancel, "Save?") = vbCancel Then
            Cancel = True
            Me.Undo 
      End If
    End Sub
    And so then it will not ask them if they want to save if they have clicked the button(s) and it will ask them the question ONLY if they haven't clicked the button.

  8. #8
    benjammin is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Nov 2010
    Posts
    60
    Thanks that's it and almost there... one little thing, how do i suppress this warning? they've already said they don't want to save it and it's obvious they want to close the object...

  9. #9
    nicknameoscar is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    Apr 2011
    Location
    Earlysville, VA
    Posts
    91
    I am not sure I am totally following what has been done already but are you familiar with this:

    DoCmd.RunCommand acCmdUndo

    My memory is not the best but I seem to remember having a similar problem with Me.Undo and solved it using the above. Just a thought.

  10. #10
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by benjammin View Post
    Thanks that's it and almost there... one little thing, how do i suppress this warning? they've already said they don't want to save it and it's obvious they want to close the object...
    Where have they said that they don't want to save it? Using the message box here in this code? Or is there another one in the button that they can click to cancel? If you have the message box there too, then you can probably just get rid of the message box code in the before update event.

  11. #11
    benjammin is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Nov 2010
    Posts
    60
    when they close out this runs:

    Code:
    If MsgBox("Would you like to save this record?", vbOKCancel, "Save?") = vbCancel Then Cancel = True Me.Undo
    so you say save or cancel there, and then when you hit cancel so you don't save it pops up that error window

  12. #12
    accessorizer2 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2012
    Posts
    1
    Quote Originally Posted by benjammin View Post
    when they close out this runs:

    Code:
    If MsgBox("Would you like to save this record?", vbOKCancel, "Save?") = vbCancel Then Cancel = True Me.Undo
    so you say save or cancel there, and then when you hit cancel so you don't save it pops up that error window

    This is exactly what I have tried for trapping the required field error. I have tried all the closing events, deactivate, close, afterupdate...I'm probably missing a few. I've even tried onError, though that was a last-ditch effort.

    I thought this would work too, until I tried the programmed form within my Navigational Form. No longer works, sure it gets rid of the save record error, but it doesn't get rid of this error: "The Microsoft Accses database engine cannot find a record in the table '' with key matching fields(s) ''."

    I get this isn't exactly a "required field" issue, but a look-up field issue....still can't seem to find a way to suppress it.

    To be slightly more clear my error trapping works exactly how I want it to, if the required fields are blank it asked if I want to discard the record or return to edit it, and those actions work perfectly except that you get that ^ annoying message after either MsgBox option....

    Anyway to suppress that error? Is it just a quirk of lookup fields being a pain?

  13. #13
    Cello is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2013
    Posts
    1
    Quote Originally Posted by benjammin View Post
    Thanks that's it and almost there... one little thing, how do i suppress this warning? they've already said they don't want to save it and it's obvious they want to close the object...
    If you're still looking for a solution on this, just came up with a very simple one.
    In the On Error event of the form use this code:
    Code:
    Private Sub Form_Error(DataErr As Integer, Response As Integer)
        Response = acDataErrContinue
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Can't save or close form
    By Buakaw in forum Forms
    Replies: 4
    Last Post: 07-26-2011, 04:47 AM
  2. Access Form- Save/Close Button error
    By Ashe in forum Forms
    Replies: 3
    Last Post: 02-10-2011, 01:01 PM
  3. Prompt on close and don't save the null record
    By hasanrazaj1 in forum Forms
    Replies: 2
    Last Post: 10-24-2010, 09:24 AM
  4. form won't save and close
    By Philislost in forum Access
    Replies: 6
    Last Post: 10-08-2010, 01:47 PM
  5. Replies: 2
    Last Post: 01-29-2010, 11:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums