ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   OK/Cancel box functionality? (https://www.excelbanter.com/excel-discussion-misc-queries/220344-ok-cancel-box-functionality.html)

sycsummit

OK/Cancel box functionality?
 
I have a snippet of code attached to a button so that when you click the
button the workbook is emailed to a specified email address. However, I
would like to add a confirmation popup before any code is executed. I've
gotten as far as:

Dim response As Long
response = MsgBox(prompt:="Are you sure you would like to submit?",
Buttons:=vbOKCancel)

When I click OK, the code executes as it should. However, when I click
cancel, the code still executes. Can't figure out how to code for cancel.
I want it to do nothing, and simply return to the form on which the button is
embedded. How do I do this?

JLatham

OK/Cancel box functionality?
 
A couple of ways to do it. Going along with what you've started, you need to
test the contents of 'response'

Dim response As Long
response = MsgBox(prompt:="Are you sure you would like to submit?",
Buttons:=vbOKCancel)
If response < vbOK Then
Exit Sub
End If
.... code to send the file goes here

or a more abbreviated version that doesn't have to use the added variable
'response':

If MsgBox("Are you sure you would like to submit?", _
vbOKCancel+vbQuestion,"Confirm Emailing") < vbOK Then
Exit Sub
End If
....

"sycsummit" wrote:

I have a snippet of code attached to a button so that when you click the
button the workbook is emailed to a specified email address. However, I
would like to add a confirmation popup before any code is executed. I've
gotten as far as:

Dim response As Long
response = MsgBox(prompt:="Are you sure you would like to submit?",
Buttons:=vbOKCancel)

When I click OK, the code executes as it should. However, when I click
cancel, the code still executes. Can't figure out how to code for cancel.
I want it to do nothing, and simply return to the form on which the button is
embedded. How do I do this?



All times are GMT +1. The time now is 11:01 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com