Results 1 to 8 of 8

Thread: Cancel InputBox

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    Cancel InputBox

    Hi,

    How can I detect whether the user has pressed Cancel or OK in an inputbox?
    I know that when they press cancel, the function returns an empty string.
    But, what about if the user leaves the textbox blank and presses OK? It still returns an empty string even though Cancel wasn't pressed.

    Anyone?

    Thanks,
    Don't anthropomorphize computers -- they hate it

  2. #2
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim s As String
    3.     s = InputBox("Give!")
    4.    
    5.     If StrPtr(s) = 0 Then MsgBox "Cancel was pressed !"
    6. End Sub

  3. #3
    Addicted Member myrrdan's Avatar
    Join Date
    Nov 2002
    Location
    Alberta Canada
    Posts
    202
    that doesnt seem to work..

    I'm using vb6.


    strSearch = InputBox("Please enter the name for this " & strType & ".", "Enter Name.")
    If strSearch = 0 Then Exit Function


    i need it to be able to determine an empty inputbox, a cancel or an input..

    with the if strSearch = 0, i get a type mismatch...


    forget my comment... i didn't realize that

    StrPtr was an actual recognized command like Cint, or something like that. i had tried a StrCheck(strSearch) first, then just the strSearch, one i put in StrPtr, worked awesome..
    I feel soo stupiddd lol
    Last edited by myrrdan; Mar 18th, 2004 at 03:41 PM.
    Oooops.....theres another semi-colon in the wrong spot....

  4. #4
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871
    Try: If strSearch = vbNullString
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  5. #5

  6. #6
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697
    Is this what you're after??

    VB Code:
    1. Private Sub Form_Load()
    2. Dim Capitals As String
    3.    
    4. Capitals = InputBox("Please Enter Your Name", "Enter Name")
    5.         If StrPtr(Capitals) = 0 Then 'Cancel hit on the Inputbox
    6.        Exit Sub
    7.        ElseIf Capitals = "" Then 'OK is pressed but input box is empty
    8.        'Do rest of code
    9.        Else 'OK pressed with contents entered in input box
    10.        'Do rest of code
    11.        End If
    12.  
    13. End Sub

  7. #7
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Originally posted by TomGibbons
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim s As String
    3.     s = InputBox("Give!")
    4.    
    5.     If StrPtr(s) = 0 Then MsgBox "Cancel was pressed !"
    6. End Sub
    That's a nifty use of StrPtr.

    Nice code!

  8. #8
    Addicted Member myrrdan's Avatar
    Join Date
    Nov 2002
    Location
    Alberta Canada
    Posts
    202
    thanks for the zipfile, will definatly use it.
    Thanks everyone for the hints.

    Now i gotta go back to my other programs LOL
    Oooops.....theres another semi-colon in the wrong spot....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width