以编程方式更改ms访问中链接表的连接

时间:2013-03-28 12:52:55

标签: ms-access odbc access-vba linked-tables

我已经为我的问题引用了其他页面,但我仍然无法使其工作。鉴于我有三个例子,我觉得有点慢,但仍然无法解决这个问题。

Changing linked table location programatically

Linked table ms access 2010 change connection string

Update an Access linked table to use a UNC path

以下是我正在使用的代码:

Dim tdf As TableDef
Dim db As Database
Set db = CurrentDb
Set tdf = db.TableDefs("DeviceListT")
tdf.Connect = "ODBC;DATABASE=" & CurrentProject.path _
                               & "\HarmonicProfileDatabase_be.accdb"
tdf.RefreshLink

问题是当我运行它时会弹出一个窗口。

Select Data Source

我不确定我应该做些什么,也不想让它首先弹出,因为我将ms访问文件发给别人,他们不知道该怎么做这个窗口也是。

1 个答案:

答案 0 :(得分:6)

您正在使用SQL Server引用但链接MS Access。对于MS Access,您不需要ODBC链接,只需参考DATABASE:

DBFile = CurrentProject.path & "\HarmonicProfileDatabase_be.accdb
''Check the file exists
strFile = Dir(DBFile)
If strFile <> "" Then
    With CurrentDb
        For Each tdf In .TableDefs
            ''Check that this is a linked table
            ''It can be useful to use table of tables instead
            If tdf.Connect Like "*HarmonicProfileDatabase_be.accdb*" Then
                tdf.Connect = ";DATABASE=" & DBFile 
                tdf.RefreshLink
            End If
        Next
    End With
    MsgBox "Link HarmonicProfileDatabase_be.accdb" 
Else
    MsgBox "Problem"
End If

你也可以使用:

 sConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
    & DBFile & ";Jet OLEDB:Database Password=pw;"
相关问题