LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default copy recordset to CSV file

Hi, I have the following code below which happily breaks down a CSV
into different worksheets in excel. However, what I want the VBA code
to do is copy the recordset to new CSV files, my code already creates
the new CSV files, but how do I send the data to the csv file and not
the worksheet? Thanks

Code:

Option Explicit

Sub ImportLargeFile()
Dim strFilePath As String, strFilename As String, strFullPath As
String
Dim oFile As TextStream
Dim lngCounter As Long
Dim oConn As Object, oRS As Object, oFSObj As Object
Dim xFile As Integer
Dim xFileName As String
Dim fso As FileSystemObject
Set fso = New FileSystemObject
Dim xStr As String
xFile = 1
xFileName = "C:\Users\Steve\Desktop\New folder\output"
'Get a text file name
strFullPath = "C:\Users\Steve\Desktop\New folder\test.csv"



'This gives us a full path name e.g. C:tempfolderfile.txt
'We need to split this into path and file name
Set oFSObj = CreateObject("SCRIPTING.FILESYSTEMOBJECT")

strFilePath = oFSObj.GetFile(strFullPath).ParentFolder.Path
strFilename = oFSObj.GetFile(strFullPath).Name
Debug.Print (strFilePath)
Debug.Print (strFilename)

'Open an ADO connection to the folder specified
Set oConn = CreateObject("ADODB.CONNECTION")
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strFilePath & ";" & _
"Extended Properties=""text;HDR=Yes;FMT=Delimited"""

Set oRS = CreateObject("ADODB.RECORDSET")

'Now actually open the text file and import into Excel
oRS.Open "SELECT * FROM " & strFilename, oConn, 3, 1, 1
While Not oRS.EOF
xFileName = "C:\Users\Steve\Desktop\New folder\output" & xFile
& ".csv"
Set oFile = fso.CreateTextFile(xFileName, True)
Worksheets(xFile).Range("A1").CopyFromRecordset oRS, 10000
xFile = xFile + 1
Wend

oRS.Close
oConn.Close

Application.ScreenUpdating = True


End Sub
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot use recordset twice with copy From recordset JasonC Excel Programming 1 February 3rd 10 11:53 PM
Copy from recordset errors in ADO Spike Excel Programming 8 March 10th 07 10:12 PM
Copy an array into an ADO recordset? quartz[_2_] Excel Programming 3 January 4th 06 08:10 AM
Copy From Recordset Nath Excel Programming 0 July 13th 04 12:30 PM
Copy Recordset into array? Andrew_blue Excel Programming 2 May 6th 04 07:23 PM


All times are GMT +1. The time now is 03:51 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"