format c:_

Site: HomeGalleryCopyright and Disclaimer
Hobbies: GadgetsiPodLEGORobotsFlashTutorialsDownloads

You are here:
format c:_
→ VBS Tutorial

 · VBS Tutorial

If you need a quick and easy way to make calculations, interact with the user via a simple interface or just want to create some small programs, you've come to the right place. VBS (Visual Basic Script) is like 'the little brother' of Visual Basic, a major programming language. The main difference is the price: VBS is absolutely free and comes with every Windows version starting from 98. Here, I'll describe some of the aspects of this programming language. Enjoy!

  • Getting started
  • Displaying messages on screen: msgbox
  • Input from the user: inputbox
  • First steps to A.I.: if ... then ... else
  • Combining the commands
  • Sooo repetitive: do ... loop
  • Loops, reloaded: for ... next
  • Maths with VBS
    This is what you need to do to start programming right away:
  • Start notepad (Start > Run > notepad.exe)
  • Type in the code (VBS commands)
  • Save the file as "something.vbs" (do not forget the quotation marks and the .vbs extension!)
  • Double-click the saved file to run the program

    To create your programs, you need certain commands to tell them what to do. Before we learn the use of these commands, I must explain the basics of VBS commands and their syntax:

  • You write one command per line, and press enter to tell the program you are writing a new command, just like this:
  • It makes no difference if you write in UPPERCASE or lowercase. I'll explain how I do it right after this.
  • A command that requires parameters (additional information for that command) is written like this:
    command(param1, param2)

    Of course there can be any number of parameters, this depends on the command.
  • To differenciate between commands and normal text, you use "quotation marks". Just like this:
    msgbox "Hello!"

    The command (msgbox) is written just like that, and the text to be displayed in the MeSsaGeBOX ("Hello!") is in quotations.

    As I said, UPPERCASE and lowercase are the same to VBS. But, because some browsers and text editors might break one line into many (and this would prevent the correct execution of the program), I will use a capital letter on the first word of each line, like this:

    Command2 (param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12)

    This way it is easier to see where a new line starts.
    From now on, let's focus on the actual programming.
    Msgbox text,options,title

    text: the text you want to apperar in the box (in quotation marks "" because it's a string, another name for normal text in VBS)
    options: parameters you can set for the box (see below; not necessary)
    title: the text to appear in the title bar (in quotation marks ""; not necessary)

    This is the way you make a message box appear on the screen. In the 'options' part, you specify the behavior of the message box. The options you can set are:

  • vbExclamation -- shows an exclamation mark icon next to the message
  • vbQuestion -- shows a question mark next icon to the message
  • vbInformation -- shows an information mark icon to the message
  • vbCritical -- shows an error icon next to the message Buttons:
  • vbOKOnly -- displays 'ok' button (default)
  • vbYesNo -- displays 'yes' and 'no' buttons
  • vbOKCancel -- displays 'ok' and 'cancel' buttons
  • vbYesNoCancel -- displays 'yes', 'no' and 'cancel' buttons
  • vbAbortRetryIgnore -- displays 'abort', 'retry' and 'ignore' buttons
  • vbRetryCancel -- displays 'retry' and 'cancel' buttons

    To combine these options: '+' sign. Example:

    Msgbox "do you want to start the program?",vbquestion+vbyesno,"my program"

    Makes a message box with a question icon next to the text and the buttons 'yes' and 'no'.

    To create a new line in a message box: vbCrLf. Example:

    Msgbox "this is some text" & vbcrlf & "distributed on two lines"
    Msgbox "this is some text" & vbcrlf & vbcrlf & "with an empty line inbetween"

    Do not forget the spaces around the & signs!
    vbCrLf stands for carriage return and line feed, if you were wondering.

    X: a variable, a space in memory where the input will be stored (can have any other name
    text: the text that is displayed in the box (in quotation marks "")
    title: the text to appear in the title bar of the box (in quotation marks ""; not necessary)


    X=inputbox("what's your name?","my program")
    Msgbox x

    >This script first prompts you for your name, then displays it in a message box. Note that if you want to display variables in a message box, you do not put them into quotation marks ("") because they are not text strings, but spaces in memory!

    If you want to combine the input with other text: & sign.

    X=inputbox("what's your name?","my program")
    Msgbox "so your name is " & x & "."

    Important: Don't forget the spaces between ", & and x, otherwise the program will not be able to understand the command.
    If something then
    End if

    This script executes the commands between then and end if (in this example the imaginary command Dosomething) if the statement (something) is true. The two spaces at the beginning of the second line are not necessary, but useful to see the Dosomething command is nested between the if and end if commands. Example:
    X=inputbox("what's your age?","my program")
    If x > 17 then
      Msgbox "so you're an adult!"
    End if

    This script asks for your age and if you enter a number that's 18 or higher (if x > 17), you recieve a message.
    X=msgbox("are you an adult?",vbyesno+vbquestion,"my program")
    If x=yes then
      Msgbox "so you're over 18!"
      Msgbox "so you're under 18!"
    End if

    This script includes some new commands:
  • The msgbox command is now in brackets: msgbox(...) because it's response value (the button you clicked) is assigned to a variable (x=msgbox...)
  • The clicked button is checked (if x=vbyes - if the answer to the message box was 'yes'; see below for possible answers)
  • If the statement (x=vbyes) is true, you recieve an alert (commands between then and else), and if not (else), you recieve a different one (commands between else and end if).
    The possible answers to a message box are:
  • vbYes
  • vbNo
  • vbOK
  • vbCancel
  • vbAbort
  • vbRetry
  • vbIgnore

    Try this script and see what happens if you modify it. If you recieve an error message, don't panic. You probably forgot to put an " somewhere or had a little typing mistake. ;-)

    Here's a script that's a little larger. Try to understand it, or copy it into notepad and see what happens!
    A=msgbox("do you want to start the program?",vbyesno+vbquestion)
    If a=vbyes then
      B=inputbox("first of all, what's your name?")
      Msgbox "oh, so your name is " & b
      C=msgbox("that's a nice name. do you like your name?",vbyesno)
      If c=vbno then
        Msgbox "well, it's not your fault, is it?"
        Msgbox "that's good."
      End if
      D=inputbox("what's your age?")
      If d > 17 then
        E=msgbox("do you have a car?",vbyesno)
        If e=vbyes then
          Msgbox "cool!"
          Msgbox "ok, don't cry..."
        End if
        Msgbox "well then, you'll have to wait a little until you're able to drive..."
      End if
      Msgbox "well then, it was nice to talk to you, " & b & "!"
      Msgbox "goodbye!"
    End if

    Do [while/until condition]
    Loop [while/until condition]

    while/until condition: the loop executes the commands between do and loop while/until condition is true (can be placed at the beginning or at the end of the loop.
    Do until a=vbyes
      A=msgbox("exit loop?",vbyesno)

    This script displays a message until the messagebox (stored in variable a) is answered with vbYes (a=vbyes=true). Here is another way of a script doing exactly the same, but written differently:
    Do while a=vbno
      A=msgbox("exit loop?",vbyesno)

    Here, the script repeats the loop while it is answered with no.
    You can make the script check the condition before or after the loop. If you make it check it after the loop, it will be executed at least once, and then it repeats until condition is true. But if you check it at the beginning and the condition is already true, the commands in the loop are not executed at all. In the example above, it wouldn't affect the result if we checked the condition after the loop, as we make sure it is executed at least once by setting a=vbno at the beginning.
    For var=start to end [step number]

    This loop is a little different from the one above:
    var: a variable that holds the number of loops executed
    start: set var at the beginning of the loop
    end: repeat loop until var=end
    number: to make var icrement/decrement in set steps (if not present, it will be 1)
    This means the loop is executed while the value of var is between start and end, and it is added to the step number in every execution.
    Got it? Here's a little example to help you understand. It simply counts from 1 to 10:
    For i=1 to 10
      Msgbox i

    See? It's not so difficult! The following code counts from 5 to 0 substracting 0.5 in every count:
    For i=5 to 0 step -0.5
      Msgbox i

    The good thing of loops is that they allow you to control your program more dinamically. Imagine you want to count from 1 to 5. You could do it like this:
    Msgbox "1"
    Msgbox "2"
    Msgbox "3"
    Msgbox "4"
    Msgbox "5"

    But what if you want to count to 5000? With loops, it's easy:
    For i=1 to 5000
      Msgbox i
    Next i

    So if you need to count to 5, to 5000 or to 5000000, you only change one little number! You can even specify the number of loops during the exectution like this:
    Max=inputbox("how many times do you want to see the message? make sure you enter an integer number that is greater than 0")
    For counter=1 to max
      Msgbox "message" & vbcrlf & "it has been displayed " & counter & " times."

    OK, I'm not a big maths fan, either, but like any other pogramming language, VBS also supports multiple math functions, so here are some of them. Examples:
    1: Msgbox inputbox("enter a number")*2
    2: Msgbox inputbox("enter a number")^2
    3: Msgbox inputbox("enter a number")+(3.6+inputbox("enter another number"))/(4-2^4)

    All these functions perform specific mathematical operations. The first one simply multiplies the number you enter by two, the second multiplies it with itself (^2=; exponent) and the third makes a complex calculation.
    It basically works just the way you know it with calculators. Power (^) goes first, then multiplication/division, then addition/substraction. you can also set brackets to modify the order of calculations or if you're not sure which operation goes first.

    You can use the following operations (and some other very specific ones):
    ^2^3=8power (2=8)
    *2*3=6multiplication (23=8;23=8)
    /2/3=0.666division (23=0.666)
    \2\3=0integer division
    mod2 mod 3=2the rest of a division
    sqrsqr(25)=5square root of the sentence in brackets
    sin/cos/tansin(3.1415/2)=1trigonometric functions in radians
    absabs(-3)=3absolute value (negative numbers become positive)
    loglog(2,718282)=1logarythm to base e (2,718282)
    sgnsgn(-3)=-1sign (returns -1 for negative, 1 for positive and 0 for 0)

    So, that's it for the moment. I'll try to add more VBS commands here later. If you have any comments or suggestions, or if you run into a problem while programming, contact me!

    © Daniel Rojas 2002-2006 Top ↑
  • E-mail:

    New website: