Yes/No/Cancel Dialog Box (1 Viewer)

sdsmith

New member
Local time
Yesterday, 21:15
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
Yesterday, 19:15
Joined
Oct 29, 2018
Messages
14,560
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, 03:15
Joined
Feb 19, 2013
Messages
12,926
please post your code using the code tags - </> to preserve indentation

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

sdsmith

New member
Local time
Yesterday, 21:15
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

CID VIP
Local time
Today, 03:15
Joined
Jan 14, 2017
Messages
14,697
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