SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Andere mochten auch

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
 

Andere mochten auch (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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
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
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
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
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 

Kürzlich hochgeladen (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
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
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
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
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

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