2016-06-06 28 views
0

我正在使用Access VBA和傳輸文本方法導入數千個CSV文件。我需要將文件名或其格式添加到表中以反映它來自的文件。我已經存儲了變量文件名以便導入它。如何追加代碼以引用「fil」變量的名稱?在Access中導入CSV時設置字段值

With DoCmd 
    .SetWarnings False 
    For Each fil In fld.Files 
     If UCase(Right(fil.Name, 3)) = "CSV" Then 
      .TransferText acImportDelim, , DestTable, fil.Path, False 



     End If 

    Next 
    .SetWarnings True 
End With 

回答

1

嘗試了這一點:

loopCounter = 1 
With DoCmd 
    .SetWarnings False 
    For Each Fil In fld.Files 
      'I'm assuming the new field that will hold the FileName 
      'doesn't exist as part of the original table so we 
      'are going to create it for the first file read in 
     If loopCounter = 1 Then CurrentDb.Execute ("Alter Table MyTable Add Column MyNewField Text") 

     If UCase(Right(Fil.Name, 3)) = "CSV" Then 
      .TransferText acImportDelim, , DestTable, Fil.Path, False 
      CurrentDb.Execute ("Update MyTable Set MyNewField = '" & Fil.Name & "' where MyNewField is null") 
     End If 

     loopCounter = loopCounter + 1 
    Next 
    .SetWarnings True 
End With 
+0

真棒!感謝您的幫助。那就是訣竅。 –