Search code examples
ms-accessms-access-2007

How to close a form onclick in Access


In Access 2007, I have a button that opens up a form in dialog mode (modal and popup set to true). I'm trying to save the data from that form and then close that form like so:

Private Sub Close_Click()
    DoCmd.Close acForm, Me.Form, acSaveYes
End Sub

The purpose is to add a new record to a table. It accomplishes this, new data is added to the database based on the inputs. However, the form still stay visible. I have also tried this not using dialog mode, and the result has been the same.


Solution

  • You need to pass DoCmd.Close the form name, not the actual form object.

    Private Sub Close_Click()
        DoCmd.Close acForm, Me.Name, acSaveYes
    End Sub
    

    Also, while you're making changes, add Option Explicit add the top of your module. This would've made it way easier to spot the bug. Read here why having Option Explicit on is a good idea.