Doorgaan naar hoofdcontent

Access: Overcoming The Max Length (64) of DoCmd.TransferText

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")

After that I let the script copy the existing CSV files to their aliasses file1.csv etc.

 '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

Populaire posts van deze blog

Excel: VBA script om wachtwoord te verwijderen

Af en toe krijg ik een vraag om een wachtwoord van een Excel blad te halen. Doodsimpel met VBA. Hier een script dat ik gebruik: Sub WachtwoordCrack()     Dim a As Integer, b As Integer, c As Integer, d As Integer, _     e As Integer, f As Integer, g As Integer, h As Integer, _  I As Integer, j As Integer, k, m As Integer     Dim begin As Date, eind As Date     Dim duur As String     Dim objSheet As Worksheet     begin = TimeValue(Time)     On Error Resume Next     For Each objSheet In Application.Worksheets         For a = 65 To 66: For b = 65 To 66: For c = 65 To 66             For d = 65 To 66: For e = 65 To 66: For f = 65 To 66                 For g = 65 To 66: For h = 65 To 66: For I = 65 To 66                     For j = 65 To 66: For k = 65 To 66: For m = 32 To 126                         ActiveSheet.Unprotect Chr(a) & Chr(b) & _   Chr(c) & Chr(d) & Chr(e) & Chr(f) & _   Chr(g) & Chr(h) &  Chr(I) & Chr(j) & C

Crystal Reports: On SQL Expression Fields

This is based on an article from the SAP site: http://search.sap.com/notes?id=0001217871&boj=/sap/bc/bsp/spn/scn_bosap/notes.do?access=69765F6D6F64653D3939382669765F7361706E6F7465735F6E756D6265723D30303031323137383731 I tried to add some more examples to it. SQL Expression Field definition SQL Expression fields are similar to formula fields, but they are written in Structured Query Language (SQL). They are useful in optimizing report performance because the tasks they execute are performed on the database server. You can use SQL Expression fields to perform pre-defined functions on database fields. The list of available functions depends on the type of database in use. This list is available in the Function Tree of the SQL Expression Editor dialog box. NOTE : If Crystal Reports recognizes the function name it will turn blue in the SQL Expression Editor dialog box. Additional database functions may be available to you, depending on your database, but they will

Excel 2013: uniek aantal in draaitabel

Tot en met versie 2010 was het in Excel lastig om in een draaitabel een uniek aantal (DISTINCT COUNT) te tellen. We geven een voorbeeld op basis van een verkoperslijst. In deze lijst kunnen we zien welke verkopers welke artikelen hebben verkocht. Willen we nu in een draaitabel laten zien hoeveel artikelen een verkoper heeft verkocht, dan krijgen we wel de aantallen maar niet de unieke aantallen te zien. Om toch de unieke aantallen te laten zien, hebben we een aantal stappen nodig. Op het moment dat we de draaitabel invoegen, krijgen we in Excel 2013 dit dialoogvenster: Onderaan zien we daar een nieuwe optie: Deze gegevens toevoegen aan het gegevensmodel . Deze optie moeten we aanvinken, voor we op OK klikken. We krijgen dan een iets ander beeld dan normaal: Normaliter krijgen we alleen de veldnamen. Nu zien we er het woord Bereik boven staan. Voor het voorbeeld heb ik nu Verkoper toegevoegd aan Rijen en Artikelomschrijving aan Waarden . Het resultaat is identiek