Export Access query results to csv

陌路散爱 提交于 2020-01-01 15:03:55

问题


I have an access database which manipulates data from a Magento e-commerce store, reformats the data and (hopefully!) spits out a CSV file which can then be imported into ebay Turbolister for mass upload to eBay.

I have got as far as creating a query which correctly lays out the data into the format required by Turbolister.

My issues are various (including some which appear to be related to Access' handling of large field contents), however the crux of my problem is that I am struggling to get working a simple script which exports the query results as a properly formatted CSV (including doubling up on double quotes where required inside a text value i.e. if the value itself contains quotes which need to be retained).

The DoCmd.TransferText solution throws an error related to field size ('the field is too small to accept the amount of data you attempted to add') so thats no good.

Has anyone got a good working CSV export routine in VBA that they can suggest?

Cheers


回答1:


This is an old function I sometimes used to use, it allows you to specify the delimeter, it also checks the data it's outputting and if it can't be evaluated to either a date or a numeric etc, then it uses double quotes:

Public Function ExportTextDelimited(strQueryName As String, strDelimiter As String)

Dim rs          As Recordset
Dim strHead     As String
Dim strData     As String
Dim inti        As Integer
Dim intFile     As Integer
Dim fso         As New FileSystemObject

On Error GoTo Handle_Err

    fso.CreateTextFile ("C:\Untitled.csv")

    Set rs = Currentdb.OpenRecordset(strQueryName)

    rs.MoveFirst

    intFile = FreeFile
    strHead = ""

    'Add the Headers
    For inti = 0 To rs.Fields.Count - 1
        If strHead = "" Then
            strHead = rs.Fields(inti).Name
        Else
            strHead = strHead & strDelimiter & rs.Fields(inti).Name
        End If
    Next

    Open "C:\Untitled.csv" For Output As #intFile

    Print #intFile, strHead

    strHead = ""

    'Add the Data
    While Not rs.EOF

        For inti = 0 To rs.Fields.Count - 1
            If strData = "" Then
                strData = IIf(IsNumeric(rs.Fields(inti).value), rs.Fields(inti).value, IIf(IsDate(rs.Fields(inti).value), rs.Fields(inti).value, """" & rs.Fields(inti).value & """"))
            Else
                strData = strData & strDelimiter & IIf(IsNumeric(rs.Fields(inti).value), rs.Fields(inti).value, IIf(IsDate(rs.Fields(inti).value), rs.Fields(inti).value, """" & rs.Fields(inti).value & """"))
            End If
        Next

        Print #intFile, strData

        strData = ""

        rs.MoveNext
    Wend

        Close #intFile

rs.Close
Set rs = Nothing

'Open the file for viewing
Application.FollowHyperlink "C:\Untitled.csv"   

Exit Function

Handle_Err:
MsgBox Err & " - " & Err.Description

End Function

It may need a couple of tweaks as I've taken out some bits which were only relevant to my particular case but this may be a starting point.



来源:https://stackoverflow.com/questions/13309994/export-access-query-results-to-csv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!