Yes/No/Cancel Dialog Box (1 Viewer)

sdsmith

New member
Local time
Today, 06:20
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:

theDBguy

I’m here to help
Staff member
Local time
Today, 04:20
Joined
Oct 29, 2018
Messages
21,358
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...
 

CJ_London

Super Moderator
Staff member
Local time
Today, 11:20
Joined
Feb 19, 2013
Messages
16,553
please post your code using the code tags - </> to preserve indentation

too late - been beaten to it.....
 

sdsmith

New member
Local time
Today, 06:20
Joined
Oct 29, 2020
Messages
6
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
 

isladogs

MVP / VIP
Local time
Today, 11:20
Joined
Jan 14, 2017
Messages
18,186
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

Top Bottom