How can a relative path specify a linked table in Access 2007?

前端 未结 6 673
抹茶落季
抹茶落季 2021-01-01 20:19

I have a front end and back end of an Access database. The front end references linked tables and I need to do a relative link instead of an explicit one i.e. \"../dat

6条回答
  •  情深已故
    2021-01-01 20:59

    The following code has been tested in the Form_Load event of the form listed in the "Display Form" option for the database; that is the form that loads whenever the database is opened. This code could also be called from the AutoExec macro for the database:

    Private Sub Form_Load()
    Dim strOldConnect As String
    Dim strNewConnect As String
    Dim intSlashLoc As Integer
    Dim intEqualLoc As Integer
    
    Dim strConnect As String
    Dim strFile As String
    Dim strCurrentPath As String
    
    strCurrentPath = CurrentProject.path
    
    Dim tblDef As TableDef
    Dim tblPrp As Property
    
    For Each tblDef In CurrentDb.TableDefs
        Debug.Print tblDef.Name
        If tblDef.Connect & "." <> "." Then
    
            strOldConnect = tblDef.Connect
            intEqualLoc = InStr(1, strOldConnect, "=", vbTextCompare)
            strConnect = Left(strOldConnect, intEqualLoc)
            intSlashLoc = InStrRev(strOldConnect, "\", -1, vbTextCompare)
            strFile = Right(strOldConnect, Len(strOldConnect) - intSlashLoc)
            strNewConnect = strConnect & strCurrentPath & "\" & strFile
    
            tblDef.Connect = strNewConnect
            tblDef.RefreshLink
        End If
    
    Next tblDef
    End Sub
    

提交回复
热议问题