How to program in VBScript

Author: Eric Farmer
Date Of Creation: 10 March 2021
Update Date: 1 July 2024
Anonim
VBScript Tutorial for Beginners | VB Scripting for Beginners Tutorial | VBScript Basics
Video: VBScript Tutorial for Beginners | VB Scripting for Beginners Tutorial | VBScript Basics

Content

1 Choose a good code editor. Of course, you can use Notepad, but it will be more convenient to use a special editor with VBScript syntax highlighting.
  • 2 Install Internet Explorer. Internet Explorer is the only browser that supports VBScript because it is a Microsoft product. To see VBScript in action, you will need to have Internet Explorer installed.
    • Since Internet Explorer is only supported by Windows, it is best if you will be programming on a Windows computer.
  • 3 Learn the basics of the VBScript language. There are a few important language fundamentals that are useful to know before diving into programming.
    • Use (apostrophe) to indicate comments. Any line starting with an apostrophe is treated as a comment and is not processed by the script.Using comments helps other developers and yourself figure out what the code is doing.
    • Use _ (underscore) to continue the line. Usually the end of a line is indicated by simply moving to the next, but if it turns out to be too long, you can simply use _ at the end of an incomplete line to indicate that the current line continues on the next line.
  • Method 2 of 5: Create a base page

    1. 1 Create an HTML page. VBScript exists within HTML sites. To see how your VBScript works, you need to create an HTML file and open it in Internet Explorer. Open your code editor and enter the following code:

      html> head> title> VBScript Test / title> / head> body> / body> / html>

    2. 2 Add VBScript tags. When creating a page with VBScript code, you need to tell the browser information about what the next script is. Insert this HTML tag into your code:

      html> head> title> VBScript Test / title> / head> body> script language = "vbscript" type = "text / vbscript"> / script> / body> / html>

    3. 3 Use VBScript on ASP server. If you are writing a VBScript script for an ASP server, you can specify that the script starts next using a special tag:

      html> head> title> VBScript Test / title> / head> body>%%> / body> / html>

    Method 3 of 5: Create a Simple Hello World!

    1. 1 Insert the Write command. This command displays content to the user. When you use this command, the assigned text will be displayed in the browser.

      html> head> title> VBScript Test / title> / head> body> script language = "vbscript" type = "text / vbscript"> document.write () / script> / body> / html>

    2. 2 Add the text to be displayed. In parentheses, add the text you want to display on the screen. The text must be enclosed in quotation marks to indicate it as a string.

      html> head> title> VBScript Test / title> / head> body> script language = "vbscript" type = "text / vbscript"> document.write ("Hello World!") / script> / body> / html>

    3. 3 Open the HTML file in a browser. Save the code in .html format. Open the resulting file using Internet Explorer. The page should display in plain text Hello World!.

    Method 4 of 5: Using Variables

    1. 1 Declare variables. Variables allow you to store data that you can work with later. Variables must be declared using the command dim before assigning any values ​​to them. You can declare multiple variables at once. Variables must begin with a letter and can contain up to 255 Latin characters and numbers. In our example, we will declare the variable "age":

      html> head> title> VBScript Test / title> / head> body> script language = "vbscript" type = "text / vbscript"> dim age / script> / body> / html>

    2. 2 Assign values ​​to the variables. Now that the variable is declared, you can assign a value to it. Use the equal sign =to set the value of the variable. You can use the Write command to display the variable on the screen and make sure everything is working.

      html> head> title> VBScript Test / title> / head> body> script language = "vbscript" type = "text / vbscript"> dim age age = 30 document.write (age) / script> / body> / html>

    3. 3 Variable manipulation. You can use all math operations to work with variables. These operations are written in the same way as any mathematical operations. All your variables, including those into which the result of all manipulations will be written, must be previously declared.

      html> head> title> VBScript Test / title> / head> body> script language = "vbscript" type = "text / vbscript"> dim x dim y dim sum x = 10 y = 5 sum = x + y document.write (sum) 'the page will display "15" / script> / body> / html>

    4. 4 Create an array. An array is essentially a table (row) that contains more than one value. The array is treated as one variable. Like other variables, arrays must also be declared. You must also specify the number of variables that can be stored in the array. Note that the array numbering starts at 0. You can later access the data from the array.

      html> head> title> VBScript Test / title> / head> body> script language = "vbscript" type = "text / vbscript"> Dim names (2) Dim mother names (0) = "John" names (1) = "Jane" names (2) = "Pat" mother = names (1) / script> / body> / html>

    5. 5 Create a 2D array. You can also create multidimensional arrays to store more data. When declaring an array, you will need to specify the number of rows and columns it contains.

      html> head> title> VBScript Test / title> / head> body> script language = "vbscript" type = "text / vbscript"> Dim table (2,2) 'This will create a 3x3 table table (0,0) = "A" table (0,1) = "B" table (0,2) = "C" table (1,0) = "D" table (1,1) = "E" table (1,2) = "F" table (2,0) = "G" table (2,1) = "H" table (2,2) = "I" / script> / body> / html>

    Method 5 of 5: Using procedures

    1. 1 Find out the difference between procedures and "sub" and "function". There are two kinds of procedures in VBScript: sub (subroutines) and function (functions). These two types of procedures allow your program to do specific things.
      • Sub procedures can perform actions, but they cannot return values ​​to the program.
      • Function procedures can call other procedures as well as return values.
    2. 2 Write a sub procedure and call it. You can use subroutines to create tasks that your program can call later. Use Sub and End Subto add a subroutine. Use Callto activate subroutine

      html> head> title> VBScript Test / title> / head> body> script language = "vbscript" type = "text / vbscript"> Sub mysubproc () document.write ("This was written in a sub procedure") End Sub Call mysubproc () 'This will display the message written in the sub procedure / script> / body> / html>

    3. 3 Create a procedure function. Functions allow you to execute simple commands and return values ​​to your program. Procedures are functions and form the basis and functionality of your program. Use Function and End Functionto indicate the contents of the function.

      html> head> title> VBScript Test / title> / head> body> script language = "vbscript" type = "text / vbscript"> Function multfunction (x, y) multfunction = x * y End Function document.write (multfunction (4,5)) 'This will use your function and insert 4 and 5 into the x and y variables. ’The result will be printed on the screen. / script> / body> / html>