Put this function into a standard module and then save the module as a different name than the function.
Code:
Function ExportText(TableOrQuery As String, FileAndPath As String, Optional HeaderInfo As String, Optional ExportSpec As String)
Dim i As Integer
Dim j As Integer
Dim strHold As String
Dim strFile As String
Dim strTempFile As String
Dim strFileOut As String
' sets a temporary file
strTempFile = CurrentProject.Path & "\temptext.csv"
' checks to see if there is an export specification
If Len(ExportSpec) > 0 Then
DoCmd.TransferText acExportDelim, ExportSpec, TableOrQuery, FileAndPath, False
Else
DoCmd.TransferText acExportDelim, , TableOrQuery, FileAndPath, False
End If
' gets the available file number
i = FreeFile
' opens the exported file to use for the temp file
Open FileAndPath For Input As #i
' gets the available file number
j = FreeFile
' opens the temp file to start adding the header info and rest of the exported text file
Open strTempFile For Output As #j
' if there is header info passed then it adds that
If Len(HeaderInfo) > 0 Then
strHold = HeaderInfo
Print #j, strHold
End If
' loops through the previously exported text file to get the values and put them in
' to the temp file
Do Until EOF(i)
Line Input #i, strHold
Print #j, strHold
Loop
Close #i
Close #j
' checks to see if the file exists and, if so, it deletes it
If Dir(FileAndPath) <> vbNullString Then
Kill FileAndPath
End If
' moves the temp file to the original text file name and path
Name strTempFile As FileAndPath
End Function
And then you can call it like this:
Code:
ExportText "TableOrQueryNameHere", "C:\Temp\YourFileNameToSaveAsHere.csv", "10,12323123," & Format(Date(), "yyyymmdd")
But if you want it tab delimited then you would need to set up an export specification for that table or query (see here for how) and then you would call that by name and your static info would also need to be passed slighly differently. So an example is:
Code:
ExportText "TableOrQueryNameHere", "C:\Temp\YourFileNameToSaveAsHere.csv", "10" & Chr(9) & "12323123" & Chr(9) & Format(Date(), "yyyymmdd"), "ExportSpecNameHereInQuotes"