【问题标题】:Exporting tables to csv changes file extension将表导出为 csv 更改文件扩展名
【发布时间】:2020-02-24 21:32:27
【问题描述】:

我正在尝试将我的所有表从我的访问数据库中导出到单独的 .csv 文件中。我有一个遍历所有表的循环,通过使用 TransferText 我想为每个表创建一个 .csv 文件。

我可以通过编写 TransferText 方法来创建单个文件。

DoCmd.TransferText acExportDelim, "ExportCsv", [Table name], filePath + "Test.csv", True

但是当我试图创建一个循环来为每个表生成一个文件时,我遇到了麻烦。 (文件路径设置为桌面)

' Loops through all tables and extracts them as .csv-files    
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Set db = CurrentDb
For Each tdf In db.TableDefs
    ' ignore system and temporary tables
    If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*") Then
        ' Export table as CSV
        'MsgBox (tdf.Name)
        fileName = tdf.Name & ".csv"
        DoCmd.TransferText acExportDelim, "ExportCsv", tdf.Name, filePath + fileName, True

    End If
Next
Set tdf = Nothing
Set db = Nothing

这样做会给我错误“3011”,说它找不到对象。然后它给了我对象名称:[表名]#csv。所以由于某种原因,它将“.csv”更改为“#csv”。

如果我从文件名中删除文件扩展名,我得到的只是错误 3027,表示对象或数据库是只读的。

有谁知道我的问题是否有解决方案或其他方法来做同样的事情?还是我必须走完全不同的路线?

编辑:

其他经过测试的变体

DoCmd.TransferText acExportDelim, "ExportCsv", tdf.Name, "C:/tempFile.csv", True 
DoCmd.TransferText acExportDelim, "ExportCsv", tdf.Name, "C:/" & tdf.Name & ".csv", True

:给出“#csv”错误。

DoCmd.TransferText acExportDelim, "ExportCsv", tdf.Name, "C:/tempFile", True
DoCmd.TransferText acExportDelim, "ExportCsv", tdf.Name, "C:/" & tdf.Name, True 

:给出只读错误

【问题讨论】:

    标签: vba csv ms-access


    【解决方案1】:

    这是已知的限制。 TransferText 不喜欢复杂的文件名。

    所以,导出为一个简单的文件名,然后重命名该文件为其最终名称:

    ExportFinal = "YourFinalName.csv"
    ExportTemp = "FileToRename.csv"
    
    DoCmd.TransferText acExportDelim, "ExportCsv", tdf.Name, ExportTemp, True
    
    VBA.FileCopy ExportTemp, ExportFinal
    VBA.Kill ExportTemp
    

    【讨论】:

    • 不幸的是,您在 TransferText 中如何编写文件名似乎并不重要,因为它似乎不喜欢处于 for 循环中。我给临时文件命名并不重要,只要它在循环中不喜欢文件扩展名前的点,它就永远不会写出正确的文件名。
    • 我不确定你的意思。但是尝试为每个循环使用不同的临时文件名。
    • 这也给了我一个错误。如果我将表名设置为文件名并添加“.csv”,则会出现“#csv”问题。如果我不添加文件扩展名,我会得到“数据库或对象是只读的”。
    • 您要导出多个表吗?如果是这样,你能告诉我你的代码吗?
    • 否,但您可以编辑问题以保留修改后的代码。
    【解决方案2】:

    所以经过多次反复试验,我找到了适合我的方法。

    在@Gustav 的一些启发下,我开始创建 .xls 文件,由于某种原因,该文件有效。然后使用自定义脚本将这些文件转换为 .csv 文件。然后我删除 .xls 文件,只留下我的 .csv 文件。

    所以我的循环现在看起来像这样:

    For Each tdf In db.TableDefs
        ' ignore system and temporary tables
        If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*") Then
            ' Export as xls-files
            fileName = tdf.Name & ".xls"
            DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, tdf.Name, filePath & env & fileName, True
    
            ' Convert xls-files to .csv and remove the xls-files.
            ConvertXls2CSV (filePath & env & fileName)
            VBA.Kill filePath & env & fileName
        End If
    Next
    

    这里是转换代码:(信用:https://www.devhut.net/2012/05/14/ms-access-vba-convert-excel-xls-to-csv/

    Function ConvertXls2CSV(sXlsFile As String)
        On Error Resume Next
        Dim oExcel          As Object
        Dim oExcelWrkBk     As Object
        Dim bExcelOpened    As Boolean    'Was Excel already open or not
        'Review 'XlFileFormat Enumeration' for more formats
        Const xlCSVWindows = 23 'Windows CSV Format
        Const xlCSV = 6 'CSV
        Const xlCSVMac = 22 'Macintosh CSV
        Const xlCSVMSDOS = 24 'MSDOS CSV
    
        Set oExcel = GetObject(, "Excel.Application")    'Bind to existing instance of Excel
    
        If Err.Number <> 0 Then    'Could not get instance of Excel, so create a new one
            Err.Clear
            'On Error GoTo Error_Handler
            Set oExcel = CreateObject("excel.application")
            bExcelOpened = False
        Else    'Excel was already running
            bExcelOpened = True
        End If
    
        'On Error GoTo Error_Handler
        oExcel.ScreenUpdating = False
        oExcel.Visible = False   'Keep Excel hidden from the user
        oExcel.Application.DisplayAlerts = False
    
        Set oExcelWrkBk = oExcel.Workbooks.Open(sXlsFile)
        'Note: you may wish to change the file format constant for another type declared
        'above based on your usage/needs in the following line.
        oExcelWrkBk.SaveAs Left(sXlsFile, InStrRev(sXlsFile, ".")) & "csv", xlCSVWindows, Local:=True
        oExcelWrkBk.Close False
    
        If bExcelOpened = False Then
            oExcel.Quit
        End If
    
    Error_Handler_Exit:
        On Error Resume Next
        Set oExcelWrkBk = Nothing
        Set oExcel = Nothing
        Exit Function
    
    Error_Handler:
        MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
                "Error Number: " & Err.Number & vbCrLf & _
                "Error Source: ConvertXls2CSV" & vbCrLf & _
                "Error Table: " & sXlsFile & vbCrLf & _
                "Error Description: " & Err.Description, _
                vbCritical, "An Error has Occurred!"
        Resume Error_Handler_Exit
    
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      相关资源
      最近更新 更多