Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export data to Excel

Status
Not open for further replies.

astonmak

MIS
Feb 14, 2003
41
0
0
CN
Hi all,

Is there any function/method to export data from recordset to excel?

Thanks
Aston
 
Hi,
I could not find any method but as Excel is an COM server, we can add it in references and create an instance of Excel. Then u can save ur data to excel sheet. your code could be like this.....

Dim Rs As Recorset
Dim oxl As Excel.Application
Dim owb As Excel.Workbook
Dim osh As Excel.Worksheet
Dim FileName as String

Set oxl = CreateObject("excel.application")
Set owb = oxl.Workbooks.Add
Set osh = owb.ActiveSheet

osh.Cells(1, 1).Value = Rs.Fields(0).Value
osh.Cells(1, 2).Value = Rs.Fields(1).Value
osh.Cells(1, 3).Value = Rs.Fields(2).Value
osh.Cells(1, 4).Value = Rs.Fields(3).Value
osh.Cells(1, 5).Value = Rs.Fields(4).Value
osh.Cells(1, 6).Value = Rs.Fields(5).Value

'Save workbook

owb.SaveAs CD1(2).FileName
Set osh = Nothing
owb.Close
oxl.Quit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top