我有一個Access數據庫的前端和後端。前端引用鏈接的表,我需要做一個相對鏈接,而不是一個明確的一個即"../database"
代替引用的"address/database"
相對路徑如何在Access 2007中指定鏈接表?
是否有可能做到這一點,或者我必須指定絕對路徑?
我有一個Access數據庫的前端和後端。前端引用鏈接的表,我需要做一個相對鏈接,而不是一個明確的一個即"../database"
代替引用的"address/database"
相對路徑如何在Access 2007中指定鏈接表?
是否有可能做到這一點,或者我必須指定絕對路徑?
據我所知,你的TableDef的連接屬性需要一個絕對路徑。如果我在這一點上錯了,我希望有人會告訴如何使用相對路徑創建鏈接表。
看看阿爾緬·斯坦的免費工具來管理您的錶鏈接:J Street Access Relinker
錶鏈接到文件(如MDB,ACCDB,DBF等)需要在他們的連接字符串的絕對路徑。
但是有一個解決方法:在數據庫啓動期間,您可以使用vba重新定義鏈接以匹配當前數據庫實例的目錄。
(下面的代碼尚未經過測試/調試)
Private Sub RelinkTables()
Dim oldConnection As String
Dim newConnection As String
Dim currentPath As String
currentPath = CurrentProject.Path
Dim tblDef As TableDef
For Each tblDef In CurrentDb.TableDefs
oldConnection = tblDef.Connect
' Depending on the type of linked table
' some string manipulation which defines
' newConnection = someFunction(oldConnection,currentPath)
tblDef.Connect = newConnection
tblDef.RefreshLink
Next tblDef
結束子
下面的代碼已在爲「顯示錶」選項列出的形式的Form_Load事件被測試數據庫;這是每當數據庫打開時加載的表單。
Private Sub Form_Load()
Dim strOldConnect As String
Dim strNewConnect As String
Dim intSlashLoc As Integer
Dim intEqualLoc As Integer
Dim strConnect As String
Dim strFile As String
Dim strCurrentPath As String
strCurrentPath = CurrentProject.path
Dim tblDef As TableDef
Dim tblPrp As Property
For Each tblDef In CurrentDb.TableDefs
Debug.Print tblDef.Name
If tblDef.Connect & "." <> "." Then
strOldConnect = tblDef.Connect
intEqualLoc = InStr(1, strOldConnect, "=", vbTextCompare)
strConnect = Left(strOldConnect, intEqualLoc)
intSlashLoc = InStrRev(strOldConnect, "\", -1, vbTextCompare)
strFile = Right(strOldConnect, Len(strOldConnect) - intSlashLoc)
strNewConnect = strConnect & strCurrentPath & "\" & strFile
tblDef.Connect = strNewConnect
tblDef.RefreshLink
End If
Next tblDef
End Sub
我發現此代碼運行良好,沒有 Dim tblDef As TableDef line。它導致「用戶定義類型未定義」錯誤,無法通過在「DAO」前添加固定。到「TableDef」 – avianattackarmada 2015-09-23 00:27:19
下面是一個簡單的步驟,爲我工作:
Public Function gbLinkTables() As Boolean
On Error GoTo ErrorRoutine
Dim sMyConnectString As String
Dim tdf As TableDef
'We will link all linked tables to an accdb Access file located in the same folder as this file.
'Replace the DATA file name in the following statement with the name of your DATA file:
sMyConnectString = ";database=" & CurrentProject.Path & "\Loan-Tracking-Data.accdb"
For Each tdf In CurrentDb.TableDefs
If Len(tdf.Connect) > 0 Then
'It's a linked table, so re-link:
tdf.Connect = sMyConnectString
tdf.RefreshLink
End If
Next tdf
ExitRoutine:
Exit Function
ErrorRoutine:
MsgBox "Error in gbLinkTables: " & Err.Number & ": " & Err.Description
Resume ExitRoutine
End Function
我嘗試過的一些問題的答案上面這段代碼也可以從AutoExec宏數據庫稱爲,尤其是Martin Thompson的回答,我得到了一些錯誤,並修改如下:
Public Function reLinkTables() As Boolean
On Error GoTo ErrorRoutine
Dim sMyConnectString As String
Dim tdf As TableDef
Dim db_name As String
' The Main Answer is by Martin Thompson
' Modified by Dr. Mohammad Elnesr
'We will link all linked tables to an accdb Access file located in the same folder as this file.
'Replace the DATA file name in the following statement with the name of your DATA file:
sMyConnectString = ";DATABASE=" & CurrentProject.Path & "\"
For Each tdf In CurrentDb.TableDefs
If Len(tdf.Connect) > 0 Then
'It's a linked table, so re-link:
'First, get the database name
db_name = GetFileName(tdf.Connect)
' Then link the table to the current path
tdf.Connect = sMyConnectString & db_name
tdf.RefreshLink
End If
Next tdf
ExitRoutine:
MsgBox "All tables were relinked successfully"
Exit Function
ErrorRoutine:
MsgBox "Error in gbLinkTables: " & Err.Number & ": " & Err.Description
Resume ExitRoutine
End Function
Function GetFileName(FullPath As String) As String
Dim splitList As Variant
splitList = VBA.Split(FullPath, "\")
GetFileName = splitList(UBound(splitList, 1))
End Function
fininshing在此之後,後藤訪問瑞邦>新建>宏從下拉列表中選擇「RunCode」,然後在功能名稱輸入「reLinkTables」這是我們在這裏輸入。然後保存名稱爲「AutoExec」的宏。每次打開數據庫時,所有鏈接表都將重新鏈接到原始路徑。如果將數據庫放入便攜式媒體中,這非常有用。
Access如何不支持相對路徑開箱即用。有人應該如何將一個客戶端發送一個帶有絕對路徑的分割數據庫? – 2013-10-16 23:51:32
這個限制很可能是由於Access是多用戶的 - 因爲多個用戶可以使用相同的文件並且有文件鎖定 - 那麼就需要一個完整的合格路徑。簡單的解決方案是在啓動前端檢查後端是否可用(並且該檢查可以是相對的)。如果鏈接錯誤,那麼您的代碼只需在啓動時重新鏈接。實際上,這意味着如果應用程序移動,您的應用程序將運行得很好。 – 2017-01-12 19:43:04