Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export all MS Access SQL queries to text files

Tags:

sql

vba

ms-access

I have to document an MS Access database with many many macros queries, etc. I wish to use code to extract each SQL query to a file which is named the same as the query, eg if a query is named q_warehouse_issues then i wish to extract the SQL to a file named q_warehouse_issues.sql

I DO NOT WISH TO EXPORT THE QUERY RESULT SET, JUST THE SQL!

I know I can do this manually in Access, but i am tired of all the clicking, doing saveas etc.

like image 891
Pieter Nienaber Avatar asked Aug 14 '09 00:08

Pieter Nienaber


People also ask

How do I export an Access query to a text file?

In the Access Navigation Pane, right-click the source object, point to Export, and then click Text File. You can also launch the Export - Text File wizard by highlighting the source object in the Navigation Pane and then on the External Data tab, in the Export group, click Text File.

How do I export all Access queries?

Tip: You can also start the export process by right-clicking the object in the Navigation Pane and then clicking Export > Access. Access opens the Export - Access Database dialog box. In the File name box on the Export - Access Database dialog box, specify the name of the destination database and then click OK.

How do I export SQL query results to text file?

However, if you prefer to export SQL query results to a text file via a Wizard, we have your back. To begin with, right-click the database in SQL Server Management Studio or SSMS. Then, select the Import or Export data option and head to Export Data under Tasks. Next, open the SQL Server Import and Export wizard.

How can I export more than 65000 records from Access to text?

To export more than 65000 rows with formatting and layout then an option is to set up a query to export 65000 rows at a time into separate spreadsheets, then copy and paste together into one spreadsheet.


2 Answers

This should get you started:

  Dim db As DAO.Database   Dim qdf As DAO.QueryDef    Set db = CurrentDB()   For Each qdf In db.QueryDefs     Debug.Print qdf.SQL   Next qdf   Set qdf = Nothing   Set db = Nothing 

You can use the File System Object or the built-in VBA File I/O features to write the SQL out to a file. I assume you were asking more about how to get the SQL than you were about how to write out the file, but if you need that, say so in a comment and I'll edit the post (or someone will post their own answer with instructions for that).

like image 111
David-W-Fenton Avatar answered Sep 22 '22 21:09

David-W-Fenton


Hope this helps.

Public Function query_print() Dim db As Database Dim qr As QueryDef  Set db = CurrentDb  For Each qr In db.QueryDefs   TextOut (qr.Name)   TextOut (qr.SQL)   TextOut (String(100, "-")) Next End Function  Public Sub TextOut(OutputString As String)      Dim fh As Long      fh = FreeFile     Open "c:\File.txt" For Append As fh     Print #fh, OutputString     Close fh  End Sub 
like image 42
yoonjinl Avatar answered Sep 18 '22 21:09

yoonjinl