【问题标题】:Export Data to File将数据导出到文件
【发布时间】:2012-09-12 00:55:19
【问题描述】:

我有一种情况需要将标题行信息放入 CSV 文件中。

之后,我需要向该文件附加 3 个不同列号的查询。

目前有这个逻辑,但是TransferText 行覆盖了我之前放在文件中的内容:

Dim fldr As String

Dim dlg As Office.FileDialog
Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
With dlg
    .AllowMultiSelect = False
    .Title = "Select a Folder:"
    .Filters.Clear
    '.Filters.Add "CSV Files", "*.csv"

    If .show = True Then
        fldr = .SelectedItems(1)
    End If
End With
GC dlg

'TODO: Remove after Debugging is complete
RaiseAlert "Folder chosen: " & fldr
'-----------------------------------------

Dim file As String
file = fldr & "\Export_DelaGet_" & Format(Now(), "yyyy_mm_dd") & ".csv"

'TODO: Remove after Debugging is complete
RaiseAlert "File: " & file
'-----------------------------------------

'TODO: OpenFile and output the header line
Open file For Output As #1
Print #1, """SYS"",""Some Data""" & vbCrLf
Close 1

'Output Query/View Results to file
DoCmd.TransferText acExportDelim, "MstPrc_Spec", "vwMasterPrices_Output", file, False

通过 RecordSet 遍历查询对我来说会更好还是我错过了 TransferText 中的某些内容?

【问题讨论】:

    标签: ms-access ms-access-2010


    【解决方案1】:

    除非其他人可以为我提供更好的执行此操作的方法,否则这是我目前所拥有的。

    Dim fldr As String
    
    Dim dlg As Office.FileDialog
    Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
    With dlg
        .AllowMultiSelect = False
        .Title = "Select a Folder:"
        .Filters.Clear
        '.Filters.Add "CSV Files", "*.csv"
    
        If .show = True Then
            fldr = .SelectedItems(1)
        End If
    End With
    GC dlg
    
    'TODO: Remove after Debugging is complete
    '    RaiseAlert "Folder chosen: " & fldr
    '-----------------------------------------
    
    Dim file As String
    file = fldr & "\Export_" & Format(Now(), "yyyy_mm_dd") & ".csv"
    
    'TODO: Remove after Debugging is complete
    '    RaiseAlert "File: " & file
    '-----------------------------------------
    
    'TODO: OpenFile and output the header line
    Open file For Output As #1
    Print #1, """SYS"",""Some Data""" & vbCrLf
    Close 1
    
    Open file For Append As #2
    Dim rst As DAO.Recordset, str As String
    
    'Append MasterPrices
    Set rst = CurrentDb.OpenRecordset("vwMasterPrices_Output")
    If rst.RecordCount > 0 Then
        Do While Not rst.EOF
            str = """" & rst(0) & """,""" & rst(1) & """,""" & rst(2) & """,""" & rst(3) & """,""" & rst(4) & """," & Format(rst(5), "##0.00")
    
            Print #2, str
    
            'Move Next
            rst.MoveNext
        Loop
    End If
    
    'Append GroupPrice
    Set rst = CurrentDb.OpenRecordset("vwGroupPrice_Output")
    If rst.RecordCount > 0 Then
        Do While Not rst.EOF
            str = """" & rst(0) & """,""" & rst(1) & """,""" & rst(2) & """," & Format(rst(3), "##0.00")
    
            Print #2, str
    
            'Move Next
            rst.MoveNext
        Loop
    End If
    
    'Append GroupLocations
    Set rst = CurrentDb.OpenRecordset("vwGroupLocations_Output")
    If rst.RecordCount > 0 Then
        Do While Not rst.EOF
            str = """" & rst(0) & """,""" & rst(1) & """," & rst(2)
    
            Print #2, str
            'Move Next
            rst.MoveNext
        Loop
    End If
    
    GC rst
    Close 2
    

    不幸的是TransferText 方法执行File-Output 而不是File-Append 操作。因此,TransferText 之前文件中的所有内容都将被清除并替换为方法的输出。

    是的,我必须在 CSV 的字符串周围设置文本限定符。

    【讨论】:

    • 是的,我构建了自己的 VB6 收集器......它是 Set <var> = Nothing(如果是对象)和 RecordSet 对象 <var>.close 的组合。它需要一个ParamArray 参数,所以如果我有一堆我需要关闭(按说),我可以用逗号分隔它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 2017-10-16
    • 2014-08-07
    • 1970-01-01
    • 2013-02-19
    • 2014-10-20
    相关资源
    最近更新 更多