Updating Links to Tables That Have Moved

If you move a table that you previously linked, you need to refresh the link before you can access the table again. To refresh a link, you must reset the Connect property of the TableDef object to point to the table’s new location. Then, use the RefreshLink method of the TableDef object to update the link information.

The following procedure shows how to re-create the link to a FoxPro table that was moved to another directory after it was linked to a Microsoft Jet database. The TableDef object LostLink is linked to the FoxPro table LostLink.dbf, which was originally in the JetBook\Samples\FoxTables\Sales directory but is now in the JetBook\Samples\FoxTables\LostLink directory. When you try to open the linked table, Microsoft Jet reports that it couldn’t find the object “LostLink.”

  1. Try to open the table LostLink.dbf through Microsoft Access. Microsoft Access displays the message “Couldn’t find object ‘LostLink’.”

  2. Run the following code, where strDbPath is the path to the Microsoft Jet sample database:
    Dim dbs As Database
    Dim qdf As QueryDef
    Dim rst As Recordset
    
    Set dbs = OpenDatabase(strDbPath)
    ' Update connection string to include new location.
    dbs!LostLink.Connect = "FoxPro 3.0" & _
    	";DATABASE=C:\JetBook\Samples\FoxTables\LostLink"
    ' Refresh link.
    dbs.TableDefs!LostLink.RefreshLink
    
    ' Run a simple query to ensure the data is accessible.
    Set qdf = dbs.CreateQueryDef("")
    qdf.SQL = "SELECT Sum(Sales) As Tally FROM LostLink;"
    Set rst = qdf.OpenRecordset()
    MsgBox "Total Sales = $" & rst!Tally
    rst.Close
    dbs.Close
  3. Try to open the table LostLink.dbf through Microsoft Access once again. Microsoft Access opens the linked table.