Results 1 to 13 of 13
  1. #1
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117

    How to Cancel and Exit a pop up form whether changes were made or not

    Back on my bee hive management system.



    I have a couple instances where I have a form containing a list of records and there is a button that introduces a blank Pop-Up form to add the details of another record to the list. (The original list shows a very condensed version of the entries).

    So, on the Pop-Up form, there are of course all the boxes for information but then I have buttons that are essentially:

    "Save & Exit" that saves the new record and returns to the previous screen

    "Save & Add Another" that saves the record and then prevents another 'new' version of the form for a fresh record

    "Cancel" which is supposed to exit the Pop-Up box and not save the record regardless of whether information has been entered or not.


    If info has not been entered, I can use the macro command 'CloseWindow' and if info has not been entered, I can use the macro command 'UndoRecord' and then 'CloseWindow'.

    I would look to use an IIF / Then condition in the macro but I don't know what the IIF condition needs to be to determine if any of the controls have had information entered.


    Probably overlooking the obvious but I need someone to point it out to me.

    Thanks

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    I don’t use macros (few do) but would have thought to cancel, you just close the form (perhaps with an undo beforehand)

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Could perhaps check if form is Dirty?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Quote Originally Posted by CJ_London View Post
    I don’t use macros (few do) but would have thought to cancel, you just close the form (perhaps with an undo beforehand)
    I tried that but if I try to do an undo with no changes then it returns an error. If I just exit the form after changes, it saves the record when I don't want to.


    Welshgasman, will look into the "dirty" parameter.

    My dependency on macros when I was actively doing things for a former company seems to be a major hindrance for all the stuff I wish to be able to do. I never had the luxury of time to learn the syntax for VBA.


    Gonna end up trying to find some code that I can adapt. Will just take me longer to figure out.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Dirty is a property of the form. Not a parameter.
    Plus you can always test for that error?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    If Me.Dirty Then
    DoCmd.RunCommand acCmdUndo
    DoCmd.Close , ""
    Else
    DoCmd.Close , ""
    End If

  7. #7
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    If Me.Dirty Then
    DoCmd.RunCommand acCmdUndo
    DoCmd.Close , ""
    Else
    DoCmd.Close , ""
    End If

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    I tried that but if I try to do an undo with no changes then it returns an error.
    That should not error. I do it all the time.

    Code:
    Private Sub LCancel_Click()
        Me.Undo
        DoCmd.Close acForm, Me.Name
    End Sub
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Quote Originally Posted by moke123 View Post
    That should not error. I do it all the time.

    Code:
    Private Sub LCancel_Click()
        Me.Undo
        DoCmd.Close acForm, Me.Name
    End Sub

    I should clarify, when doing it in a macro, I get an error. Almost everything I have donme is macro driven. Going to have to break down and start using bits of code. Will take longer because I never had the luxury of time to learn the syntax structure.

  10. #10
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    I was having the same problem as yours ..just type a condition >> [Forms]![Your Form Name].[Dirty]=-1...besides the RunCommand action and it will quit without this error message...Hope it helps
    from /threads/cancle-button-macro.24913/

  11. #11
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Going to have to break down and start using bits of code. Will take longer because I never had the luxury of time to learn the syntax structure.
    It's worth the effort and isn't really as hard as you may think. One way to familiarize yourself is to use the conversion tool on the ribbon.
    Then you can see what your macro looks like in VBA. Try it out with copies of your forms.

    Click image for larger version. 

Name:	convert.jpg 
Views:	17 
Size:	7.3 KB 
ID:	52155
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  12. #12
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Quote Originally Posted by moke123 View Post
    It's worth the effort and isn't really as hard as you may think. One way to familiarize yourself is to use the conversion tool on the ribbon.
    Then you can see what your macro looks like in VBA. Try it out with copies of your forms.

    Click image for larger version. 

Name:	convert.jpg 
Views:	17 
Size:	7.3 KB 
ID:	52155

    I have seen that and gave it a brief consideration. Will probably end up doing that. Need to wait for a rainy day so I can commit some time to it. Right now I'm an hour here, an hour there and I don't have the continuity I need to absorb the new knowledge.

  13. #13
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Got it !!

    Figuring out how to utilize the Dirty property (not parameter) took some experimenting but it ends up being pretty simple.

    An IIF condition where [Forms]![Form_Name].[Dirty]=-1

    An UndoRecord if true and skip that step if false and just close the form.

    Problem solved.

    Thanks for the input. Still gonna have to take some time and learn a little VBA and the convert macros to VB will come in handy

    Now onto the next issue that should be super simple and yet, it is evading me. New post to come.

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

Similar Threads

  1. Exit when press cancel Form
    By Gregory23 in forum Forms
    Replies: 3
    Last Post: 01-10-2019, 03:32 PM
  2. Replies: 2
    Last Post: 09-10-2018, 09:08 AM
  3. Replies: 6
    Last Post: 07-07-2017, 12:23 AM
  4. Replies: 1
    Last Post: 11-17-2016, 10:55 AM
  5. Cancel an entry and exit form
    By michwh1 in forum Access
    Replies: 3
    Last Post: 03-28-2013, 03:13 PM

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