使用 vba-MS Access 将查询结果导出到文本文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17272945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exporting a query result into a text file using vba-MS Access
提问by Kaja
I have a Table in MS Access 2010 and I want to export the result of a query into a text file( the user specified a path and this Textfile should be saved in this Path)
我在 MS Access 2010 中有一个表,我想将查询结果导出到一个文本文件中(用户指定了一个路径,这个文本文件应该保存在这个路径中)
Here is my Query:
这是我的查询:
SELECT Name FROM MyTable
and I want to have each name in a seprate row in a text file. How can I do that in VBA?
我想将每个名称都放在文本文件的单独行中。我怎样才能在 VBA 中做到这一点?
回答by Gord Thompson
In this particular case the most straightforward approach would be something like this:
在这种特殊情况下,最直接的方法是这样的:
Sub ExportToText()
Dim rst As DAO.Recordset
Open "C:\__tmp\names.txt" For Output As #1
Set rst = CurrentDb.OpenRecordset("SELECT [Name] FROM MyTable", dbOpenSnapshot)
Do While Not rst.EOF
Print #1, rst!Name
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Close #1
End Sub
回答by Clon
You do it with DoCmd.TransferText method. Like:
您可以使用 DoCmd.TransferText 方法来完成。喜欢:
DoCmd.TransferText acExportDelim,"mySpecification","myView","C:\DATA\myfile.csv",True
First, you do it once manually, and save the specification (where you decide which columns to export, types, etc.).
首先,您手动执行一次,并保存规范(您决定导出哪些列、类型等)。
MyView is a view you create as "SELECT Name FROM myTable"
MyView 是您创建为“SELECT Name FROM myTable”的视图