Results 1 to 7 of 7
  1. #1
    4 Star Lounger
    Join Date
    Jun 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    527
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Input Box - 'Cancel' (A2k2)

    I am using an input box to get an e-mail address from a user. If the user selects "Cancel" from the buttons available on the form, it just keeps right on going in the routine. Is there a value that gets returned from the cancel button similar to a MSGBOX control? I want it to basically exit the routine.

    Thanks,
    Mark

  2. #2
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Input Box - 'Cancel' (A2k2)

    The InputBox function returns a string. If user hits "Cancel", presses ESC key, or selects "OK" w/o entering any text, the function will return a zero-length string. The Len() function is simplest way to test to see if response received from user. Example:

    Public Sub TestInputBoxFunction()

    Dim strResponse As String

    strResponse = InputBox("What is your name?", "ENTER NAME")

    If Len(strResponse) = 0 Then
    ' User cancelled or pressed ESC:
    MsgBox "No response.", vbInformation, "NO RESPONSE"
    Else
    ' User entered some text:
    MsgBox "Hello " & strResponse, vbInformation, "RESPONSE"
    End If

    End Sub

    Whether the response, if rcvd, constitutes a valid e-mail address would be a separate issue.

    HTH

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Input Box - 'Cancel' (A2k2)

    If the user clicks "Cancel", the return value of the InputBox function is an empty string "". So you could test like this:

    Dim strResult As String
    strResult = InputBox("Enter e-mail address")
    If strResult = "" Then Exit Sub
    ...

  4. #4
    4 Star Lounger
    Join Date
    Jun 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    527
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Input Box - 'Cancel' (A2k2)

    Thanks Guys!

    The test for "" is the best solution for my situation.

    Thanks,
    Mark

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Input Box - 'Cancel' (A2k2)

    It's the same test. A Null string ("") has a length of zero.
    Charlotte

  6. #6
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Input Box - 'Cancel' (A2k2)

    I got in habit of using Len() to test for zero-length strings because (as noted in ADH and elsewhere) it is less "work" for VBA to look up the length of the string, which is stored in the 4 bytes preceding the actual byte array of characters, than to compare a string to "" or some other value. Although in this case any increase in "efficiency" would be neglible if not nonexistent. Efficiency aside, I tend to avoid using "" where practical because it makes the code more readable (e.g., using vbNullString to initialize strings, etc).

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Input Box - 'Cancel' (A2k2)

    I generally use that syntax as well, although my preferred method is If Len(Me.ControlName & "") > 0 Then, which handles Nulls as well.
    Charlotte

Posting Permissions

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