问题
I'm trying to export all my tables, from my access database, to separate .csv-files. I have a loop that runs through all tables and by using TransferText I want to create a .csv-file for each table.
I am able to create a single file by writing the TransferText method.
DoCmd.TransferText acExportDelim, "ExportCsv", [Table name], filePath + "Test.csv", True
But when I'm trying to create a loop to generate a file for each table I get into trouble. (Filepath is set to desktop)
' 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
Doing it like this gives me Error '3011' saying it can't find the object. Then it gives me the object name: [table name]#csv. So for some reason it changes ".csv" to "#csv".
If I remove the file extension from the file name all I get is Error 3027 saying that the object or database is read-only.
Does anyone know if there is a solution to my problem or another way to do the same thing? Or am I gonna have to go a completely different route?
EDIT:
Other tested variations
DoCmd.TransferText acExportDelim, "ExportCsv", tdf.Name, "C:/tempFile.csv", True
DoCmd.TransferText acExportDelim, "ExportCsv", tdf.Name, "C:/" & tdf.Name & ".csv", True
: Gives a "#csv" error.
DoCmd.TransferText acExportDelim, "ExportCsv", tdf.Name, "C:/tempFile", True
DoCmd.TransferText acExportDelim, "ExportCsv", tdf.Name, "C:/" & tdf.Name, True
: Gives a read only error
回答1:
This is known limitation. TransferText doesn't like convoluted filenames.
So, export to a simple filename, then rename that file to its final name:
ExportFinal = "YourFinalName.csv"
ExportTemp = "FileToRename.csv"
DoCmd.TransferText acExportDelim, "ExportCsv", tdf.Name, ExportTemp, True
VBA.FileCopy ExportTemp, ExportFinal
VBA.Kill ExportTemp
回答2:
So after lots of trial and error I have found a way that works for me.
With some inspiration from @Gustav I went with creating .xls files, which for some reason works. And then convert those files with a custom script to .csv-files. Then I remove the .xls files leaving only my .csv-files left.
So my loop now looks like this:
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
And here is the converting code: (Credit to: 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
来源:https://stackoverflow.com/questions/58610890/exporting-tables-to-csv-changes-file-extension