Search code examples
loopsvbscriptinputboxdo-loops

How to make a vbscript loop in loop go to start of code


When running this code;

varCheck=True
Do While varCheck
    Pass=InputBox("Enter Password")
    Do
        If IsEmpty(pass) Then
            WScript.quit
            Exit Do
        End If
        If Pass = "123" Then
            varCheck=False
            Exit Do
        Else
            varCheck=True
            MsgBox("Wrong Password...Try Again")
        End If
    Loop
Loop

If the password is wrong then it doesn't restart to the top of the code, it just endlessly loops the "Wrong Password...Try Again" message box. How do I make it ask the password again? (p.s. I'm a newbie at coding so please explain yourself. Thanks!) WARNING! As I said before, if you get the password wrong, it endlessly loops the wrong password message.


Solution

  • Here's the script you're looking for, I'd suggest just getting rid of the msgbox entirely and appending onto InputBox text. Looks like you forgot to break your inner loop after the wrong password was entered. So it could never get back to the beginning.

    varCheck=True
    Dim StarterText: StarterText = "" 
    Do While varCheck
        Pass=InputBox(StarterText & "Enter Password")
        Do
            If IsEmpty(pass) Then
                WScript.quit
                Exit Do
            End If
            If Pass = "123" Then
                varCheck=False
                Exit Do
            Else
                varCheck=True
                StarterText = "Sorry, wrong password, try again: "
    
                Exit Do '<-----this is what you forgot. 
    
            End If
        Loop
    Loop