Monday, 29 December 2014

VB Script to find the number of occurences of a char in a string

Method 1:
str="Hello world"
Set regex=new RegExp
regex.pattern="l"
regex.global=true
Set matches=regex.execute(str)
msgbox matches.count

Method 2:
arr=split(str,"l")
msgbox ubound(arr)

Method 3:
counter=0
For i=1 to len(str)
   char=mid(str,i,1)
   If char="l" Then
     counter=counter+1
   End If
Next

msgbox counter

QTP - Sql Server 2005 Connections



Option Explicit
Dim con,rs, i

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

con.Open"Provider=sqloledb;Server=SYS;Database=csr_db;uid=sa;pwd="
rs.open "select * from csr1",con
i=0
MsgBox rs.Fields.Count
Do while not rs.eof
    print "Row  :  "&i
    print rs.fields(0).Name &" : "&  rs.fields(0).Value
    print rs.fields(1).Name &" : "&  rs.fields(1).Value
    print rs.fields(2).Name &" : "&  rs.fields(2).Value
    rs.movenext
    i = i+1
Loop

'Release objects'Release objects
Set rs= nothing
Set con= nothing

QTP - Accessing Databases



QTP Scripts for connecting to MS Access:
Option Explicit
Dim con,rs

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

con.open "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;"

rs.open "select * from emp",con

Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop

'Release objects'Release objects
Set rs= nothing
Set con= nothing

Note: The database we are using here is MS Access.Before running this script create a table in MS Acess.In the above script I used table called "emp" and column 'names as "v1" and "v2". "d:testdata.mdb" is path of the table which we created. The main use of this script is to use testdata of table(which is in  ' database) in the application. In the above script we are passing values from database to Textboxes in Windows Application.

QTP Script for connecting to sqlserver: 
Option Explicit
Dim con,rs

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

con.open"Driver={SQL Server};server=MySqlServer;uid=MyUserName;pwd=MyPassword;database=pubs"
rs.open "select * from emp",con

Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop

'Release objects'Release objects
Set rs= nothing
Set con= nothing

QTP Script for connecting to oracle:
Option Explicit
Dim con,rs

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

con.open "Driver={Microsoft ODBC for Oracle};Server=QTPWorld; Uid=your_username;Pwd=your_password;"
rs.open "select * from emp",con

Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop

'Release objects
Set rs= nothing
Set con= nothing

QTP Script for connecting to MySQL:
Option Explicit
Dim con,rs

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

con.open"Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=myDB;User=Uname;Password=Pwd;Option=3;"
rs.open "select * from emp",con

Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop

'Release objects
Set rs= nothing
Set con= nothing

QTP Script for connecting to Excel:
Option Explicit
Dim con,rs

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

con.open "DRIVER={Microsoft Excel Driver (*.xls)};DBQ=C:\TestStatus.xls;Readonly=True"
rs.open "SELECT count(*) FROM [Status$] where Status = 'Failed' ",con

Msgbox rs(0)

'Release objects
Set rs= nothing
Set con= nothing


QTP Script for connecting to Sybase:
  
Option Explicit
Dim con,rs

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

' Open a session to the database
con.open"Driver={SYBASE SYSTEM 11};Srvr=myServerAddress;Uid=Uname;Pwd=Pwd;Database=myDataBase;"
rs.open "select * from emp",con

Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop

'Release objects
Set rs= nothing
Set con= nothing