Automatically reconnect data tables in instance analysis Access

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.