VBS won't allow programming

I was doing some programming and I get an error message when I try and run it. my code:

MsgBox ("hi")
sleep (2)
MsgBox ("you ok" vbYesNo, "how are you?")

Select Case result
    Case vbYes,
        MsgBox ("that's good!")
    Case vbNo, MsgBox ("sorry to hear that.")
End Select

Line: 3
Char: 18
Error: Expected ')' Source: Microsoft VBScript compilation error.

1 answer

  • answered 2019-03-14 09:12 Lankymart

    The problem is as the error says you have complication issues but looking at the rest of the example code you also have other issues waiting to be corrected.

    1. Missing argument syntax in MsgBox() Function:

      Line: 3

      Char: 18

      Error: Expected ')' Source: Microsoft VBScript compilation error.

      is because the Function MsgBox() expects arguments separated by a comma but the argument separator after "you ok" is missing causing the compiler to throw the exception.

    2. Cannot use parentheses when calling a Sub:

      When calling a procedure that doesn't return a value using brackets around multiple arguments passed to the procedure will cause;

      Microsoft VBScript compilation error: Cannot use parentheses when calling a Sub

      There is a caveat to this which is if it's just one argument it will not fail to compile, but this isn't because it accepts one argument but because it sees the brackets as part of the argument not part of the procedure (Eric Lippert discusses this at length in his blog);

      MsgBox("Hi") 'Will work

      This is equivalent to writing;

      Call MsgBox(("Hi")) 'Note the extra brackets

      This can be a little confusing so when it comes to multiple arguments you might think this is ok;

      MsgBox("you ok", vbyesno, "how are you?") 'This will error

      But as the brackets now denote the argument parentheses it becomes invalid and throws a compilation error. To avoid this there are three things you can do;

      1. Remove the parentheses

         MsgBox "you ok", vbyesno, "how are you?"
      2. Return a value (if the procedure can return a value)

         result = MsgBox("you ok", vbYesNo, "how are you?")

        As you check the value of result on the next line this would be the correct option as it result will contain the outcome of clicking either vbYes or vbNo in the MsgBox() function.

      3. Use the Call statement to prefix the procedure call

         Call MsgBox("you ok", vbYesNo, "how are you?")
    3. Syntax Error:

      This comes down to trailing commas after the Case arguments, just remove the commas and the code will compile.

      Select Case result
      Case vbYes
        Call MsgBox("that's good!")
      Case vbNo
        Call MsgBox("sorry to hear that.")
      End Select
    4. The Sleep duration:

      The Sleep() function expects a value expressed in milliseconds not seconds, at the moment the example code is waiting for 0.002 seconds. To wait for 2 seconds (which I'm assuming was the intention) use;

      Call Sleep(2000)

    Useful links