Yes/No/Cancel Dialog Box

sdsmith

New member
Local time
Today, 17:37
Joined
Oct 29, 2020
Messages
6
I can not get my dialog box to perform correctly. Right now, I'm using the code below. The Yes button saves and the No button clears everything out. I want the Cancel button to do nothing and just return to the same page with no changes. How can I correct this?

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
   Dim strMsg As String
   Dim iResponse As Integer
 
   ' Specify the message to display.
   strMsg = "Do you wish to save the changes?" & Chr(10)
   strMsg = strMsg & "Click Yes to Save or No to Discard changes."
 
   ' Display the message box.
   iResponse = MsgBox(strMsg, vbQuestion + vbYesNoCancel, "Save Record?")
    
   ' Check the user's response.
   If iResponse = vbNo Then
    
      ' Undo the change.
      DoCmd.RunCommand acCmdUndo
 
      ' Cancel the update.
      Cancel = True
   End If
End Sub
 
Last edited:
Hi. If you use the code tag when you post, you can preserve the indentation of your code, and it would be easier to follow/read.

When you check for the response, you need to check for at least two of them. For example:
Code:
If iResponse=vbNo Then
  ...
ElseIf iResponse=vbCancel Then
  ...
Else
  ...
End If
Hope that helps...
 
please post your code using the code tags - </> to preserve indentation

too late - been beaten to it.....
 
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
   Dim strMsg As String
   Dim iResponse As Integer
 
   ' Specify the message to display.
   strMsg = "Do you wish to save the changes?" & Chr(10)
   strMsg = strMsg & "Click Yes to Save or No to Discard changes."
 
   ' Display the message box.
   iResponse = MsgBox(strMsg, vbQuestion + vbYesNoCancel, "Save Record?")
    
   ' Check the user's response.
   If iResponse = vbNo Then
    
      ' Undo the change.
      DoCmd.RunCommand acCmdUndo
 
      ' Cancel the update.
      Cancel = True
   End If
End Sub
 
The code you have only has 2 choices so you need either vbYesNo or vbOKCancel

Otherwise perhaps modify to something like:
Code:
  ' Check the user's response.
   If iResponse = vbYes Then
      'save code here   
   ElseIf iResponse = vbNo Then
    
      ' Undo the change.
      DoCmd.RunCommand acCmdUndo
 
      ' Cancel the update.
      Cancel = True
   Else 'user clicked cancel
      Exit Sub
   End If
 

Users who are viewing this thread

Back
Top Bottom