locked
How to Detect vba InputBox "Cancel" or "X" clicked other than detecting null input ? RRS feed

  • Question

  • Code below detects Cancel or "X" clicked for MsgBox.

    How can you do this for an InputBox other than detecting "" ?

    Thanks for any help.

     

    Dim iAnswer As Integer

        iAnswer = MsgBox(Prompt:="Click Yes or No or Cancel or X to see Demo", _            Buttons:=vbYesNoCancel, Title:="Demo Cancel")

            If iAnswer = vbYes Then
            MsgBox "You Clicked Yes"
            Exit Sub    

        ElseIf iAnswer = vbNo Then
           MsgBox "You Clicked No"
           Exit Sub

       Else 'They cancelled (VbCancel)
            MsgBox "You Canceled"
            Exit Sub

        End If

     

     

    Monday, September 12, 2011 3:34 PM

Answers

  • I don't know if the following works or not.  I've never tested it, but got it on line several years ago.

    Detect in user pressed CANCEL while input box is displayed.

    Dim s as String
    s = InputBox ("What do you want to find?")
    If strptr (s) = 0 then
         ' user cancelled.
    else
        If Len(S) = 0 then
            'user Pressed OK, but there was no entry in the input control
       Else
           'User pressed OK and there was something in the input control
       End if
    endif


    John Spencer Access MVP 2002-2005, 2007-2011
    • Marked as answer by Bruce Song Thursday, September 22, 2011 8:22 AM
    Monday, September 12, 2011 5:43 PM
  • Mel_3,

    You need to understand the built-in function. If you cancel the input, it will return a Zero lenght string. The Cancel button return an empty string. There is no Args for the Cancel button. So you cannot detect the Cancel button being press by the user.

    So what John. P Spencer gave would work for you if you're wanting to use the built-in fucntion else follow what Stefan suggest...do your own custom inputbox.

    • Marked as answer by Bruce Song Thursday, September 22, 2011 8:22 AM
    Tuesday, September 13, 2011 1:22 AM
  • You can't. Either you live with it or you implement your own.
    • Marked as answer by Bruce Song Thursday, September 22, 2011 8:22 AM
    Monday, September 12, 2011 4:12 PM

All replies

  • You can't. Either you live with it or you implement your own.
    • Marked as answer by Bruce Song Thursday, September 22, 2011 8:22 AM
    Monday, September 12, 2011 4:12 PM
  • I don't know if the following works or not.  I've never tested it, but got it on line several years ago.

    Detect in user pressed CANCEL while input box is displayed.

    Dim s as String
    s = InputBox ("What do you want to find?")
    If strptr (s) = 0 then
         ' user cancelled.
    else
        If Len(S) = 0 then
            'user Pressed OK, but there was no entry in the input control
       Else
           'User pressed OK and there was something in the input control
       End if
    endif


    John Spencer Access MVP 2002-2005, 2007-2011
    • Marked as answer by Bruce Song Thursday, September 22, 2011 8:22 AM
    Monday, September 12, 2011 5:43 PM
  • Mel_3,

    You need to understand the built-in function. If you cancel the input, it will return a Zero lenght string. The Cancel button return an empty string. There is no Args for the Cancel button. So you cannot detect the Cancel button being press by the user.

    So what John. P Spencer gave would work for you if you're wanting to use the built-in fucntion else follow what Stefan suggest...do your own custom inputbox.

    • Marked as answer by Bruce Song Thursday, September 22, 2011 8:22 AM
    Tuesday, September 13, 2011 1:22 AM
  • So so so wrong. As stated below, use strptr().

    An MVP should know that...

    Friday, July 29, 2016 1:12 PM