Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Exit forms without saving

  1. #1
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    153

    Exit forms without saving

    Below is the code for exiting without saving. It works perfectly fine. I just want to know if I can change the MSGBOX message to something like "Are you sure you want to exit without saving?" instead of its default message - "Are you sure you want to delete this record?

    and also, right now, if I click NO when I am asked if I want to delete this record, it exit the form rather than staying in the same form.
    But how do I make it to STAY in the same form if I click NO.

    Thanks...

    Code:
    Private Sub Command24_Click()
    
    
    On Error GoTo Command24_Click_Err
        On Error Resume Next
        DoCmd.RunCommand acCmdUndo
        DoCmd.Close
        DoCmd.OpenForm FormName:="fdlgSearchPatient"
        If (MacroError <> 0) Then
            Beep
            MsgBox MacroError.Description, vbOKOnly, ""
        End If
     
    Command24_Click_Exit:
        Exit Sub
    Command24_Click_Err:
        MsgBox Error$
        Resume Command24_Click_Exit
    
    
    
    
    End Sub

  2. #2
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    Use the following to ask the question.

    Code:
    If MsgBox("Are you sure you want to delete this record? ", vbQuestion + vbYesNo, "Delete Record") = vbNo Then Exit Sub
    Using that If statement you don't need an "End If" after it. Just place that code infront of the code you want to run if yes is pressed. If no is pressed it will "Exit Sub" which means it won't run any more code below this line. If Yes is pressed it skips the "Exit Sub" and continues with any code you wrote for the button.

  3. #3
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    153
    Quote Originally Posted by robsworld78 View Post
    Use the following to ask the question.

    Code:
    If MsgBox("Are you sure you want to delete this record? ", vbQuestion + vbYesNo, "Delete Record") = vbNo Then Exit Sub
    Using that If statement you don't need an "End If" after it. Just place that code infront of the code you want to run if yes is pressed. If no is pressed it will "Exit Sub" which means it won't run any more code below this line. If Yes is pressed it skips the "Exit Sub" and continues with any code you wrote for the button.

    Thanks robsworld!
    it worked but right now it is giving me double message.
    The first one is "are you sure you want to delete.." then comes the second question which I think is part of Access - "Relationships that specify cascading deletes are about to cause 1 record(s) in this table and in related tables to be deleted. Are you sure you want to delete these records?"

    How do I prevent the second question from appearing?

    my code right now is :
    Code:
    Private Sub Command24_Click()
    If MsgBox("Are you sure you want to exit without saving? ", vbQuestion + vbYesNo, "Delete Record") = vbNo Then Exit Sub
    
        DoCmd.RunCommand acCmdUndo
        DoCmd.Close
        DoCmd.OpenForm FormName:="fdlgSearchPatient"
        
    
    
    End Sub

  4. #4
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    What are you trying to do, delete a record or undo a record?

    You command is undo but your question is delete. I'm guessing you have a delete button on a form to delete a record and that record may have other records attached to it because of a relationship so its asking if it should delete all records.

    Are you sure you want the other records related in other tables to be deleted as well? I don't think you can delete a main record without deleted records belonging to it.

    If you do want to delete all records try the following after the question code.

    Code:
    CurrentDb.Execute "DELETE FROM TableName WHERE TableFieldName= " & Me.FormFieldName
    DoCmd.Close

  5. #5
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    I just read properly, you want to undo changes made to a record.

    For that I use a macro, its called "RunMenuCommand" then select "UndoRecord" as command.

    After that add a close window event to the macro to close that form.

  6. #6
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    153
    Quote Originally Posted by robsworld78 View Post
    What are you trying to do, delete a record or undo a record?

    You command is undo but your question is delete. I'm guessing you have a delete button on a form to delete a record and that record may have other records attached to it because of a relationship so its asking if it should delete all records.

    Are you sure you want the other records related in other tables to be deleted as well? I don't think you can delete a main record without deleted records belonging to it.

    If you do want to delete all records try the following after the question code.

    Code:
    CurrentDb.Execute "DELETE FROM TableName WHERE TableFieldName= " & Me.FormFieldName
    DoCmd.Close

    sorry for confusing you. I am actually finding a way to have a button which will exit the form without saving it.
    I read some where that Access saves the record automatically and the only way to exit without saving is to use the UNDO command.

    is it true?

  7. #7
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    yeah, that's true, I couldn't get VBA to work for undo so I used a macro and it works great. You can use the "onclick" event for the macro and "mousedown" or "mouseup" event for any extra VBA you want to run, that's what I've been doing to mix the to, not sure if its correct but it works. I'm no expert so....

  8. #8
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    Is this for an existing record edited or a new record entered and then canceled out?

  9. #9
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    153
    Quote Originally Posted by robsworld78 View Post
    I just read properly, you want to undo changes made to a record.

    For that I use a macro, its called "RunMenuCommand" then select "UndoRecord" as command.

    After that add a close window event to the macro to close that form.

    this works well only for undo right?

    But I want Undo and exit. Will it work?

  10. #10
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    Quote Originally Posted by sk88 View Post
    this works well only for undo right?

    But I want Undo and exit. Will it work?
    Yes it will close the form as well, but you need to add another line to the macro to close the form.

  11. #11
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    153
    Quote Originally Posted by robsworld78 View Post
    Is this for an existing record edited or a new record entered and then canceled out?

    it could be the edited record or a new record entered and then canceled out...

  12. #12
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    Quote Originally Posted by sk88 View Post
    it could be the edited record or a new record entered and then canceled out...
    The macro should handle both

  13. #13
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    153
    Quote Originally Posted by robsworld78 View Post
    The macro should handle both

    THANKS! its working ... just one more thing I would like to add.. not sure if it is possible... right now it just give me a message box "are you sure you want to undo?" with OK button. It does not give me an option to click NO or Cancel and stay.

  14. #14
    robsworld78 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    181
    Create a new macro except create it using the "Create Tab" in the ribbon Then "Macro" don't use the event method. Then save the macro as "Undo" or something and anytime you need to undo a record you can use VBA to call the macro.

    Code:
    DoCmd.RunMacro "Undo"
    In the macro only use "RunMenuCommand" with "UndoRecord" don't add the close window event to the macro or you can't use it for other forms, you can add it in the VBA.

    Then use VBA to run the message box and possibly the macro depending on user input.

    Code:
    If MsgBox("Are you sure you want to delete this record? ", vbQuestion + vbYesNo, "Delete Record") = vbNo Then Exit Sub
    DoCmd.RunMacro "MACRONAME"
    DoCmd.Close

  15. #15
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    153
    Quote Originally Posted by robsworld78 View Post
    Create a new macro except create it using the "Create Tab" in the ribbon Then "Macro" don't use the event method. Then save the macro as "Undo" or something and anytime you need to undo a record you can use VBA to call the macro.

    Code:
    DoCmd.RunMacro "Undo"
    In the macro only use "RunMenuCommand" with "UndoRecord" don't add the close window event to the macro or you can't use it for other forms, you can add it in the VBA.

    Then use VBA to run the message box and possibly the macro depending on user input.

    Code:
    If MsgBox("Are you sure you want to delete this record? ", vbQuestion + vbYesNo, "Delete Record") = vbNo Then Exit Sub
    DoCmd.RunMacro "MACRONAME"
    DoCmd.Close

    Thanks Rob! Its working but giving me the same problem like before. It shows me two messages. One is ARE YOU SURE YOU WANT TO DELETE THIS RECORD? and the next one is the default by Access I think... can I get rid of that message?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. exit
    By slimjen in forum Forms
    Replies: 4
    Last Post: 10-09-2011, 08:30 AM
  2. Replies: 23
    Last Post: 02-09-2011, 08:56 AM
  3. Replies: 5
    Last Post: 05-24-2010, 10:52 AM
  4. Saving forms as Data Access Pages
    By frowsyone in forum Access
    Replies: 1
    Last Post: 03-08-2010, 06:25 AM
  5. Saving records in multiple sub forms
    By niak32 in forum Forms
    Replies: 0
    Last Post: 10-13-2008, 03:24 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
  •  
Tech Forums: Microsoft Office Forums