Export SQL statement from query to text file

  • Thread starter Thread starter W Dean
  • Start date Start date
W

W Dean

Hi,

Is there an easy way to save a query or queries (ie, the sql from them) as a
text file?

I tried using documenter but it doesn't export delete statements and also
seems to truncate some queries,

W Dean
 
What's wrong with the good old copy/paste routine?

Switching to SQL view automatically selects the block of text. Ctrl-C , open Wordpad or similar,
Ctrl-V, Ctrl-S.

hth
Andrew L.
 
W Dean said:
Hi,

Is there an easy way to save a query or queries (ie, the sql from them) as a
text file?

I tried using documenter but it doesn't export delete statements and also
seems to truncate some queries,
Hi W Dean,

Roy Fisher provided this routine

Private Sub SaveQueries_Click()
Dim qdf As DAO.querydef
Dim destdir As String
Dim fn As Integer
destdir = "c:\"

For Each qdf In CurrentDb.QueryDefs
fn = FreeFile()
Open destdir & qdf.Name & ".TXT" For Output As #fn
Print #fn, qdf.SQL
Close #fn
Next
End Sub

You could play with this if you wanted
to save all queries in one file,
or provide a path in sub parameter.

It also would not hurt to add err-handling code

and
Set qdf = Nothing
Reset fn
instructions at end.

Good luck,

Gary Walter
 
that should have been

Reset 'fn

Gary Walter said:
as
Hi W Dean,

Roy Fisher provided this routine

Private Sub SaveQueries_Click()
Dim qdf As DAO.querydef
Dim destdir As String
Dim fn As Integer
destdir = "c:\"

For Each qdf In CurrentDb.QueryDefs
fn = FreeFile()
Open destdir & qdf.Name & ".TXT" For Output As #fn
Print #fn, qdf.SQL
Close #fn
Next
End Sub

You could play with this if you wanted
to save all queries in one file,
or provide a path in sub parameter.

It also would not hurt to add err-handling code

and
Set qdf = Nothing
Reset fn
instructions at end.

Good luck,

Gary Walter
 
Back
Top