-3

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
i Mr Oli i
  • 605
  • 5
  • 12
  • 1
    Try `msgbox ("you ok", vbyesno, "how are you?")` – Matthias Sommer Mar 13 '19 at 20:52
  • Once you fix that you'll also need to remove the trailing `,` on the end of the `Case` variables or you'll get `Microsoft VBScript compilation error: Syntax error`, also your `MsgBox()` function calls will either need a return value to use brackets or be prefixed with `Call Msgbox(...)`, not the mention your `Sleep()` as well plus it's only sleeping for `0.002` seconds *(`Sleep` is in milliseconds not seconds)*. – user692942 Mar 14 '19 at 08:38
  • 1
    It would be nice if you'll carefully read docs about syntax of VBScript and the functions/statements you're trying to use https://ss64.com/vb/syntax.html – montonero Mar 14 '19 at 09:04

1 Answers1

2

The problem is as the error says you have compilation 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:

    Microsoft VBScript compilation error: 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

user692942
  • 16,398
  • 7
  • 76
  • 175