Search code examples
vbams-accessms-access-2016

Can I add a UI action confirmation?


I have a form that displays a record (about 40 fields). I have a button to duplicate the record in instances where a slight change is needed (i.e. A541AB becomes A541AC). I've seen a couple of instances of solutions on the interwebs but I couldn't find one that works for this UI action. Is it possible to create a confirmation box asking if they're sure they want to duplicate the record? Currently, the button is designed using a macro since I'm not very good with Access VBA.


Solution

  • You need to do this in VBA by setting a flag to handle the auto-update. Upon clicking the button ask the user and save if the answer is Yes.

    'Set a flag for manual update
    Private mIsUserUpdate As Boolean 'Flag
    
    'Cancel auto-update
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        If Not mIsUserUpdate Then Cancel = True
    End Sub
    
    'Save Button - Change 'YourButtonName'
    Private Sub YourButtonName_Click()
        If MsgBox("Are you sure you want to duplicate the record?", vbYesNo + vbQuestion, "Confirm") = vbYes Then
            mIsUserUpdate = True  'flag ON
            DoCmd.RunCommand acCmdSaveRecord
        End If
        mIsUserUpdate = False 'flag OFF again
    End Sub