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

天大地大妈咪最大 提交于 2020-06-08 13:50:22

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/42140865/how-to-make-a-vbscript-loop-in-loop-go-to-start-of-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!