Search code examples
vbaformsms-accessms-office

MS Access pop up and modal form insert record without save button?


Good morning all viewers and MS access expert,

Usually when I make a table have some columns and make a pop-up form to insert records when I start to type in form inputs why if I press (X) button the form insert the record?

I'm looking for the best way to stop form inserting records as long as specific save button not pressed ? for example cancel button or Close(x) discard changes without warning?

Best regards


Solution

  • You need to handle the form's auto update manually.

    To do this, you need a module level boolean variable in the form's code behind file. This will act as flag to stop the form's auto-update.

    Then on your save button you can validate the inputs and set the flag to True to allow the form to be saved.

    'Flag
    Private mIsUserUpdate As Boolean 
    
    'Cancel auto-update
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        If Not mIsUserUpdate Then Cancel = True
    End Sub
    
    'Save button
    Private Sub ButtonSave_Click()
        If Validated then
            mIsUserUpdate = True
            DoCmd.RunCommand acCmdSaveRecord
        End If
    End Sub