I want to save the file with an extension other than .csv.
You might be interested in the following function:
Function ExportToText(strQuery As String, _
strExportTo As String, _
strDelim As String, _
blnQuoteText As Boolean, _
Optional blnHasFieldNames As Boolean = True)
' Accepts:
' strQuery - Text - name of query to be exported
' strExportTo - Text- path to file to export to
' strDelim - Text - delimiter character(s) to separate fields
' blnQuoteText - Boolean - True to enclose text or memo data in quotes
' blnHasFieldNames - Boolean - (optional) - True (default) to export field names as first line
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Dim Fld As DAO.Field
Dim n As Integer
Dim strPrintList As String
Dim strQuote As String
Set dbs = CurrentDb
Set qdf = dbs.QueryDefs(strQuery)
' evaluate query's parameters, if any
For Each prm In qdf.Parameters
prm = [removed_js]
Next prm
Set rst = qdf.OpenRecordset
If Dir(strExportTo) <> "" Then
If MsgBox("Overwrite " & strExportTo & "?", vbQuestion + vbYesNo, "Export Query") = vbYes Then
Kill strExportTo
ElseIf MsgBox("Append rows to " & strExportTo & "?", vbQuestion + vbYesNo, "Export Query") = vbNo Then
Exit Function
End If
End If
With rst
If Not (.BOF And .EOF) Then
Open strExportTo For Append As #1
If blnHasFieldNames Then
' include column headings in text file
For n = 0 To qdf.Fields.Count - 1
strPrintList = strPrintList & strDelim & qdf.Fields(n).Name
Next n
' remove leading delimiter
strPrintList = Mid$(strPrintList, Len(strDelim) + 1)
Print #1, strPrintList
strPrintList = ""
End If
Do While Not .EOF
For n = 0 To qdf.Fields.Count - 1
Set Fld = .Fields(n)
strQuote = IIf(blnQuoteText And (Fld.Type = dbText Or Fld.Type = dbMemo), """", "")
strPrintList = strPrintList & strDelim & strQuote & \_
.Fields(n) & strQuote
Next n
' remove leading delimiter
strPrintList = Mid$(strPrintList, Len(strDelim) + 1)
Print #1, strPrintList
strPrintList = ""
.MoveNext
Loop
Close #1
End If
End With
Exit_Here:
rst.Close
End Function
This writes the rows from a query line by line to a file, which can be given any extension you wish.