VBA get value returned by MsgBox

Solved
Code Jack Posted messages 18 Status Member -  
 Yop73 -
Hello everyone,

I'm unable to retrieve the value returned by a MsgBox into a variable myChoice. I need to know if the user wants to exit the step-by-step procedure...

Could you help me?

Thank you in advance,

Code Jack

Configuration: Windows 7 / Firefox 5.0

2 answers

  1. pijaku Posted messages 13513 Registration date   Status Moderator Last intervention   2 773
     
    Hello,
    It all depends on your MsgBox...
    Example with a "yes - no" message box:
    Sub test() Dim result As String, MyChoice As String result = MsgBox("Make your choice!", vbYesNo) If result = vbYes Then MyChoice = "Choice 1: Yes" Else MyChoice = "Choice 2: No" End If MsgBox MyChoice End Sub

    --
    Best regards,
    Franck P
    10
    1. Yop73
       
      You're welcome!
      0
  2. Code Jack Posted messages 18 Status Member 7
     
    That is exactly the type of request I have to make, thank you.

    But the specific help regarding MsgBox:

    "Returned values:

    Constant, Value, Description
    vbOK, 1, OK
    vbCancel, 2, Cancel
    vbAbort, 3, Abort
    vbRetry, 4, Retry
    vbIgnore, 5, Ignore
    vbYes, 6, Yes
    vbNo, 7, No"

    I thought I needed to retrieve an integer in myChoice...

    But if not, that's fine, I can work with your suggestion.

    Thank you very much,

    Code Jack
    0
    1. pijaku Posted messages 13513 Registration date   Status Moderator Last intervention   2 773
       
      You can, you can...
      It was just a suggestion.
      Here, take this test:
      Dim result As Integer result = MsgBox("click", vbYesNoCancel) MsgBox result

      returns 6 when you click "yes", 7 when you click "no" and 2 when you click "cancel"
      It's up to you!!!
      -1
    2. Code Jack Posted messages 18 Status Member 7
       
      This is interesting, though!

      You store two different things depending on how you introduce your variable in your two examples.
      MsgBox seems to return both string and integer, depending on where we store it... Strange

      Thank you very much nonetheless!

      Code Jack
      0
    3. pijaku Posted messages 13513 Registration date   Status Moderator Last intervention   2 773
       
      What you raise is even more interesting to me because I've never asked myself the question and I've just realized that... I was completely wrong!!!!
      MsgBox always returns an Integer... vbYes, for example, is a VBA constant equal to 6.
      The mistake I've been making all along is automatically handled by VBA...
      Dim result As String while actually result = 6 is processed automatically (I read it somewhere but where...???).
      Sources: MsgBox
      0
    4. Code Jack Posted messages 18 Status Member 7
       
      Well there you go, mystery solved ;) !

      Code Jack
      0