设置链接数据库(MS Access)路径而无需访问链接数据库

时间:2014-03-10 16:46:55

标签: ms-access linked-tables

我有一个Access系统,它包含两个部分:a" frontend"包含表单,报表和宏的.mdb文件,以及包含数据的后端.mdb文件。前端MDB文件的副本存储在每台计算机上,后端文件位于\\server\share\backend.mdb。前端MDB文件使用Access的链接表功能连接到后端MDB。

我最近在家庭网络上对MDB进行了一些更改,我将这两个文件复制到我的机器上,并且能够更改链接表路径,因为我在本地计算机上有后端文件。但是现在我需要将更新的前端MDB放回(远程)客户端网络上,但是我无法远程将链接表路径更改回\\server\shares\backend.mdb

有没有办法将链接表路径(在我的本地计算机上)设置为不存在的路径? Access GUI只允许我通过“文件打开”对话框进行设置,因此不允许我手动设置它。

我使用Access 2010,但客户端使用Access 2003和Access 2013。

1 个答案:

答案 0 :(得分:2)

我使用此代码链接到另一个后端文件(例如,如果我发送并且后端不在同一个地方,就像它很少一样)

Public Function AttachToAnotherDataFile() As Boolean
    On Error GoTo 0
    Dim ofd As FileDialog
    Dim result As VbMsgBoxResult
    Set ofd = FileDialog(msoFileDialogFilePicker)
    ofd.show
    If ofd.SelectedItems.Count = 1 Then

        result = RelinkLinedTablesToBackend(ofd.SelectedItems(1))
        If result = vbCancel Then
            AttachToAnotherDataFile = False
        End If
        AttachToAnotherDataFile = True
    Else
        AttachToAnotherDataFile = False
    End If
End Function

Function RelinkLinedTablesToBackend(backendPath As String) As VbMsgBoxResult

    Dim tdf As TableDef
    Dim db As Database
    Dim tdfRefresh As TableDef
    Set db = CurrentDb

        For Each tdf In CurrentDb.TableDefs
            If tdf.Connect <> vbNullString Then
                On Error Resume Next
                db.TableDefs(tdf.Name).Connect = ";DATABASE=" & backendPath
                db.TableDefs(tdf.Name).RefreshLink
                If Err.Number <> 0 Then
                    RelinkLinedTablesToBackend = MsgBox(Err.Description, vbCritical + vbRetryCancel, "Error #:" & Err.Number)
                    Exit Function
                End If

                On Error GoTo 0
            End If
        Next

    Set tdf = Nothing
    Set db = Nothing
End Function

然后当我打开数据库时打开默认表单时,我尝试连接到后端

On Error Resume Next
    Dim rs As DAO.Recordset: Set rs = CurrentDb.OpenRecordset("Select Username, Password, UserGroup FROM Users")
    If Err.Number = 3024 Or Err.Number = 3044 Then
        MsgBox Err.Description & vbNewLine & "You will be prompted next to locate the data file. Without this file the database cannot open." _
            , vbCritical + vbOKOnly, "Backend Data File Not Found"
        GoTo FindBackEndFile
    End If
相关问题