how to write a code to act on the yes and no on a message box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ryno Bower
    New Member
    • Nov 2010
    • 76

    how to write a code to act on the yes and no on a message box

    I have tried many thing and it just would not work.
    Say I have a form with a close button on it. If I click on cancel by accident and the messeage box appears asking if I'm sure I want to close the form, and I don't want to. What coding do I use if I click no the it would just close the msgbox and leave the form open. However if I click yes, it should close the form.

    Can a code be written to do this procedure?
  • malcolmk
    New Member
    • Sep 2010
    • 79

    #2
    You just need to insert the code you want for the condition Vbyes or Vbno.
    Code:
    Private Sub Command0_Click()
    Dim x As Integer
    x = MsgBox("are you sure you want to cancel", vbYesNo)
    If x = vbNo Then
    x = MsgBox("ok don't cancel", vbOKOnly)
    Else
    x = MsgBox("ok cancelling operation as requested", vbOKOnly)
    End If
    
    End Sub

    Comment

    • JenniferM
      New Member
      • Oct 2010
      • 33

      #3
      You could try using a MsgBox here:

      For instance, you could try:

      Code:
      Private Sub YourButtonName_Click()
      
      If MsgBox("Are you sure you want to close this form?", vbYesNo + vbQuestion) = vbYes Then
      
      DoCmd.Close
      
      Else
      Exit Sub
      End If
      
      End Sub
      The code opens up a message box with the question, "Are you sure you want to close this form?"-- you can change this if you wish. If you click yes, the form will be closed. If not, it exits the subroutine. A pretty simple (but useful) fix. Should work for you here, I believe.

      Comment

      Working...