Ms access 将数据导出到文件
我有一种情况,我需要将信息的标题行放入CSV文件中 在这之后,我需要向这个文件追加3个不同列号的查询 当前有此逻辑,但Ms access 将数据导出到文件,ms-access,ms-access-2010,Ms Access,Ms Access 2010,我有一种情况,我需要将信息的标题行放入CSV文件中 在这之后,我需要向这个文件追加3个不同列号的查询 当前有此逻辑,但TransferText行覆盖了我之前在文件中放置的内容: Dim fldr As String Dim dlg As Office.FileDialog Set dlg = Application.FileDialog(msoFileDialogFolderPicker) With dlg .AllowMultiSelect = False .Title =
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
对我来说,通过记录集迭代查询会更好吗?还是我在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_" & 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
方法执行的是文件输出
,而不是文件附加
操作。因此,TransferText
之前文件中的任何内容都将被清除并替换为方法的输出
是的,我必须在CSV的字符串周围设置文本限定符。是的,我构建了自己的VB6收集器……它是
Set=Nothing
(if object)和if RecordSet object的组合。关闭。它需要一个ParamArray
parrameter,所以如果我有一堆需要收尾(比如说),我可以用逗号分隔它们。