Results 1 to 6 of 6
  1. #1
    Eranka is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    150

    Closing Form Without Saving Data

    Hi Everyone



    In my application when i exit a form it saves the data automatically in the database. How to stop saving data automatically and exit the form. When exit the form should go back to main menu. How to do this part?

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    you cant. The nature of the form is to save data if it is connected to the table/query.

    If you do NOT want it to save, you must build a form UNBOUND to the table/query.
    This would be a whole lot of extra work to build. (which defeats the purpose of the instant bound form above)

    You need 2 methods: 1 to edit, 1 to add new.
    The new is easy, user clicks a 'new record' button and the form opens with no data.
    The user would fill out the form the press a 'save' button.
    This button would run an append query to add all the new data.

    The Edit button would also load the empty form BUT, it would also load the KEY from the record to edit.
    Then when 'save' is pressed, it must run an update query to alter all fields using the key.

    Like I said, a lot of work to re-invent the wheel that you already have with a bound form.

    if you want to keep user from accidently changing data, use the bound form, lock the form, put a LOCK/UNLOCK button to enable users to edit.
    Last edited by ranman256; 01-02-2018 at 07:55 AM.

  3. #3
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    If it was just on adding a new record, you could just use VBA code to delete the record upon exiting (without saving).
    However, if you were editing an existing record, you really cannot exit it and have it go back to its previous state without using an Unbound Form like ranman suggested.

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    There is a command - Me.Undo - which will remove any changes made to the data before closing.

  5. #5
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Undo will only work if the control or form has not been updated. OP hasn't said what type of form this is. It could be continuous, data sheet or split form. It could involve a subform. It could be a single form with navigation controls. Any action pertinent to the type of form involved, that causes a record to be saved, cannot be undone, right? Closing the form is certainly one of those actions. What the form is based on can also come into play here. If it's a table, setting form properties in concert (such as Allow Additions / Allow Edits / Allow Deletions) might be required. If it's based on a query, making the recordset Snapshot will prevent edits.

    Bottom line might be - read up on form properties to see if these alone will provide the needed protection from edits. The alternatives may be (as noted) recordset types or controlling what mode a form is opened in (from a switchboard or something similar) by locking/disabling controlsOne might be able to prompt to save (or not) changes as long as other actions have not committed the changes, by testing the form Dirty property in the Unload event, which can be cancelled if the user says 'don't save'. Again, it depends on what we're dealing with because form changes might be committed by other actions (especially subforms), which can negate the usefulness of the Dirty property.

    Here's a great source for form properties and events - https://msdn.microsoft.com/en-us/vba...-object-access
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    SierraJuliet's Avatar
    SierraJuliet is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Dec 2017
    Location
    Earth
    Posts
    211
    Try swapping out one of the "Private Sub Form_" listed below and append the remainder of VB that follows to the form using the VBA Editor:

    Private Sub Form_Close()
    Private Sub Form_Unload(Cancel As Integer)
    Private Sub Form_Dirty(Cancel As Integer)
    If Me.Dirty Then
    Me.Undo
    End If
    DoCmd.Close acForm, Me.Name
    End Sub

    Alternatively you can add the italicized information into your form where you have some other action occurring. Likely you will require more than this depending on how you choose to implement.

    Either way I would not want to delete something on exit because I might have a valid record open that I would not want deleted when I closed or quit something in the database. Therefore, I would steer clear of using any sort of delete functionality unless I was certain of what it was doing and the possibilities of how something might go wrong by implementing it.

    Another option is to add the following into your form where the closing action is being performed:
    DoCmd.RunCommand acCmdUndo
    DoCmd.Close acForm, Me.Name

    The following is an example if I were using a button that closed the form and did not want anything saving that was not manually saved prior thereto:

    Private Sub buttonNameHere_Click()
    On Error GoTo buttonNameHere_Click_Err
    DoCmd.RunCommand acCmdUndo 'Undoes anything not saved
    DoCmd.Close acForm, Me.Name 'Closes the form
    buttonNameHere_Click_Exit:
    Exit Sub
    buttonNameHere_Click_Err: 'If error encountered
    MsgBox Error$ 'Whatever error that occurs based on predefined Access error codes will appear here but you can make a custom message if so desired
    Resume buttonNameHere_Click_Exit 'Close the error message but not the form
    End Sub


    If you repeatedly receive some weird error code when clicking the button then either troubleshoot what is causing the error and program a more robust error handling into aforementioned code or do the lazy way of programming by adding "On Error Resume Next" in place of "On Error GoTo ...." and then remove all other items associated with error handling (i.e. "..... Err") from the aforementioned code.
    Last edited by SierraJuliet; 01-03-2018 at 04:56 PM.

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

Similar Threads

  1. Replies: 1
    Last Post: 08-06-2016, 10:39 AM
  2. Replies: 5
    Last Post: 01-27-2016, 07:05 AM
  3. Closing form without saving data
    By Thompyt in forum Programming
    Replies: 5
    Last Post: 03-28-2015, 02:55 PM
  4. Closing the form without saving the information
    By selvakumar.arc in forum Forms
    Replies: 5
    Last Post: 12-11-2013, 08:50 AM
  5. Closing and saving a form
    By Lxmanager in forum Forms
    Replies: 14
    Last Post: 11-21-2010, 02:04 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