Using Access as an application is often made into two databases: Data and program. The data database only stores data tables, and the program database stores all form, query, report, and module, you can access tables in all data databases through the table Connection Program database. When our applications change the storage location, the correct table join is often lost, resulting in
Using Access as an application is often made into two databases: Data and program. The data database only stores data tables, and the program database stores all form, query, report, and module, you can access tables in all data databases through the table Connection Program database. When our applications change the storage location, the correct table join is often lost, resulting in
Using Access for applications is often made into two databases: "data" and "program". "data" databases only store data tables, and "program" databases store all form and query, report, module, etc. You can access all tables in the "data" database by connecting to the "program" database through tables.
When our applications change the storage location, the correct table connection is often lost, resulting in running errors. The following program can automatically retrieve the table join. In my application, the "program" Database Name Is stockMgr. mdb, and the "data" Database Name Is stock-Data.mdb.
Note: This program is only applicable when "program" and "data" are stored in the same directory and correspond to a single "data" file. The following are
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 & ", continue? ", VbYesNo) Then Exit
End If
End If
Next I
DoCmd. Hourglass False
Msgbox "Tables relink finish ."
End Function
'Remove the file name from the absolute path and return the path
Function trimFileName (fullname As String) As String
Dim slen As Long, I As Long
Slen = Len (fullname)
For I = slen To 1 Step-1
If Mid (fullname, I, 1) = "" Then
Exit
End If
Next
TrimFileName = Left (fullname, I)
End Function
It can be called in the program startup or button action.