PDA

View Full Version : Solved: How to code a Yes/No MsgBox under a command button!



jamieCR9
09-25-2009, 08:20 AM
Hey I am new to these forums. Can I ask, how do I code the following:Sub Msgbox_Yes_No()
Dim Response As Integer

' Displays a message box with the yes and no options.
Response = MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo)

' If statement to check if the yes button was selected.
If Response = vbYes Then
MsgBox "Proceed to build your laptop."
ActivePresentation.SlideShowWindow.View.Next
Else
' The no button was selected.
MsgBox "Please pick the other option"
End If
End Sub
under a command button? I tried to copy and paste the following code into the command button but came up with an error.

This is what I have been typing in:
Private Sub CommandButton1_Click()
Sub Msgbox_Yes_No()
Dim Response As Integer

' Displays a message box with the yes and no options.
Response = MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo)

' If statement to check if the yes button was selected.
If Response = vbYes Then
MsgBox "Proceed to build your laptop."
ActivePresentation.SlideShowWindow.View.Next
Else
' The no button was selected.
MsgBox "Please pick the other option"
End If
End Sub
End Sub
What do I need to fix? Thanks. :)

Cosmo
09-25-2009, 10:18 AM
Either:
Private Sub CommandButton1_Click()
Dim Response As Integer

' Displays a message box with the yes and no options.
Response = MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo)

' If statement to check if the yes button was selected.
If Response = vbYes Then
MsgBox "Proceed to build your laptop."
ActivePresentation.SlideShowWindow.View.Next
Else
' The no button was selected.
MsgBox "Please pick the other option"
End If
End Sub

or:
Private Sub CommandButton1_Click()
Msgbox_Yes_No
End Sub
Sub Msgbox_Yes_No()
Dim Response As Integer

' Displays a message box with the yes and no options.
Response = MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo)

' If statement to check if the yes button was selected.
If Response = vbYes Then
MsgBox "Proceed to build your laptop."
ActivePresentation.SlideShowWindow.View.Next
Else
' The no button was selected.
MsgBox "Please pick the other option"
End If
End Sub

if you have multiple buttons you want to run the same Msgbox_Yes_No code.

jamieCR9
09-25-2009, 10:59 AM
Either:
Private Sub CommandButton1_Click()
Dim Response As Integer

' Displays a message box with the yes and no options.
Response = MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo)

' If statement to check if the yes button was selected.
If Response = vbYes Then
MsgBox "Proceed to build your laptop."
ActivePresentation.SlideShowWindow.View.Next
Else
' The no button was selected.
MsgBox "Please pick the other option"
End If
End Sub
or:
Private Sub CommandButton1_Click()
Msgbox_Yes_No
End Sub
Sub Msgbox_Yes_No()
Dim Response As Integer

' Displays a message box with the yes and no options.
Response = MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo)

' If statement to check if the yes button was selected.
If Response = vbYes Then
MsgBox "Proceed to build your laptop."
ActivePresentation.SlideShowWindow.View.Next
Else
' The no button was selected.
MsgBox "Please pick the other option"
End If
End Sub
if you have multiple buttons you want to run the same Msgbox_Yes_No code.
Top guy, works a treat. :beerchug:

John Wilson
10-06-2009, 10:37 AM
It does but you can streamline it to

If MsgBox(prompt:="Are you sure you want to continue with your choice? You can't go back after your choice.", Buttons:=vbYesNo) = vbYes Then
'Go on
Else: 'Quit
End If

jamieCR9
10-06-2009, 04:29 PM
Thanks John, do you think you can answer this query for me too? I don't have enough posts to insert a link, so it is called something along the lines of 4 options in a dropdown menu.

Much appreciated. :)