Exporting a query result into a text file using vba-MS Access

风格不统一 提交于 2019-12-06 17:12:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!