Consulting

Results 1 to 3 of 3

Thread: Solved: Select Case statement not working

  1. #1
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location

    Solved: Select Case statement not working

    OK - I've just hit a wall wiht something that should be simple

    From an InputBox statement, I need to determine whether the user:

    1. Clicked OK but did not enter anything
    2. Clicked OK but did not enter a sufficient number of characters
    3. Clicked Cancel

    Here is how I have been trying to write the code:
    [vba]

    strInput = InputBox("Enter something greater than 6 characters.")
    Select Case strInput

    'Check for a blank entry
    Case (Len(strInput) = 0)
    MsgBox "You must enter something."

    'Check that the user entered the minimum length
    Case (Len(strInput) < 6)

    MsgBox "Your entry must be contain at least 6 characters"

    'Check if the user pressed Cancel
    Case ""
    Exit Function

    'If we get here, the string passes
    Case other
    Msgbox "Good job, you can follow directions!"

    End Select
    [/vba]

    I'm sure the problem is simple. Ideas on what I'm doing wrong?

    Thanks,
    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi James,

    The statement ..

    Select Case strInput

    .. says "compare strInput to each of the following Cases".

    Next you have case #1 ..

    Case (Len(strInput) = 0)

    .. so it compares strInput to (Len(strInput) = 0)

    and, whether it is True or False, it is NEVER equal to strInput, so the condition is never fulfilled and it moves on to check the next condition.

    You must either use some other construct, or use ..

    Select Case True

    .. followed by a series of complete conditions.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    I get it! Guess we can mark this one Solved.
    "All that's necessary for evil to triumph is for good men to do nothing."

Posting Permissions

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