The Problem
The other day I ran into a problem with DoCmd.TransferText: it will not accept text files having longer names that 64 characters. Possibly, because 64 characters is the maximum length of an object name in Access.You will get an error message:
So I tried to find a work around.
My Work Around
First, I created a table in Access to store information about the text files, in this particular case, CSV files.
The I created a VBA script which actually store the information in the table
strMapCSV = CurrentProject.Path & "\"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(strMapCSV)
For Each f1 In f.files
If Right(f1.Name, 3) = "csv" And Left(f1.Name, 4) <> "file" Then
strSQL = "insert into tblCSVfiles (CSVname, CSVsize,CSVdate,CSValias) "
strSQL = strSQL & " VALUES ('" & Replace(f1.Name, "'", "`") & "'," & f1.Size & ", #" & f1.DateLastModified & "#,'" & "file" & intCounter & ".csv')"
db.Execute (strSQL)
intCounter = intCounter + 1
End If
Next
So, in the table tblCSVfiles all CSV files got an alias like file1.csv etc.
Every time the script is rerun, which is actually done autamatically in this case, the script removes the existing aliasses:
'remove aliasses
Set f = fs.GetFolder(strMapCSV)
For Each f1 In f.files
If Left(f1.Name, 4) = "file" Then
Set f = fs.getfile(strMapCSV & f1.Name)
f.Delete
End If
Next
The script also empties the table tblCSVfiles:
db.Execute ("delete from tblcsvfiles")
'copy CSV's
intCounter = 1
Set f = fs.GetFolder(strMapCSV)
For Each f2 In f.files
If Right(f2.Name, 3) = "csv" Then
Set f = fs.getfile(strMapCSV & f2.Name)
f.copy strMapCSV & "bestand" & intCounter & ".csv"
intCounter = intCounter + 1
End If
Next
After that, I can pick any file from the table tblCSVfiles and use the alias to refer to the shorter named CSV version.
Problem solved.
An Even Better Work Around
As I posted my solution, I received an even better idea from Jack Stockton: the FileSystemObject has both a ShortPath and ShortName.The much shorter code looks like this:
strMapCSV = CurrentProject.Path & "\"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(strMapCSV)
For Each f1 In f.files
If f1.Name = "NOVIBAT_14454_20150615_030440-1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890.csv" Then
strFile = f1.ShortName
End If
Next
DoCmd.TransferText transfertype:=acImportDelim, _
SpecificationName:="glans", _
tablename:="tblGlans", _
FileName:=strMapCSV & strFile, _
hasfieldnames:=False
Reacties