Sunday, November 22, 2009

Learning VBScript

Currently im working with SQL Server for business report automation. Although i love to write code in C++ or Java, but now i need to learn vbscript for the purpose of scheduled sending of mails. SQL server provides packages to perform scheduled data transformation, but working with the imported data ie. send the imported excel to the concerned person attaching it requires a activx script object and scripting in it and theres a whole lot of things you can do with microsoft automation scripting.

First i'd like to add most frequently asked questions' answers when working in this type of environment


  1. Set a variable when this is an object like
    dim name
    name=some value

    when some value is of primitive literal type the name doesnt require setting. But if some value is of like a createObject() method call of another reference of object like anObj.Property then you need to write

    dim name
    Set name=some object reference
    name=some other reference

    for the first time when initialising the name variable. Thenafter you can assign other references.
  2. In VBScript we have four looping statements:

    -For...Next statement - runs statements a specified number of times.
    -For Each...Next statement - runs statements for each item in a collection or each element of an array
    -Do...Loop statement - loops while or until a condition is true
    -While...Wend statement - Do not use it - use the Do...Loop statement instead

    Sample Codes:

    For i=1 to 10
    some code
    Next

    For i=2 To 10 Step 2
    some code
    Next

    For i=10 To 2 Step -2
    some code
    Next

    dim cars(2)
    cars(0)="Volvo"
    cars(1)="Saab"
    cars(2)="BMW"
    For Each x in cars
    document.write(x & "
    ")
    Next

    Do While i>10
    some code
    Loop

  3. Useful functions:

    Syntax




    FormatDateTime(date,format)





    ParameterDescription
    dateRequired. Any valid date expression (like Date()
    or Now())
    formatOptional. A Format value that specifies the date/time format to use

    Example 1







    document.write("The current date is: ")
    document.write(FormatDateTime(Date()))

    Output:

    The current date is: 2/22/2001


    Example 2







    document.write("The current date is: ")
    document.write(FormatDateTime(Date(),1))

    Output:

    The current date is: Thursday, February 22, 2001

No comments:

Post a Comment