Pages

Showing posts with label Export Sql Query To Delimit Text File. Show all posts
Showing posts with label Export Sql Query To Delimit Text File. Show all posts

Wednesday, December 28, 2011

Export Sql Query To Delimit Text File

If you export the result of query to the text file.

Here is the code-


Sub export_query_to_text_file()
DoCmd.SetWarnings (False)
Dim temp_table_name As String
Dim exportfile As String
temp_table_name = Format(Now(), "dd_mmm_yyyy_hh_ss")
exportfile = "C:\Users\ashishkoul\Desktop\learn\rep_name_a.txt"
' check if the any excel file aready exists with same name
If Dir(exportfile) <> "" Then
MsgBox "File already exists"
Exit Sub
End If
' create a new temp table from query
DoCmd.RunSQL "SELECT * INTO " & temp_table_name & " FROM sales_detail WHERE [rep name] = 'a'"
DoCmd.TransferText acExportDelim, "", temp_table_name, exportfile
On Error Resume Next
'close the table if open
DoCmd.Close acTable, temp_table_name, acSaveYes
On Error Resume Next
' delete the temp table
CurrentDb.TableDefs.Delete temp_table_name
DoCmd.SetWarnings (True)
End Sub


Download Access Database