· 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! TABLE OF CONTENTS ↑ GETTING STARTED This is what you need to do to start programming right away: 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: Command1
Command2 command(param1, param2) Of course there can be any number of parameters, this depends on the command. 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: Command1 Command2 (param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12) Command3 This way it is easier to see where a new line starts. From now on, let's focus on the actual programming. ↑ DISPLAYING MESSAGES ON SCREEN: MSGBOX 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:
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. ↑ INPUT FROM THE USER: INPUTBOX X=inputbox(text,title) 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)
Example: 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. ↑ FIRST STEPS TO A.I.: IF ... THEN ... ELSE If something then Dosomething 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!" Else Msgbox "so you're under 18!" End if This script includes some new commands: The possible answers to a message box are: 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. ;-) ↑ COMBINING THE COMMANDS 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?" Else 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!" Else Msgbox "ok, don't cry..." End if Else 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 ↑ SOOO REPETITIVE: DO ... LOOP 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. Example: A=vbno Do until a=vbyes A=msgbox("exit loop?",vbyesno) Loop 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: A=vbno Do while a=vbno A=msgbox("exit loop?",vbyesno) Loop 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. ↑ LOOPS, RELOADED: FOR ... NEXT For var=start to end [step number] ... Next 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 Next 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 Next 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." Next ↑ MATHS WITH VBS 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. 2+2*2=6 (2+2)*2=8
You can use the following operations (and some other very specific ones):
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!
|
← navigation ↑ home · stuff ↓
E-mail:
Visitor map:
SITE STATUS:
|