View Single Post
Old 03-02-2009, 05:28 PM   #18
datAdrenaline
AWF VIP
 
Join Date: Jun 2008
Location: Northern Virginia
Posts: 697
Thanks: 0
Thanked 27 Times in 12 Posts
datAdrenaline is a jewel in the rough datAdrenaline is a jewel in the rough datAdrenaline is a jewel in the rough
Re: Export a query output as csv format

Hello Danmac ...

Sorry to be so long on a reply! ... I have been busy of late, and I am just getting around to returning to AWF ... so ... in response to ...

>> Could this possibly be an indicator I need to use your ADO version? <<

Yep ... you definately need the ADO version with an ADP ... so ... give this a shot:


Code:
Public Function ExportToCSV_A(strSource As String, _
                            strFileName As String, _
                            Optional strColumnDelimiter As String = ",", _
                            Optional blHeaders As Boolean = False) As Byte
'Exports a table or query or SQL statement to a text file.  If a SQL is passed
'as the source, enclose it in Parenthesis.
    
    Dim intChannel As Integer
    Dim strSQL As String
    Dim strHeaders As String
    Dim x As Integer
    
    'Close any open files
    For intChannel = 1 To 511
        Close #intChannel
    Next intChannel
       
    'Open a channel to communicate with your TEMP file and
    intChannel = FreeFile
    Open strFileName For Output Access Write As #intChannel
     
    'Write the contents of the table to the file
    'Open the source
    strSQL = "SELECT * FROM " & strSource & " As vTbl"
    
    With CurrentProject.Connection.Execute(strSQL, , 1) 'adCmdText = 1
                
        'Build Headers if appropriate
        If blHeaders = True Then
            For x = 0 To .Fields.Count - 1
                strHeaders = strHeaders & strColumnDelimiter & .Fields(x).Name
            Next
            strHeaders = Mid(strHeaders, Len(strColumnDelimiter) + 1) & vbCrLf
        End If
    
        'Write to the CSV file
        Print #intChannel, strHeaders & .GetString(2, , strColumnDelimiter, vbCrLf, "<NULL>") 'adClipString = 2
        
    End With
           
    'Close all the files
    Close #intChannel
    
End Function
Hope it helps you out!

__________________
Brent Spaulding | datAdrenaline |
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
datAdrenaline is offline   Reply With Quote
The Following User Says Thank You to datAdrenaline For This Useful Post:
tucker61 (09-12-2019)