locked
End Script with No Entry on Select Case Input RRS feed

  • Question

  • Hello,

    Is there a way to end a VBS script if the user enters nothing into the input box?  I have try Case Else but that did work.

    Any recommendations?

    Thanks, CAZan


    • Edited by CAZan Sunday, June 11, 2017 6:07 AM
    • Moved by Dave PatrickMVP Sunday, June 11, 2017 12:40 PM
    Sunday, June 11, 2017 5:56 AM

Answers

  • Hi CAZan,

    You should add these lines before Select Case:
    If Question = empty then
      WScript.Quit
    End if

    Question = InputBox(message,Title,Default)
    
    if Question = empty then
    	WScript.Quit
    end if
    
    Select Case Question 
    	Case 1
    		strAlgOpt = "MD5"
    	Case 2
    		strAlgOpt = "SHA1"
    	Case 3
    		strAlgOpt = "SHA256"
    	case else
    		WScript.Quit
    End Select

    Good luck!


    Ashidacchi

    • Marked as answer by CAZan Wednesday, June 14, 2017 7:14 PM
    Tuesday, June 13, 2017 2:23 AM

All replies

  • Dear Dave Patrick,

    Why did you move this thread to JScript?

    OP mentioned "VBS script".


    Ashidacchi

    Sunday, June 11, 2017 4:04 PM
  • Thanks, Ashidacchi.

    Yes, my Select Case question is not regarding Java Script.  It is VBScript, Windows Script Host environment.

    Can you please move it back to the VBScript forum where it belongs?

    Oops, realized I incorrectly typed one of my sentences.  The Case Else did not work.  I tried an example found in the Microsoft WSH and VBScript Programming for the Absolute Beginner, 4th ed.

    Select Case strAnswer
      Case "rock"
        If intGetRandomNumber = 3 Then strResults = "Tie"
        If intGetRandomNumber = 2 Then strResults = "You Win"
        If intGetRandomNumber = 1 Then strResults = "You Lose"
      Case Else
        objWshShell.Popup "Sorry. Your answer was not recognized. " & "Please type rock, paper, or scissors in all lowercase letters."
        WScript.Quit
    End Select

    Thanks, CAZan



    • Edited by CAZan Sunday, June 11, 2017 11:05 PM
    Sunday, June 11, 2017 10:55 PM
  • Hi CAZan,

    I don't use VBScript, but could guess algorithm:
      1. check input errors 
      2. then use "Select Case"
    Here's a sample code (correct it according to VBScript rule since I don't know syntax of VBScript)
    If (intGetRandomNumber >= 1) and (intGetRandomNumber <= 3) then
      strResults = "input 1, 2, or 3"
      Exit Sub	'-- or WScript.Quit
    End If
    
    If (strAnswer = "rock") or (strAnswer = "paper") or (strAnswer = "scissors") then
      objWshShell.Popup "Sorry. Your answer was not recognized. " & "Please type rock, paper, or scissors in all lowercase letters."
      WScript.Quit
    End If 
    
    Select Case strAnswer
      Case "rock"
        Select Case intGetRandomNumber
          Case 3	' -- 3: rock  
            strResults = "Tie"
          Case 2	' -- 2: scissors
            strResults = "You Win"
          Case 1	' -- 1: paper
            strResults = "You Lose"
    	End Select
      Case "paper"
        Select Case intGetRandomNumber
          Case 3	' -- 3: rock  
            strResults = "You Win"
          Case 2	' -- 2: scissors
            strResults = "You Lose"
          Case 1	' -- 1: paper
            strResults = "Tie"
    	End Select
      Case "scissors"
    	Select Case intGetRandomNumber
          Case 3	' -- 3: rock  
            strResults = "You Lose"
          Case 2	' -- 2: scissors
            strResults = "Tie"
          Case 1	' -- 1: paper
            strResults = "You Win"
      End Select
    End Select
     

    Ashidacchi

    Sunday, June 11, 2017 11:48 PM
  • Hi Ashidacchi,

    Is this topic in the VBScripts area?  It certainly isn't Java about which I am asking.

    I did some testing and can get it to quit if I enter in any thing other than 1,2 and 3.  But, enter nothing causes it to default to the first item "MD5".

    Question = InputBox(message,Title,Default)
    Select Case Question 
    	Case 1
    		strAlgOpt = "MD5"
    	Case 2
    		strAlgOpt = "SHA1"
    	Case 3
    		strAlgOpt = "SHA256"
    	Case Else
    		WScript.Quit
    End Select

    If I use an IF THEN after the Case Else to quit with nothing entered into the input box, it seems to ignore it.  I used the test for Question = "".


    Thanks, CAZan

    Monday, June 12, 2017 11:41 PM
  • Hi CAZan,

    You should add these lines before Select Case:
    If Question = empty then
      WScript.Quit
    End if

    Question = InputBox(message,Title,Default)
    
    if Question = empty then
    	WScript.Quit
    end if
    
    Select Case Question 
    	Case 1
    		strAlgOpt = "MD5"
    	Case 2
    		strAlgOpt = "SHA1"
    	Case 3
    		strAlgOpt = "SHA256"
    	case else
    		WScript.Quit
    End Select

    Good luck!


    Ashidacchi

    • Marked as answer by CAZan Wednesday, June 14, 2017 7:14 PM
    Tuesday, June 13, 2017 2:23 AM
  • Ashidacchi,

    I had a sneaking suspicion I had to put that outside of the Select Case.  Thinking a little more about how that works, it makes total sense to put it before the Select Case.

    With it laid out like that it allow me to cover the various end user areas.

    Without further ado, that works perfectly.  Thanks!


    Thanks, CAZan


    • Edited by CAZan Wednesday, June 14, 2017 7:16 PM Addendum
    Wednesday, June 14, 2017 7:14 PM