文章作者 100test 发表时间 2007:12:29 12:10:35
来源 100Test.Com百考试题网
当我们的应用程序更换存储位置的时候往往失去正确的表联接,造成运行错误。下面的一段程序可以自动找回表联接。在我的应用中"程序"数据库名字是stockMgr.mdb,"数据"数据库名字是stock-Data.mdb
注意: 该程序仅限于"程序"和"数据"存放在同一个目录而且是对应单个"数据"文件的情况。
Function ReAttachTable()
Dim MyDB As Database, MyTbl As TableDef
Dim cpath As String
Dim datafiles As String, i As Integer
On Error Resume Next
Set MyDB = CurrentDb
cpath = trimFileName(CurrentDb.Name)
datafiles = "stock-data.mdb"
DoCmd.Hourglass True
For i = 0 To MyDB.TableDefs.Count - 1
Set MyTbl = MyDB.TableDefs(i)
If MyTbl.Attributes = DB_ATTACHEDTABLE And Left(MyTbl.Connect, 1) = "." Then
MyTbl.Connect = ".DATABASE=" &. cpath &. datafiles
MyTbl.RefreshLink
If Err Then
If vbNo = MsgBox(Err.description &. ",继续吗?", vbYesNo) Then Exit For
End If
End If
Next i
DoCmd.Hourglass False
msgbox "Tables relink finish."
End Function