*更新: - 私は、参照機能Relink_CSV :(
を含めるのを忘れてはい、私はそれのバグを呼び出しますマイクロソフトは、おそらく「特徴的なデザイン」と呼ん
あなたが発見したとおり。 CSVファイルがコンマで区切られている場合は、コードソリューションに興味がある場合は、CSVファイルをカンマで区切って入力してください。
次のコードmodify!)既存のリンクされたcsvファイルを削除し、同じファイルへのリンクを追加します。デバッグのために、私のコードはそのリンクを削除し、別のファイル名へのリンクを同じフォルダにddsします。
は、あなたのcsv形式は単純ではない場合は、再利用することができ、保存されたインポート定義を利用する他のソリューションがあります。
Option Explicit
Option Compare Database
Sub Call_Relink()
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim strTableName As String
Dim strPath As String
Dim strFile As String
Dim iReply As Integer
iReply = MsgBox("WARNING!!!! This code will remove the linked tables 'FileA' and 'FileB'" & vbCrLf & vbCrLf & _
"Click 'Yes' to Continue" & vbCrLf & "Click 'No' to Stop", vbYesNo, "CAUTION!! Will remove linked table(s)")
If iReply <> vbYes Then
Exit Sub
End If
On Error GoTo Error_Trap
Set dbs = CurrentDb
dbs.TableDefs.Delete "FileA" ' For testing; delete table if it already exists
strPath = "C:\Temp\"
strFile = "FileA.csv"
strTableName = "FileA" ' Table name in Access
Relink_CSV strTableName, strPath, strFile ' Call function to link the CSV file
dbs.TableDefs.Refresh ' Refresh TDF's
Debug.Print "Pause here and check file link" ' Put a breakpoint here; pause and look at the table in Access
dbs.TableDefs.Delete "FileA" ' For testing; delete table if it already exists
strPath = "C:\Temp\" ' Path to next csv
strFile = "FileB.csv" ' Name of next csv file
strTableName = "FileA" ' Table name in Access
Relink_CSV strTableName, strPath, strFile ' Call function to link to a different CSV file
dbs.TableDefs.Refresh
Debug.Print "Pause here and check file link" ' Put a breakpoint here; pause and look at the table in Access
My_Exit:
Set dbs = Nothing
Exit Sub
Error_Trap:
Debug.Print Err.Number & vbTab & Err.Description
If Err.Number = 3265 Then ' Item not found in this collection.
' Ignore this error
Resume Next
End If
MsgBox Err.Number & vbTab & Err.Description
Resume My_Exit
Resume
End Sub
Function Relink_CSV(strTableName As String, strPath As String, strFile As String)
' (1) Name of the table in Access
' (2) Path to the file
' (3) File name
On Error GoTo Relink_Err
DoCmd.TransferText acLinkDelim, , strTableName, strPath & strFile, False, ""
Relink_Exit:
Exit Function
Relink_Err:
Debug.Print Err.Number & vbTab & Err.Description
MsgBox Err.Number & vbTab & Err.Description
Resume Relink_Exit
Resume
End Function
別の名前(FileA.csvとFileB.csv)のファイルを選択しようとしていますか? FileA.csvにリンクしてLinked Table Managerを使用してFileB.csvにリンクしても、それはうまくいっていますが、同じ古いファイルは保持されています。別のフォルダにあるFileA.csvに再リンクすると、正しく動作します。私はまた、次のリンクでコードをテストし、それが/再リンクを削除するために動作するようです:http://www.tek-tips.com/viewthread.cfm?qid=865710 –
あなたは正しいです。しかし、私は単に現在のパスのサブフォルダを使うことはできませんでした。私はまったく新しいフォルダを選ぶ必要がありました。それは私がそれを試していたときにうまくいかなかった理由の一部でした。奇妙なバグと、マイクロソフトがずっと前に対処していたはずのもの。解答としてあなたのコメントを投稿してください。私は解決策としてそれをチェックすることができます!ありがとう – rohrl77