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?
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?
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.
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.
There is a command - Me.Undo - which will remove any changes made to the data before closing.
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.
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.UndoEnd If
DoCmd.Close acForm, Me.Name
End SubAlternatively 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 acCmdUndoThe 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:
DoCmd.Close acForm, Me.Name
Private Sub buttonNameHere_Click()On Error GoTo buttonNameHere_Click_ErrDoCmd.RunCommand acCmdUndo 'Undoes anything not savedDoCmd.Close acForm, Me.Name 'Closes the formbuttonNameHere_Click_Exit:Exit SubbuttonNameHere_Click_Err: 'If error encounteredMsgBox Error$ 'Whatever error that occurs based on predefined Access error codes will appear here but you can make a custom message if so desiredResume buttonNameHere_Click_Exit 'Close the error message but not the formEnd 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.