Search code examples
vbscriptmsgbox

My VBS script "if" statement made an error


I was creating a VBS script (a prank script) but the script does what's written below if vbNo then when I click Yes

X=MsgBox("Please Restart. Your system will break down.",4+4096, ErR0r)
if vbNo then
 X=MsgBox("e")
 dim count
 set object = wscript.CreateObject("wscript.shell")

 do
 object.run "error.vbs"
 count = count + 1
 loop until count = 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
end if

if vbYes then
   Dim IntCounter
   Dim objWshShl : Set objWshShl = WScript.CreateObject("wscript.shell")
   Dim objVoice : Set objVoice = WScript.CreateObject("sapi.spvoice")

   ShutdownWarning()
   TimedMessageBox()
   ShutdownComputer()

   Function ShutdownWarning
        X=MsgBox("Thou hath been tricked", 0+4096)
    WScript.Sleep 5000
   End Function

   Function TimedMessageBox
    For IntCounter = 5 To 1 Step -1
    objWshShl.Popup "Your time will be wasted in " _
    & IntCounter & " seconds",1,"Computer Shutdown", 0+4096
    Next
   End Function

   Function ShutdownComputer
    objWshShl.Run "Shutdown /s /f /t 0",0
   End Function
end if

How do I fix this?


Solution

  • vbNo is a constant value equal to 7.

    if vbNo will always be true, since 7 is not a falsy value.

    You need to use a variable to get the return value of the user input, such as :

    userInput = msgBox("click yes or no", vbYesNo)
    if userInput = vbNo Then
       ' do something
    end if