SlideShare uma empresa Scribd logo
1 de 6
www.gcreddy.com
       QTP Database Script Examples

1) Data Driven Testing for Login Operation by Fetching Test Data
directly From a Database

Option Explicit
Dim objConnection, objRecordSet
'Creating an Automation Object in Database Connection Class, that can
be used to connect to Databases
Set objConnection=CreateObject("Adodb.Connection")
'Creating an Automation Object in Database RecordSet Class, that can
be used to Perform Operations on Database Tables (Records)
Set objRecordSet=CreateObject("Adodb.RecordSet")
'Establishing Connection String for MS Access Database
objConnection.Provider=("Microsoft.Jet.OLEDB.4.0")
objConnection.Open "C:Documents and
SettingsbannuDesktopgcreddy.mdb"
'Fetching Test Data using RecordSet Object
objRecordSet.Open "Select * From Login",objConnection

Do Until objRecordSet.EOF=True
SystemUtil.Run "C:Program FilesHPQuickTest
Professionalsamplesflightappflight4a.exe"
Dialog("text:=Login").Activate
Dialog("text:=Login").Winedit("attached text:=Agent Name:").Set
objRecordSet.Fields("Agent")
Dialog("text:=Login").Winedit("attached text:=Password:").Set
objRecordSet.Fields("Password")
Wait 2
Dialog("text:=Login").Winbutton("text:=OK","width:=60").Click
Window("text:=Flight Reservation").Close
objRecordSet.MoveNext
Loop

objRecordSet.Close
objConnection.Close

Set objRecordSet=Nothing
Set objConnection=Nothing




                      www.gcreddy.net
www.gcreddy.com
2) Write Data to a Database Table

Dim objConnection, objCommand
Set objConnection=CreateObject("Adodb.Connection")
Set objCommand=CreateObject("Adodb.Command")
objConnection.Provider=("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:Documents and SettingsTest
classDesktopFlights.mdb"

objCommand.ActiveConnection=objConnection

objCommand.CommandText ="Insert into Login values
(8,'Chennai','Mercury')"
objCommand.Execute

objConnection.Close
Set objCommand=Nothing
Set objConnection=Nothing

-----------------------------------------------------
3) Write Multiple Sets of Data to a Database Table

Dim objConnection, objCommand
Dim objExcel, objWorkBook, objWorksheet

Set objExcel=CreateObject("Excel.Application")
Set objWorkBook=objExcel.Workbooks.Open ("C:Documents and
SettingsTest classDesktopdata1.xls")
Set objWorkSheet=objWorkBook.Worksheets("Sheet1")

Set objConnection=CreateObject("Adodb.Connection")
Set objCommand=CreateObject("Adodb.Command")

objConnection.Provider=("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:Documents and SettingsTest
classDesktopFlights.mdb"

objCommand.ActiveConnection=objConnection
Rows_Count=objWorkSheet.Usedrange.Rows.Count
For i= 2 To Rows_Count Step 1
SNO=objWorkSheet.Cells(i,"A")
Agent=objWorkSheet.Cells(i,"B")
Password=objWorkSheet.Cells(i,"C")


                   www.gcreddy.net
www.gcreddy.com
objCommand.CommandText ="Insert into Login values
('"&SNO&"','"&Agent&"','"&Password&"')"
objCommand.Execute
Next
objWorkBook.Close
objExcel.Quit
Set objExcel=Nothing
objConnection.Close
Set objCommand=Nothing
Set objConnection=Nothing

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

4) Export Data from a Database Table to an Excel file (2nd
Sheet)

Option Explicit
Dim objConnection, objRecordset
Dim objExcel, objWorkbook, objWorksheet, i
Set objConnection = Createobject ("Adodb.Connection")
Set objRecordset = Createobject ("Adodb.Recordset")

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:Documents and
SettingsTest classDesktopdata1.xls")
Set objWorksheet = objWorkbook.Worksheets("Sheet2")

objWorksheet.cells(1,"A") = "Agents"
objWorksheet.cells(1,"B") = "Password"

objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:Documents and SettingsTest
classDesktopFlights.mdb"

objRecordset.Open "Select Agent, Password from Login", objConnection

i = 2 'Rows
Do Until objRecordset.EOF

objWorksheet.cells(i, "A") = objRecordset.Fields("Agent")
objWorksheet.cells(i, "B") = objRecordset.Fields("Password")
i=i+1
objRecordset.MoveNext
Loop


                      www.gcreddy.net
www.gcreddy.com
objWorkbook.Save
objWorkbook.Close
objExcel.Quit
Set objExcel = Nothing

objRecordset.Close
objConnection.Close

Set objRecordset = Nothing
Set objConnection = Nothing

----------------------------------------------------
5) Export Data from a Database Table to a Text file

Option Explicit
Dim objConnection, objRecordset
Dim objFso,myFile
Set objConnection = Createobject ("Adodb.Connection")
Set objRecordset = Createobject ("Adodb.Recordset")

Set objFso = CreateObject("Scripting.Filesystemobject")
Set myFile=objFso.OpenTextFile("C:Documents and SettingsTest
classDesktopdata1.txt",2)

myFile.WriteLine "Agent "&" Password"
myFile.WriteLine "--------------------"

objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:Documents and SettingsTest
classDesktopFlights.mdb"

objRecordset.Open "Select Agent, Password from Login", objConnection


Do Until objRecordset.EOF

myFile.WriteLine objRecordset.Fields("Agent")&" ," &
objRecordset.Fields("Password")
objRecordset.MoveNext
Loop

myFile.Close
Set objFso=Nothing


                      www.gcreddy.net
www.gcreddy.com
objRecordset.Close
objConnection.Close

Set objRecordset = Nothing
Set objConnection = Nothing

------------------------------------------------
6) Export Data from a Text File to a Database Table

Option Explicit
Dim objConnection, objCommand
Dim objFso,myFile,myLine,myField,SNO,Agent,Password
Set objConnection = Createobject ("Adodb.Connection")
Set objCommand = Createobject ("Adodb.Command")

Set objFso = CreateObject("Scripting.Filesystemobject")
Set myFile=objFso.OpenTextFile("C:Documents and SettingsTest
classDesktopdat1.txt",1)
objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:Documents and SettingsTest
classDesktopFlights.mdb"
myFile.SkipLine
myFile.SkipLine

Do While myFile.AtEndOfStream = FALSE
myLine = myFile.ReadLine
myField = Split(myLine,",")
SNO = myField(0)
Agent = myField(1)
Password = myField(2)

objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Insert into Login2
values('"&SNO&"','"&Agent&"','"&Password&"')"
objCommand.Execute

Loop


myFile.Close
Set objFso=Nothing

Set objCommand = Nothing


                      www.gcreddy.net
www.gcreddy.com
objConnection.Close
Set objConnection = Nothing




                   www.gcreddy.net

Mais conteúdo relacionado

Destaque

Automation testing material by Durgasoft,hyderabad
Automation testing material by Durgasoft,hyderabadAutomation testing material by Durgasoft,hyderabad
Automation testing material by Durgasoft,hyderabadDurga Prasad
 
New features in qtp11
New features in qtp11New features in qtp11
New features in qtp11G.C Reddy
 
How Manual Testers Can Break into Automation Without Programming Skills
How Manual Testers Can Break into Automation Without Programming SkillsHow Manual Testers Can Break into Automation Without Programming Skills
How Manual Testers Can Break into Automation Without Programming SkillsRanorex
 
Manual Testing Notes
Manual Testing NotesManual Testing Notes
Manual Testing Notesguest208aa1
 
Qtp 92 Tutorial
Qtp 92 TutorialQtp 92 Tutorial
Qtp 92 Tutorialsasidhar
 
Manual Testing Material by Durgasoft
Manual Testing Material by DurgasoftManual Testing Material by Durgasoft
Manual Testing Material by DurgasoftDurga Prasad
 
Manual testing concepts course 1
Manual testing concepts course 1Manual testing concepts course 1
Manual testing concepts course 1Raghu Kiran
 
Testing documents
Testing documentsTesting documents
Testing documentssuhasreddy1
 
Manual QA Testing Interview Questions From H2KInfosys
Manual QA Testing Interview Questions From H2KInfosysManual QA Testing Interview Questions From H2KInfosys
Manual QA Testing Interview Questions From H2KInfosysH2kInfosys
 
Manual testing-training-institute-in-marathahalli
Manual testing-training-institute-in-marathahalliManual testing-training-institute-in-marathahalli
Manual testing-training-institute-in-marathahallisiyaram ray
 
Test Life Cycle - Manual Testing Concept.
Test Life Cycle - Manual Testing Concept.Test Life Cycle - Manual Testing Concept.
Test Life Cycle - Manual Testing Concept.guestf9bc
 
01. testing fresher-resume
01. testing fresher-resume01. testing fresher-resume
01. testing fresher-resumemuqtar12
 
TESTING LIFE CYCLE PPT
TESTING LIFE CYCLE PPTTESTING LIFE CYCLE PPT
TESTING LIFE CYCLE PPTsuhasreddy1
 

Destaque (15)

Ppt Qtp
Ppt QtpPpt Qtp
Ppt Qtp
 
Automation testing material by Durgasoft,hyderabad
Automation testing material by Durgasoft,hyderabadAutomation testing material by Durgasoft,hyderabad
Automation testing material by Durgasoft,hyderabad
 
New features in qtp11
New features in qtp11New features in qtp11
New features in qtp11
 
How Manual Testers Can Break into Automation Without Programming Skills
How Manual Testers Can Break into Automation Without Programming SkillsHow Manual Testers Can Break into Automation Without Programming Skills
How Manual Testers Can Break into Automation Without Programming Skills
 
Manual Testing Notes
Manual Testing NotesManual Testing Notes
Manual Testing Notes
 
Qtp 92 Tutorial
Qtp 92 TutorialQtp 92 Tutorial
Qtp 92 Tutorial
 
Manual Testing Material by Durgasoft
Manual Testing Material by DurgasoftManual Testing Material by Durgasoft
Manual Testing Material by Durgasoft
 
Manual testing concepts course 1
Manual testing concepts course 1Manual testing concepts course 1
Manual testing concepts course 1
 
Testing documents
Testing documentsTesting documents
Testing documents
 
Manual QA Testing Interview Questions From H2KInfosys
Manual QA Testing Interview Questions From H2KInfosysManual QA Testing Interview Questions From H2KInfosys
Manual QA Testing Interview Questions From H2KInfosys
 
Manual testing-training-institute-in-marathahalli
Manual testing-training-institute-in-marathahalliManual testing-training-institute-in-marathahalli
Manual testing-training-institute-in-marathahalli
 
Test Life Cycle - Manual Testing Concept.
Test Life Cycle - Manual Testing Concept.Test Life Cycle - Manual Testing Concept.
Test Life Cycle - Manual Testing Concept.
 
Fresher testing cv
Fresher testing cvFresher testing cv
Fresher testing cv
 
01. testing fresher-resume
01. testing fresher-resume01. testing fresher-resume
01. testing fresher-resume
 
TESTING LIFE CYCLE PPT
TESTING LIFE CYCLE PPTTESTING LIFE CYCLE PPT
TESTING LIFE CYCLE PPT
 

Mais de G.C Reddy

QTP Training
QTP TrainingQTP Training
QTP TrainingG.C Reddy
 
Software+struc+doc
Software+struc+docSoftware+struc+doc
Software+struc+docG.C Reddy
 
Qtp (advanced)
Qtp (advanced)Qtp (advanced)
Qtp (advanced)G.C Reddy
 
Qtp (basics to advanced)
Qtp (basics to advanced)Qtp (basics to advanced)
Qtp (basics to advanced)G.C Reddy
 
Object Repository
Object RepositoryObject Repository
Object RepositoryG.C Reddy
 
Advanced Qtp
Advanced QtpAdvanced Qtp
Advanced QtpG.C Reddy
 
Advanced Qtp Book
Advanced Qtp BookAdvanced Qtp Book
Advanced Qtp BookG.C Reddy
 
File System Operations
File System OperationsFile System Operations
File System OperationsG.C Reddy
 
Web Dictionary
Web DictionaryWeb Dictionary
Web DictionaryG.C Reddy
 

Mais de G.C Reddy (13)

Html
HtmlHtml
Html
 
QTP Training
QTP TrainingQTP Training
QTP Training
 
Software+struc+doc
Software+struc+docSoftware+struc+doc
Software+struc+doc
 
Qtp (advanced)
Qtp (advanced)Qtp (advanced)
Qtp (advanced)
 
Qtp (basics to advanced)
Qtp (basics to advanced)Qtp (basics to advanced)
Qtp (basics to advanced)
 
Object Repository
Object RepositoryObject Repository
Object Repository
 
Advanced Qtp
Advanced QtpAdvanced Qtp
Advanced Qtp
 
Advanced Qtp Book
Advanced Qtp BookAdvanced Qtp Book
Advanced Qtp Book
 
File System Operations
File System OperationsFile System Operations
File System Operations
 
Qtp Faq
Qtp FaqQtp Faq
Qtp Faq
 
Qtp Summary
Qtp SummaryQtp Summary
Qtp Summary
 
Qtp Scripts
Qtp ScriptsQtp Scripts
Qtp Scripts
 
Web Dictionary
Web DictionaryWeb Dictionary
Web Dictionary
 

Último

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 

Último (20)

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 

QTP Database Script Examples

  • 1. www.gcreddy.com QTP Database Script Examples 1) Data Driven Testing for Login Operation by Fetching Test Data directly From a Database Option Explicit Dim objConnection, objRecordSet 'Creating an Automation Object in Database Connection Class, that can be used to connect to Databases Set objConnection=CreateObject("Adodb.Connection") 'Creating an Automation Object in Database RecordSet Class, that can be used to Perform Operations on Database Tables (Records) Set objRecordSet=CreateObject("Adodb.RecordSet") 'Establishing Connection String for MS Access Database objConnection.Provider=("Microsoft.Jet.OLEDB.4.0") objConnection.Open "C:Documents and SettingsbannuDesktopgcreddy.mdb" 'Fetching Test Data using RecordSet Object objRecordSet.Open "Select * From Login",objConnection Do Until objRecordSet.EOF=True SystemUtil.Run "C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe" Dialog("text:=Login").Activate Dialog("text:=Login").Winedit("attached text:=Agent Name:").Set objRecordSet.Fields("Agent") Dialog("text:=Login").Winedit("attached text:=Password:").Set objRecordSet.Fields("Password") Wait 2 Dialog("text:=Login").Winbutton("text:=OK","width:=60").Click Window("text:=Flight Reservation").Close objRecordSet.MoveNext Loop objRecordSet.Close objConnection.Close Set objRecordSet=Nothing Set objConnection=Nothing www.gcreddy.net
  • 2. www.gcreddy.com 2) Write Data to a Database Table Dim objConnection, objCommand Set objConnection=CreateObject("Adodb.Connection") Set objCommand=CreateObject("Adodb.Command") objConnection.Provider=("Microsoft.ACE.OLEDB.12.0") objConnection.Open "C:Documents and SettingsTest classDesktopFlights.mdb" objCommand.ActiveConnection=objConnection objCommand.CommandText ="Insert into Login values (8,'Chennai','Mercury')" objCommand.Execute objConnection.Close Set objCommand=Nothing Set objConnection=Nothing ----------------------------------------------------- 3) Write Multiple Sets of Data to a Database Table Dim objConnection, objCommand Dim objExcel, objWorkBook, objWorksheet Set objExcel=CreateObject("Excel.Application") Set objWorkBook=objExcel.Workbooks.Open ("C:Documents and SettingsTest classDesktopdata1.xls") Set objWorkSheet=objWorkBook.Worksheets("Sheet1") Set objConnection=CreateObject("Adodb.Connection") Set objCommand=CreateObject("Adodb.Command") objConnection.Provider=("Microsoft.ACE.OLEDB.12.0") objConnection.Open "C:Documents and SettingsTest classDesktopFlights.mdb" objCommand.ActiveConnection=objConnection Rows_Count=objWorkSheet.Usedrange.Rows.Count For i= 2 To Rows_Count Step 1 SNO=objWorkSheet.Cells(i,"A") Agent=objWorkSheet.Cells(i,"B") Password=objWorkSheet.Cells(i,"C") www.gcreddy.net
  • 3. www.gcreddy.com objCommand.CommandText ="Insert into Login values ('"&SNO&"','"&Agent&"','"&Password&"')" objCommand.Execute Next objWorkBook.Close objExcel.Quit Set objExcel=Nothing objConnection.Close Set objCommand=Nothing Set objConnection=Nothing --------------------------------------------------------- 4) Export Data from a Database Table to an Excel file (2nd Sheet) Option Explicit Dim objConnection, objRecordset Dim objExcel, objWorkbook, objWorksheet, i Set objConnection = Createobject ("Adodb.Connection") Set objRecordset = Createobject ("Adodb.Recordset") Set objExcel = CreateObject("Excel.Application") Set objWorkbook = objExcel.Workbooks.Open("C:Documents and SettingsTest classDesktopdata1.xls") Set objWorksheet = objWorkbook.Worksheets("Sheet2") objWorksheet.cells(1,"A") = "Agents" objWorksheet.cells(1,"B") = "Password" objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0") objConnection.Open "C:Documents and SettingsTest classDesktopFlights.mdb" objRecordset.Open "Select Agent, Password from Login", objConnection i = 2 'Rows Do Until objRecordset.EOF objWorksheet.cells(i, "A") = objRecordset.Fields("Agent") objWorksheet.cells(i, "B") = objRecordset.Fields("Password") i=i+1 objRecordset.MoveNext Loop www.gcreddy.net
  • 4. www.gcreddy.com objWorkbook.Save objWorkbook.Close objExcel.Quit Set objExcel = Nothing objRecordset.Close objConnection.Close Set objRecordset = Nothing Set objConnection = Nothing ---------------------------------------------------- 5) Export Data from a Database Table to a Text file Option Explicit Dim objConnection, objRecordset Dim objFso,myFile Set objConnection = Createobject ("Adodb.Connection") Set objRecordset = Createobject ("Adodb.Recordset") Set objFso = CreateObject("Scripting.Filesystemobject") Set myFile=objFso.OpenTextFile("C:Documents and SettingsTest classDesktopdata1.txt",2) myFile.WriteLine "Agent "&" Password" myFile.WriteLine "--------------------" objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0") objConnection.Open "C:Documents and SettingsTest classDesktopFlights.mdb" objRecordset.Open "Select Agent, Password from Login", objConnection Do Until objRecordset.EOF myFile.WriteLine objRecordset.Fields("Agent")&" ," & objRecordset.Fields("Password") objRecordset.MoveNext Loop myFile.Close Set objFso=Nothing www.gcreddy.net
  • 5. www.gcreddy.com objRecordset.Close objConnection.Close Set objRecordset = Nothing Set objConnection = Nothing ------------------------------------------------ 6) Export Data from a Text File to a Database Table Option Explicit Dim objConnection, objCommand Dim objFso,myFile,myLine,myField,SNO,Agent,Password Set objConnection = Createobject ("Adodb.Connection") Set objCommand = Createobject ("Adodb.Command") Set objFso = CreateObject("Scripting.Filesystemobject") Set myFile=objFso.OpenTextFile("C:Documents and SettingsTest classDesktopdat1.txt",1) objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0") objConnection.Open "C:Documents and SettingsTest classDesktopFlights.mdb" myFile.SkipLine myFile.SkipLine Do While myFile.AtEndOfStream = FALSE myLine = myFile.ReadLine myField = Split(myLine,",") SNO = myField(0) Agent = myField(1) Password = myField(2) objCommand.ActiveConnection = objConnection objCommand.CommandText = "Insert into Login2 values('"&SNO&"','"&Agent&"','"&Password&"')" objCommand.Execute Loop myFile.Close Set objFso=Nothing Set objCommand = Nothing www.gcreddy.net