MS Access Linked Table 管理器不更新新数据源

MS Access Linked Table Manager doesn't update new data source

我有一个 MS Access 2010 数据库,其中有一个链接到 CSV 文件的 table。使用内置访问 "Linked Table Manager" 更新 CSV 文件位置不起作用。

我检查要更新的文件,选择 "always prompt for new location" 和 select 新文件。我收到一条消息告诉我更新成功,但是当我去检查时,table 仍然链接到旧文件。

这是 MS Access 错误吗?如果是,最有效的解决方法是什么?

我最终删除了旧的 table 并手动重新创建了具有相同规格的新 table。

*更新:——我忘记包含引用的函数 Relink_CSV :(

是的,我会称之为错误。微软可能称它为 'design characteristic'.

如您所见,您可以手动修复该问题。如果您对代码解决方案感兴趣,那么我可能有适合您的东西——如果您的 CSV 文件以逗号分隔。

以下代码(您需要修改!)将删除现有的 linked csv 文件,然后将 link 添加到同一文件。为了调试,我的代码然后删除 link 并将 link 添加到不同的文件名,但在同一文件夹中。

还有其他解决方案使用已保存的导入规范,如果您的 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