Public Sub ExportTableAsCSV_InSpecialFormat(ByVal TableName As String)
    'Exports the specified table in the current database to the same folder as the current database.
    'The export format is: text, comma-separated, no text qualifier, field names on first row.
 
    'Look for the specified table in the current db.
    Dim TD As TableDef: Set TD = Nothing
    On Error GoTo Err_TblNotExist:
    Set TD = CurrentDb.TableDefs(TableName)
    On Error GoTo 0
    If TD Is Nothing Then GoTo Err_TblNotExist:
 
    'Set full filepath for export
    Dim ExportFilePath As String
    ExportFilePath = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "\")) & TableName & ".csv"
 
    'Create temporary export specification, for a CSV in a specific format
    Dim TempExportSpec As String
    TempExportSpec = ExportSpec_Add(FieldSeparator:=",", FileType:=1252, TextDelim:="", StartRow:=0)
 
    'Export the table
    DoCmd.SetWarnings False
    DoCmd.TransferText acExportDelim, TempExportSpec, TableName, ExportFilePath
    DoCmd.SetWarnings True
 
    'Delete the temporary export specification
    Call ExportSpec_Delete(TempExportSpec)
 
    Exit Sub
 
Err_TblNotExist:
    Err.Raise 999, , "Table """ & TableName & """ does not exist!"
    Exit Sub
 
End Sub
 
Function ExportSpec_Add( _
                    Optional ByVal DateDelim As String = "/", _
                    Optional ByVal DateFourDigitYear As Boolean = True, _
                    Optional ByVal DateLeadingZeros As Integer = 0, _
                    Optional ByVal DateOrder As Integer = 0, _
                    Optional ByVal DecimalPoint As String = ".", _
                    Optional ByVal FieldSeparator As String = ",", _
                    Optional ByVal FileType As Long = 1252, _
                    Optional ByVal SpecName As String = "", _
                    Optional ByVal SpecType As Integer = 1, _
                    Optional ByVal StartRow As Integer = 0, _
                    Optional ByVal TextDelim As String = "", _
                    Optional ByVal TimeDelim As String = ":") As String
    'Adds an export specification (for use in text file export) to the current
    '   database, with the specified options.
    'Returns the name of the new export specification as a string.
    '   If the SpecName parameter is nonblank then that is the name that will be used.
    '   Otherwise, the specification name will be both random and time-dependent.
 
    'Set the new specification name to use
    Dim SpecName_ToUse As String
    If SpecName = "" Then
        SpecName_ToUse = "TempSpec-" & Format(Now(), "yyyymmddhhmmss") & "-" & Format(CLng(Rnd() * 100000), "000000")
    Else
        SpecName_ToUse = SpecName
    End If
 
    'If other fields are passed as blank strings then change them to Null
    Dim DateDelim_ToUse As String:      DateDelim_ToUse = IIf(DateDelim = "", "Null", """" & DateDelim & """")
    Dim TextDelim_ToUse As String:      TextDelim_ToUse = IIf(TextDelim = "", "Null", """" & TextDelim & """")
    Dim TimeDelim_ToUse As String:      TimeDelim_ToUse = IIf(TimeDelim = "", "Null", """" & TimeDelim & """")
 
    'Add a new specification to the hidden table that stores export specs
    Call DoCmd.SetWarnings(False)
    DoCmd.RunSQL "INSERT INTO MSysIMEXSpecs ( " & _
                    "DateDelim, DateFourDigitYear, DateLeadingZeros, DateOrder, " & _
                    "DecimalPoint, FieldSeparator, FileType, SpecName, SpecType, " & _
                    "StartRow, TextDelim, TimeDelim ) " & vbCrLf & _
                "SELECT " & _
                    DateDelim_ToUse & " AS Expr1, " & _
                    CInt(DateFourDigitYear) & " AS Expr2, " & _
                    DateLeadingZeros & " AS Expr3, " & _
                    DateOrder & " AS Expr4, " & _
                    """" & DecimalPoint & """ AS Expr5, " & _
                    """" & FieldSeparator & """ AS Expr6, " & _
                    FileType & " AS Expr7, " & _
                    """" & SpecName_ToUse & """ AS Expr8, " & _
                    SpecType & " AS Expr9, " & _
                    StartRow & " AS Expr10, " & _
                    TextDelim_ToUse & " AS Expr12, " & _
                    TimeDelim_ToUse & " AS Expr11 " & vbCrLf & _
                "FROM MSysIMEXSpecs;"
    Call DoCmd.SetWarnings(True)
 
    'Set return value
    ExportSpec_Add = SpecName_ToUse
 
End Function
 
Sub ExportSpec_Delete(ByVal ExportSpecName As String)
    'Deletes the named export specification
    Call DoCmd.SetWarnings(False)
    DoCmd.RunSQL "DELETE MSysIMEXSpecs.* FROM MSysIMEXSpecs WHERE SpecName=""" & ExportSpecName & """;"
    Call DoCmd.SetWarnings(True)
End Sub