MS Access链接表管理器不会更新新数据源

时间:2017-05-22 13:23:42

标签: ms-access ms-access-2010

我有一个MS Access 2010数据库,其中包含一个链接到CSV文件的表。使用内置的Access"链接表管理器"来升级CSV文件位置不起作用。

我检查要更新的文件,选择"始终提示输入新位置"并选择新文件。我收到一条消息,告诉我更新成功了,但是当我去检查时,表仍然链接到旧文件。

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

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

1 个答案:

答案 0 :(得分:1)

*更新: - 我忘了包含引用的函数Relink_CSV :(

是的,我称之为bug。微软可能称之为“设计特色”。

正如您所发现的,您可以手动修复问题。如果您对代码解决方案感兴趣,那么我可能会有一些适合您的东西 - 如果您的CSV文件是用逗号分隔的。

以下代码(您需要修改!)将删除现有的链接csv文件,然后添加指向同一文件的链接。为了进行调试,我的代码然后删除该链接并添加指向不同文件名的链接,但是在同一文件夹中。

如果您的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
相关问题