By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,164 Members | 1,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,164 IT Pros & Developers. It's quick & easy.

If..Then..loop not working for MsgBox

P: 46
I want to display a message box with Yes, No, Cancel buttons and execute different code depending on which button is clicked.

Expand|Select|Wrap|Line Numbers
  1. strMsg1 = "Click a button"
  3. If MsgBox(strMsg1, vbYesNoCancel) = vbYes Then
  4.   'code A here
  5. ElseIf MsgBox(strMsg1, vbYesNoCancel) = vbNo Then
  6.   'code B here
  7. ElseIf MsgBox(strMsg1, vbYesNoCancel) = vbCancel Then
  8.   'do nothing
  9. End If
What happens is this...user clicks Yes and code A executes. User clicks No and message redisplays, user clicks No again and code B executes. User clicks Cancel, message redisplays, user clicks Cancel again, message redisplays, user clicks Cancel a third time and message finally disappears. It's as if the If...Then statement has to loop twice to get to the second option and three times to get to the third option. Why is this happening...I don't follow the logic.
Sep 29 '08 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 2.5K+
P: 2,545
Hi WyvsEyeView. It's happening because you are repeatedly calling the messagebox function in each test of your IF. Define a variable to hold the result of your message box function and then test that variable with your IF, like this:

Expand|Select|Wrap|Line Numbers
  1. Dim UserChoice as vbMsgBoxResult
  2. UserChoice = MsgBox(strMsg1, vbYesNoCancel)
  3. If UserChoice = vbYes Then
  4.   'code A here
  5. ElseIf UserChoice = vbNo Then
  6.   'code B here
  7. End If
Note that if the user's choice is neither Yes nor No it must be Cancel - testing for that value is redundant. As your code was to do nothing for Cancel I've removed the branch altogether.

By the way, an If..Then structure like this is not a loop, even though your post is titled as such! It can be contained in one, but that is not what you have posted...

Sep 29 '08 #2

P: 46
Perfect! Makes absolute sense once you understand what's happening. Thank you, Stewart. I'm going to look up "loops" to be sure I understand what is and isn't one :)
Sep 29 '08 #3

Expert Mod 15k+
P: 31,660
I would also recommend using the Select Case ... construct in place of If ... ElseIf ... End If.

This could also be another way to avoid your problem, although the fundamental reasoning is now understood so that's less important.
Sep 29 '08 #4

Expert 5K+
P: 8,669
Sorry, but I think NeoPa's point is one worth mentioning:
Expand|Select|Wrap|Line Numbers
  1. Dim strMsg1 As String
  2. strMsg1 = "Click a button"
  4. Select Case MsgBox(strMsg1, vbYesNoCancel)
  5.   Case vbYes
  6.     'code A here
  7.   Case vbNo
  8.     'code B here
  9. End Select
Sep 30 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.