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.
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.
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.
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.
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With