在Access导出

时间:2016-03-15 11:29:21

标签: vba ms-access access-vba

我想将Access查询结果导出到文本文件中,并在导出文件的第一行添加一些字符串。

具体来说,我想组合一个文本字符串:

 *Abc def

使用Access查询结果(制表符分隔):

DoCmd.TransferText acExportDelim, "Export_spec", "Export", "C:\export.txt", True, ""

然后将其另存为文本文件。

文本字符串必须位于文本文件的第一行,然后是访问查询结果。

结果如下:

*Abc def
Header1 Header2 Header3 Header4 ...
Value1 Value2 Value3 Value4 ...
... ... ... ... ...

1 个答案:

答案 0 :(得分:2)

你需要

  • 将数据导出到临时文件
  • 创建新文本文件
  • 将字符串写入新文本文件,然后
  • 将临时文件中的数据附加到新文件中。

实现这一目标的一种方法是在代码中使用FileSystemObject

Dim tempExportSpec as String
tempExportSpec = "C:\__tmp\tempexport.txt"
DoCmd.TransferText acExportDelim, "Export_spec", "Export", tempExportSpec, True, ""
Dim fso As New FileSystemObject
Dim finalFile As TextStream
Set finalFile = fso.CreateTextFile("C:\folder\export.txt")
finalFile.WriteLine "*Abc def"
Dim tempExport As TextStream
Set tempExport = fso.OpenTextFile(tempExportSpec, ForReading)
finalFile.Write tempExport.ReadAll
finalFile.Close
tempExport.Close
fso.DeleteFile tempExportSpec