You are on page 1of 10

Sample VB script examples

1) salary caluculation
Option explicit
Dim emp(100), noe, i, comm, gsal
'Read No: of employees
noe=inputbox("Enter No: of employees")
For i=0 to noe-1 step 1
emp(i)=inputbox("Enter employee basic salary")
Next
'Calculate gross salary
For i=0 to noe-1 step 1
If emp(i)>=30000 Then
comm=emp(i)*10/100
gsal=emp(i)+comm
emp(i)=gsal
elseif emp(i)<30000 and emp(i)>=15000 then
comm=emp(i)*5/100
gsal=emp(i)+comm
emp(i)=gsal
else
comm=200
gsal=emp(i)+comm
emp(i)=gsal
End If
Next
'Disply gross salary
For i=0 to noe-1 step 1
print emp(i)
Next
2)Validating Fields In Excel Sheet
Option explicit
Dim exo, wbo, wso, cho, rngo
Set exo=createobject("excel.application")
exo.Visible=true
Set wbo=exo.Workbooks.Open("C:\runs.xls")
set wso=wbo.Worksheets("Sheet1")
Set rngo=wso.range("A2","B11")
Set cho=wbo.Charts.add
cho.charttype=64
cho.setsourcedata rngo,2
cho.location 2,"Sheet1"
wbo.Save
exo.Quit
Set cho=nothing
Set rngo=nothing
Set wso=nothing
Set wbo=nothing
Set exo=nothing
3)Validation Of Values In Data Table
Option explicit
Dim x, y, z, n, i
n=datatable.GetSheet(2).GetRowCount
For i=1 to n step 1
datatable.GetSheet(2).SetCurrentRow(i)
x=datatable.Value("input1",2)
y=datatable.Value("input2",2)
z=cint(x)+cint(y)
datatable.Value("output",2)=z
Next

Option explicit
Dim x,y,n,i
n=datatable.GetSheet(2).GetRowCount
For i=1 to n step 1
datatable.GetSheet(2).SetCurrentRow(i)
x=datatable.value("input1",2)
y=datatable.Value("input2",2)
If cint(x)=cint(y) Then
datatable.value("output",2)="pass"
else
datatable.Value("output",2)="fail"
End If
Next
4) Data importing from excel to Data Table
Option explicit
Dim exo, wbo, wso, r, c, i, j, x, y
'create excel objects
Set exo=createobject("Excel.Application")
Set wbo=exo.Workbooks.Open("C:\mindq.xls")
Set wso=wbo.Worksheets("Sheet1")
r=wso.usedrange.rows.count
c=wso.usedrange.columns.count
'Datatable add sheet and create columns
datatable.AddSheet("Mindq")
For i=1 to c step 1
x=wso.cells(1,i)
datatable.GetSheet(3).AddParameter x,""
Next
'Data importing from excel to DT
For i=2 to r step 1
For j=1 to c step 1
y=wso.cells(i,j)
datatable.GetSheet(3).SetCurrentRow(i-1)
datatable.Value(j,3)=y
Next
Next
exo.Quit
Set wso=nothing
Set wbo=nothing
Set exo=nothing
5)Read line by line and stored into Data Table
Option explicit
Dim fso, fo, i, x, y, r
'Flat file open
Set fso=createobject("scripting.filesystemobject")
Set fo=fso.OpenTextFile("c:\testresults.txt",1,false)
'Datatable add sheet and createcolumns
Datatable.AddSheet("Mindq")
x=fo.ReadLine
y=split(x," ")
For i=lbound(y) to ubound(y) step 1
datatable.GetSheet(3).AddParameter y(i),""
Next
'Read line by line and stored into DT
r=1
While fo.AtEndOfStream<>true
x=fo.ReadLine
y=split(x," ")
datatable.GetSheet(3).SetCurrentRow(r)
For i=0 to ubound(y) step 1
datatable.Value(i+1, 3)=y(i)
Next
r=r+1
Wend
fo.Close
Set fo=nothing
Set fso=nothing
6) Working With Data Bases
Option explicit
Dim cono, rso, onum, expected, actual
'Read testdata
onum=inputbox("Enter order number")
expected=inputbox("Enter customer name")
expected=cstr(expected)
'Connect to DB
Set cono=createobject("ADODB.Connection")
cono.Open("DSN=QT_Flight32")
'Update DB
cono.Execute "Update orders set Customer_Name='"&expected&"' where
order_number="&onum
'select data from DB
Set rso=createobject("ADODB.Recordset")
rso.Open "select Customer_Name from orders where order_number="&onum,cono
actual=rso.Fields("Customer_Name").Value
If cstr(expected)=cstr(actual) Then
reporter.ReportEvent micPass,"DB testing","Correctly updated"
else
reporter.ReportEvent micFail,"DB testing","Incorrectly updated"
End If
rso.Close
cono.Close
Set rso=nothing
Set cono=nothing

--------------------------------------------
Option explicit
Dim cono, rso, x
Set cono=createobject("ADODB.Connection")
cono.Open("DSN=QT_Flight32")
Set rso=createobject("ADODB.Recordset")
rso.Open "Select Order_number from orders where tickets_ordered>5",cono
While rso.EOF<>true
x=rso.Fields("Order_Number").Value
cono.Execute "Update orders set tickets_ordered=5 where order_number="&x
rso.MoveNext
Wend
rso.Close
cono.Close

--------------------------------------------
Option explicit
Dim cono, rso, x
'connect to database
Set cono=createobject("adodb.connection")
cono.Open ("dsn=qt_flight32")
'select all customer names
Set rso=createobject("adodb.recordset")
rso.Open "select customer_name from orders",cono
'Disply customer names which starts with capital
While rso.EOF<>true
x=rso.Fields("customer_name").Value
If left(x,1)>="A" and left(x,1)<="Z" Then
print x
End If
rso.MoveNext
Wend
rso.Close
cono.Close

------------------------------------------------------------

Option explicit
Dim cono, rso, x, y, flag
'Connect to DB
x=inputbox("Enter order number")
Set cono=createobject("ADODB.Connection")
cono.Open("DSN=QT_Flight32")
'Delete specified record
cono.Execute "Delete from orders where Order_Number="&x
'Select all Orders
Set rso=createobject("ADODB.Recordset")
rso.Open "Select Order_number from orders",cono
flag=0
While rso.EOF<>true
y=rso.Fields("Order_Number").Value
If x=y Then
flag=1
End If
rso.MoveNext
Wend
If flag=0 Then
Reporter.ReportEvent micPass,"DB test","Deleted"
else
Reporter.ReportEvent micFail,"DB test","Not Deleted"
End If
rso.Close
cono.close
---------------------------------------------------------------------
Option explicit
Dim cono, rso, noc, i, cn
Set cono=createobject("ADODB.Connection")
cono.Open("DSN=QT_Flight32")
Set rso=createobject("ADODB.Recordset")
rso.Open "select * from orders",cono
noc=rso.Fields.Count
For i=0 to noc-1 step 1
cn=rso.Fields(i).Name
print cn
Next
rso.Close
cono.Close
Set rso=nothing
Set cono=nothing

--------------------------------------------------------

Option explicit
Dim cono, rso, noc
Set cono=createobject("ADODB.connection")
cono.open("Provider=SQLOLEDB; Server=SYS; Database=master; Trusted_Connection=yes;")
Set rso=createobject("ADODB.recordset")
rso.Open "select * from DBO.Spt_values", cono
noc=rso.Fields.Count
print "no. of columns in data table" &noc
rso.Close
cono.close
-------------------------------------------------------

Fetching the Data From Data base and storing the results in Excel Sheet

Option explicit
Dim exo, wbo, wso, cono, rso, noc, i, j
'create excel file
Set exo=createobject("Excel.Application")
exo.Visible=true
Set wbo=exo.Workbooks.Add
Set wso=wbo.Worksheets("Sheet1")
'Connect to DB and get data
Set cono=createobject("adodb.connection")
cono.Open("DSN=qt_flight32")
Set rso=createobject("adodb.recordset")
rso.Open "select * from orders",cono
noc=rso.Fields.Count
'create columns in excel
For i=0 to noc-1 step 1
wso.cells(1, i+1)=rso.Fields(i).Name
Next
'Rows storing
j=2
While rso.EOF<>true
For i=0 to noc-1 step 1
wso.cells(j, i+1)=rso.Fields(i).Value
Next
rso.MoveNext
j=j+1
Wend
wbo.SaveAs("C:\mydb.xls")
rso.Close
cono.Close
exo.Quit
Set rso=nothing
Set cono=nothing
Set wso=nothing
Set wbo=nothing
Set exo=nothing

----------------------------------------------------------

Fetching the Data From Data base and storing the results in Data Table

Option explicit
Dim cono, rso, noc, i, j, x
'Connect to DB and get data
Set cono=createobject("adodb.connection")
cono.Open("DSN=qt_flight32")
Set rso=createobject("adodb.recordset")
rso.Open "select * from orders",cono
noc=rso.Fields.Count
'create columns in Datatable
For i=0 to noc-1 step 1
x=rso.Fields(i).Name
datatable.GetSheet(1).AddParameter x,""
Next
j=1
'Rows storing
While rso.EOF<>true
datatable.GetSheet(1).SetCurrentRow(j)
For i=0 to noc-1 step 1
y=rso.Fields(i).Value
datatable.Value(i+1,1)=y
Next
rso.MoveNext
j=j+1
Wend
rso.Close
cono.Close
Set rso=nothing
Set cono=nothing

7) Working With Text/ Flat Files


Option explicit
Dim fso, fo1, fo2, x
Set fso=createobject("scripting.Filesystemobject")
Set fo1=fso.OpenTextFile("C:\testdata.txt",1,false)
Set fo2=fso.OpenTextFile("C:\testresults.txt",2, true)
While fo1.AtEndOfStream<>true
x=fo1.ReadLine
fo2.Writeline(x)
fo2.WriteBlankLines(3)
Wend
fo1.Close
fo2.Close
Set fo1=nothing
Set fo2=nothing
Set fso=nothing