locked
Closing a Form in Access w/o saving the record RRS feed

  • Question

  • I want to exit a form in Access 2016 and not save the data.  Can someone plz

    let me know how to do this?  Also, I am trying to save a record and then add

    a new blank record to the screen.  Sometime this logic works and other times it

    is failing and I am getting an error.  Run-Time Error '2105'.  YOu can't go to the

    specified record.

    Private Sub cmdSaveRecord_Enter()
       MsgBox "Record has been successfully Added."

       DoCmd.GoToRecord , , acNewRec
    '  DoCmd.RunCommand (acCmdRecordsGoToNew)
    End Sub

    Monday, December 18, 2017 6:30 PM

Answers

  • Hi,

    The Me.Undo code works except the button was not "wired" to call the [Event Procedure]. It was set to use [Embedded Macro].

    Hope it helps...

    • Marked as answer by ballj_351 Wednesday, December 20, 2017 10:46 AM
    Tuesday, December 19, 2017 8:14 PM

All replies

  • Hi,

    The best event to trap unwanted records is in the Form's BeforeUpdate event. Different approaches can be implemented. For example, you could add a button to explicitly "save" the record by merely flagging it, so that when the BeforeUpdate event fires, you can check the flag if the record should be saved or not.

    When you say the logic only works sometimes, is it on the same form or different situations?

    Monday, December 18, 2017 6:38 PM
  • It is on the same form.  IN the BeforeUpdateEvent, will this be for everything or just when I try to exit?
    Monday, December 18, 2017 7:07 PM
  • Hi,

    The BeforeUpdate event will fire each time a record has changed and is about to be saved, so it will give you a chance to back out.

    Monday, December 18, 2017 7:09 PM
  • Hello,

    As you have posted a new thread for saving records, do you have any issue about closing form and not saving records? If you have any issue, please let us know and share your code and error here. If your issue has been resolved, please mark your solution or helpful post as answer to close this thread.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 19, 2017 7:52 AM
  • I have a challenge with both.  This should be just for exiting the form, so i can go back to the menu.  Even if there is data on the form, I want to click on the button and then exit the form.  Here is the code:

    Private Sub Close_Form_Click()

    On Error GoTo HandleError
    DoCmd.Close ObjectType:=acForm, ObjectName:=Me.Name

    HandleExit:
        Exit Sub
    HandleError:
    '    MsgBox Err.Description
        Resume HandleExit
        
    End Sub

    Tuesday, December 19, 2017 11:28 AM
  • Hi,

    If you're saying you never want to save the form when the Close button is clicked, then you can simply ignore any changes before closing. For example:

    Private Sub Close_Form_Click()

    Me.Undo
    DoCmd.Close acForm, Me.Name

    End Sub

    Hope it helps...

    • Proposed as answer by Chenchen Li Wednesday, December 20, 2017 5:33 AM
    Tuesday, December 19, 2017 4:30 PM
  • Just changed it as you have.  If I have entered anything on the screen and then press the button, I get an error message about a field needs to have input, then a message telling me I cannot save the record at this time.  The objective is to just close the form and if any fields have data, then it is trashed.  I thought this is how it should be working after doing a ton of reading, but it is not.  Any other ideas?
    Tuesday, December 19, 2017 4:39 PM
  • Hi,

    It's all a matter of when and which events are firing and what code you have in them. In this case, it might be simpler if we could see a sample copy of your file to see how all the different parts work together. Are you able to share it?

    Tuesday, December 19, 2017 5:32 PM
  • sure I can do that.  I have Teamviewer if you wish to connect or I could send the db stuff if you want.  How do you want me to do it?
    Tuesday, December 19, 2017 5:49 PM
  • Hi,

    You can email me a copy, and I'll see what I can do. Please tell me which form to check. Thanks.

    Tuesday, December 19, 2017 5:55 PM
  • done.  Let me know if you do not get it.  Thanx
    Tuesday, December 19, 2017 6:06 PM
  • Hi,

    Got it. I'll let you know...

    Tuesday, December 19, 2017 6:20 PM
  • Hi,

    The Me.Undo code works except the button was not "wired" to call the [Event Procedure]. It was set to use [Embedded Macro].

    Hope it helps...

    • Marked as answer by ballj_351 Wednesday, December 20, 2017 10:46 AM
    Tuesday, December 19, 2017 8:14 PM