iixCarbonxZz
iixCarbonxZz

Reputation: 17

If Statement for Function looping incorrectly

I am trying to recreate a simple program I made once in Python in VBScript to have it run in a dialog box.

This is the code:

Set objShell = CreateObject("Wscript.Shell")

Function Query()
    query = InputBox("Please input the Subreddit you with to navigate to e.g (globaloffensive)", "iixCarbonxZz's Subreddit Finder")
End Function

Do
    If query = "" Then
        err = MsgBox("Please enter a Valid Subreddit", vbOKOnly + vbExclamation, "Invalid")
    Else
        objShell.Run("http://www.reddit.com/r/" & query)
        WScript.Quit()
    End If
    Query()
Loop

My problem is this:

The code opens the input text box and if there is nothing in the text box when the OK button is pressed it should bring up the 'err' error message box and then loop. If it gets something in the input box it should use it then close the script.

In practice if I leave the box blank it displays the message, loops back to the input box but then if left blank again skips the if statement and just reloads the input box. If something is input that it can use it skips the if statement and reloads the box the first time and then runs and closes the second time. If prior to this a blank was submitted it will skip the if statement one further time before using the input properly.

Upvotes: 1

Views: 116

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38755

  1. Use a variable to store the function's result
  2. Don't use the same name for the variable and the function

Demo:

Option Explicit

Function getQuery()
    getQuery = InputBox("Please input the Subreddit you with to navigate to e.g (globaloffensive)", "iixCarbonxZz's Subreddit Finder")
End Function

Dim query
Do
    query = getQuery()
    If query = "" Then
        err = MsgBox("Please enter a Valid Subreddit", vbOKOnly + vbExclamation,"Invalid")
    Else
        WScript.Echo "http://www.reddit.com/r/" & query
        WScript.Quit 0
    End If
Loop

Upvotes: 3

Related Questions