2012-09-11 72 views
0

我有一種情況,我需要將一個標題行信息放入一個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中的一些內容?

回答

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文本限定符。

+0

是的,我建立了我自己的VB6收藏家....它的組合'​​Set = Nothing'(如果對象),並且如果RecordSet對象' .close'。它需要一個'ParamArray'參數,所以如果我有一堆我需要關閉(每說),我可以用逗號分隔它們。 – GoldBishop