Results 1 to 5 of 5
  1. #1
    School Boy Error is offline Advanced Beginner
    Windows 7 32bit Access 2013 64bit
    Join Date
    Oct 2012
    Posts
    72

    Error when closing form

    Hi,



    I have created a form but when the user clicks the x button I want it just to unload and not save anything. I have included this code in the VBA module but if I click close and some of the combo boxes are empty it still comes up with an error.

    Code:
    Private Sub close_form_Click()
    
    On Error Resume Next
    
    
    Dim ctl As Control
    For Each ctl In Me.Controls
        If ctl.ControlType = acTextBox Then ctl = Null
    Next
    Set ctl = Nothing
    DoCmd.Close
    
    
    End Sub

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    Code:
    Private Sub close_form_Click()    
        'On Error Resume Next
        Dim ctl As Control
        Me.Undo
        For Each ctl In Me.Controls
            If ctl.ControlType = acTextBox Then ctl = Null
        Next
        Set ctl = Nothing
        DoCmd.Close
    End Sub
    Last edited by davegri; 01-26-2019 at 08:44 AM. Reason: format

  3. #3
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    It's often easier to set the forms Close button and/or Control box to no and include a cancel button to undo and close without saving.

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    Need a bit more information from OP.
    Notice that if the red x is clicked, the close_form_click event is not executed.
    Also, the OP might want to differentiate the action taken for an existing record vs a new record?
    Last edited by davegri; 01-26-2019 at 09:39 AM. Reason: N/A, more info needed

  5. #5
    School Boy Error is offline Advanced Beginner
    Windows 7 32bit Access 2013 64bit
    Join Date
    Oct 2012
    Posts
    72
    Code:
    Private Sub Form_Close()
    
        Me.Undo
    
    
    End Sub
    This fixed it!! So simple when you know how - really difficult when you don't know what to look for! Thank you for the pointers!

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

Similar Threads

  1. Error closing form after exporting graph to gif
    By BrettAltea in forum Access
    Replies: 9
    Last Post: 07-14-2021, 09:12 AM
  2. Replies: 2
    Last Post: 04-20-2017, 02:51 PM
  3. Error Message When Closing a Form w/ Macro
    By KCC47 in forum Programming
    Replies: 8
    Last Post: 11-14-2014, 03:35 PM
  4. closing report pop-up error
    By ronni in forum Reports
    Replies: 1
    Last Post: 07-27-2014, 10:11 PM
  5. Replies: 5
    Last Post: 12-22-2011, 01:12 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