vbams-accessbeforeupdate

VBA Code to prompt user to make a selection


I have some VBA code that asks a user if they wish to save a record before they close out of a form in MS Access. This is what the code looks like:

Private Sub Form_BeforeUpdate(Cancel As Integer)


   Dim ctl As Control

   On Error GoTo Err_BeforeUpdate


   If Me.Dirty Then

      If MsgBox("Do you want to save?", vbYesNo + vbQuestion, _
              "Save Record") = vbNo Then
         Me.Undo
      End If
   End If

Exit_BeforeUpdate:
   Exit Sub

Err_BeforeUpdate:
   MsgBox Err.Number & " " & Err.Description
   Resume Exit_BeforeUpdate
End Sub

I would also like to add another message box right after the above, that asks the user if they want to "Copy the record to the Analysis & Support Table?". If yes, than I want the code to run the saved query called "Insert_Query". If no, then go to a new record.

I have very little experience when it comes to coding VBA, hence the need for assistance.

Can anyone help?


Solution

  • If you want the message to appear only if they say yes to save the record then

    If MsgBox("Do you want to save?", vbYesNo + vbQuestion, "Save Record") = vbNo Then
        Me.Undo
    Else
        If MsgBox("Copy the record to the Analysis & Support Table?", vbYesNo + vbQuestion, "Copy Record") = vbYes Then
            CurrentDb.Execute("Insert_Query")
        Else
            'go to new record
        end if
    End If
    

    If you want the message to appear regardless of the selection of the first message box then

    If MsgBox("Do you want to save?", vbYesNo + vbQuestion, "Save Record") = vbNo Then
        Me.Undo
    End If
    If MsgBox("Copy the record to the Analysis & Support Table?", vbYesNo + vbQuestion, "Copy Record") = vbYes Then
        CurrentDb.Execute("Insert_Query")
    Else
        'go to new record
    end if