0

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?

2 Answers 2

9

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
Sign up to request clarification or add additional context in comments.

Comments

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"

1 Comment

In Access 2016 this doesn't work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.