Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

How to close a form without saving data

I have a search form that I want the user to close with a cancel button.  If data is entered I don't want it saved.  I have used the attached code, which works if there is data entered.  But when no data is entered I get a run time error.  How do I enable a user to close the form without saving data on the rare occasion that no data is entered?
Private Sub Cancel_Click()
    DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
    DoCmd.Close acForm, Me.Name
End Sub

Open in new window

0
Harry Batt
Asked:
Harry Batt
  • 3
  • 2
1 Solution
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:


Private Sub Cancel_Click()
    Me.Undo
    DoCmd.Close acForm, Me.Name
End Sub

Or

Private Sub Cancel_Click()
    On Error Resume Next
    Me.Undo
    Err.Clear
    DoCmd.Close acForm, Me.Name
End Sub

mx
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Note however, Undo only applies if data has been entered (Form is Dirty)... BUT ... the user wants to cancel that entry.  If no data has been entered, there is nothing to Undo.  

mx
0
 
Harry BattDirector of DevelopmentAuthor Commented:
DatabaseMX--I understand. So if there is nothing to undo, how do I close the form?  I have disabled the close button. I know it probably has to do with the dirty property, but don't know the proper syntax.  My goal is for the cancel button to work regardless if data has been entered.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
peter57rCommented:
(no points)   You use the second option posted by mx

Or you can do:

If me.dirty then me.undo
DoCmd.Close acForm, Me.Name
0
 
Harry BattDirector of DevelopmentAuthor Commented:
DatabaseMX--Thanks again.  I apologize for not realizing that your comment after you posted the solution was referring to the first option and not both options.  I respect your amazing talent.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Actually, the first solution works fine because Undo does not error if there is nothing to undo ... just wasn't sure last night.

" My goal is for the cancel button to work regardless if data has been entered."
Yes, that's what it does ... doubles as a close button if no data is entered.

thx for the kudos ...

mx
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now