Export SQL statement from query to text file

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
 
G

Gary Walter

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
 
G

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
 
Top