Sub ExportDelimitedText( _
pRecordsetName As String, _
pFilename As String, _
Optional pBooIncludeFieldnames As Boolean, _
Optional pBooDelimitFields As Boolean, _
Optional pFieldDeli As String)
'BASIC USEAGE
' ExportDelimitedText "QueryName", "c:\path\filename.csv"
'set up error handler
On Error GoTo ExportDelimitedText_error
Dim mPathAndFile As String, mFileNumber As Integer
Dim r As Recordset, mFieldNum As Integer
Dim mOutputString As String
Dim booDelimitFields As Boolean
Dim booIncludeFieldnames As Boolean
Dim mFieldDeli As String
booDelimitFields = Nz(pBooDelimitFields, False)
booIncludeFieldnames = Nz(pBooIncludeFieldnames, False)
'make the delimiter a TAB character unless specified
If Nz(pFieldDeli, "") = "" Then
mFieldDeli = Chr(9)
Else
mFieldDeli = pFieldDeli
End If
'if there is no path specfied, put file in current directory
If InStr(pFilename, "\") = 0 Then
mPathAndFile = CurrentProject.Path
Else
mPathAndFile = ""
End If
mPathAndFile = mPathAndFile & "\" & pFilename
'if there is no extension specified, add TXT
If InStr(pFilename, ".") = 0 Then
mPathAndFile = mPathAndFile & ".txt"
End If
'get a handle
mFileNumber = FreeFile
'close file handle if it is open
'ignore any error from trying to close it if it is not
On Error Resume Next
Close #mFileNumber
On Error GoTo ExportDelimitedText_error
'delete the output file if already exists
If Dir(mPathAndFile) <> "" Then
Kill mPathAndFile
DoEvents
End If
'open file for output
Open mPathAndFile For Output As #mFileNumber
'open the recordset
Set r = CurrentDb.OpenRecordset(pRecordsetName)
'write fieldnames if specified
If booIncludeFieldnames Then
mOutputString = ""
For mFieldNum = 0 To r.Fields.Count - 1
If booDelimitFields Then
mOutputString = mOutputString & """" _
& r.Fields(mFieldNum) & """" & mFieldDeli
Else
mOutputString = mOutputString _
& r.Fields(mFieldNum).Name & mFieldDeli
End If
Next mFieldNum
'remove last delimiter
mOutputString = Left(mOutputString, Len(mOutputString) - Len(mFieldDeli))
'write a line to the file
Print #mFileNumber, mOutputString
End If
'loop through all records
Do While Not r.EOF()
'tell OS (Operating System) to pay attention to things
DoEvents
mOutputString = ""
For mFieldNum = 0 To r.Fields.Count - 1
If booDelimitFields Then
Select Case r.Fields(mFieldNum).Type
'string
Case 10, 12
mOutputString = mOutputString & """" _
& r.Fields(mFieldNum) & """" & mFieldDeli
'date
Case 8
mOutputString = mOutputString & "#" _
& r.Fields(mFieldNum) & "#" & mFieldDeli
'number
Case Else
mOutputString = mOutputString _
& r.Fields(mFieldNum) & mFieldDeli
End Select
Else
mOutputString = mOutputString & r.Fields(mFieldNum) & mFieldDeli
End If
Next mFieldNum
'remove last TAB
mOutputString = Left(mOutputString, Len(mOutputString) - Len(mFieldDeli))
'write a line to the file
Print #mFileNumber, mOutputString
'move to next record
r.MoveNext
Loop
'close the file
Close #mFileNumber
'close the recordset
r.Close
'release object variables
Set r = Nothing
MsgBox "Done Creating " & mPathAndFile, , "Done"
Exit Sub
'ERROR HANDLER
ExportDelimitedText_error:
MsgBox Err.Description, , "ERROR " & Err.Number & " ExportDelimitedText"
'press F8 to step through code and correct problem
Stop
Resume
End Sub