问题
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)
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?
回答1:
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
回答2:
You do it with DoCmd.TransferText method. Like:
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"
来源:https://stackoverflow.com/questions/17272945/exporting-a-query-result-into-a-text-file-using-vba-ms-access