refreshing linked tables in access

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

Hey. I have the main access database located on a stand alone PC off the network and i have a access database with linked tables on the network linked back to the stand alone PC. I have linked the tables by creating a network share to the stand alone PC and linking them though a path. Can i set it up so that when the database is opened it automatically updates the linked tables. Ben

回答1:

You can. I often find it convenient to use a small check form that runs on start-up (set through start-up options) and checks a variety of things, including linked tables. To this end, I also hold a table of linked tables on the local machine, although a list of linked tables can be obtained by iterating through the TableDefs collection, I think it is slightly safer to keep a list.

The check form can check all links and if a link is broken or missing, either ask the user for a new location or use a fixed location. If no problems are found, the form can close itself and open a menu or other form.

In the case of linking to a linked table, it is possible to get the connection to use from:

 CurrentDB.TableDefs("TableName").Connection 

Here are some more notes:

Sub RelinkTables(Optional strConnect As String = "") Dim db As DAO.Database Dim rs As DAO.Recordset Dim strSQL Dim tdf As DAO.TableDef  On Error GoTo TrapError      Set db = CurrentDb      If strConnect = "" Then         ''Where Me.txtNewDataDirectory is a control on the check form         strConnect = "MS Access;PWD=databasepassword;DATABASE=" & Me.txtNewDataDirectory     End If      ''Table of tables to be linked with two fields TableName, TableType     Set rs = CurrentDb.OpenRecordset("Select TableName From sysTables " _            & "WHERE TableType = 'LINK'")      Do While Not RS.EOF         ''Check if the table is missing         If IsNull(DLookup("[Name]", "MSysObjects", "[Name]='" & rs!TableName & "'")) Then             Set tdf = db.CreateTableDef(RS!TableName, dbAttachSavePWD, _                 rs!TableName, strConnect)             ''If the table is missing, append it             db.TableDefs.Append tdf         Else             ''If it exists, update the connection             db.TableDefs(rs!TableName).Connect = strConnect         End If         db.TableDefs(rs!TableName).RefreshLink         RS.MoveNext     Loop      Set db = Nothing     RS.Close     Set RS = Nothing   Exit_Sub:     Exit Sub  TrapError:     HandleErr Err.Number, Err.Description, "Relink Tables"  End Sub 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!