2010-07-23 11 views
8

Ho un front-end e il back-end di un database di Access. Il front-end fa riferimento alle tabelle collegate e ho bisogno di fare un link relativo invece di uno esplicito, ovvero "../database" viene fatto riferimento invece di "address/database"In che modo un percorso relativo può specificare una tabella collegata in Access 2007?

È possibile farlo, oppure devo specificare il percorso assoluto?

+3

Quanto è ridicolo che Access non supporti percorsi relativi fuori dalla scatola. In che modo qualcuno dovrebbe spedire a un cliente un database diviso con percorsi assoluti? –

+0

La limitazione è probabilmente dovuta all'accesso multiutente: poiché più utenti possono utilizzare lo stesso file e il blocco dei file è necessario, è necessario un percorso completo. La soluzione semplice è all'avvio il front end controlla se il back-end è disponibile (e che il controllo può essere relativo). Se il link è sbagliato, il tuo codice si ricollegherà semplicemente all'avvio. In effetti, ciò significa che l'applicazione verrà eseguita correttamente se viene spostata. –

risposta

1

Per quanto ne so, la proprietà Connect di TableDef richiede un percorso assoluto. Se mi sbaglio su questo punto, spero che qualcuno dirà come creare una tabella collegata usando un percorso relativo.

Date un'occhiata a programma di utilità libero di Armen Stein per gestire i collegamenti di tabella: J Street Access Relinker

6

tabelle collegate a file (come MDB, ACCDB, DBF, ecc) richiedono percorsi assoluti nei loro stringhe di connessione.

Tuttavia esiste una soluzione alternativa: durante l'avvio del database è possibile utilizzare vba per ridefinire i collegamenti in modo che corrispondano alla directory dell'istanza del database corrente.

(Il codice qui sotto non è stato testato/debug)

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 

End Sub

1

Il seguente codice è stato testato in Form_Load del modulo elencato nell'opzione "Modulo Display" per il database; questo è il modulo che carica ogni volta che il database viene aperto. Questo codice potrebbe anche essere chiamato dalla macro AutoExec per il database:

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 
+0

Ho trovato che questo codice funzionava correttamente senza la riga Dim tblDef As TableDef .Stava causando un errore "Tipo definito dall'utente non definito", che non è stato possibile correggere con il prefisso "DAO". a "TableDef" – avianattackarmada

1

Ecco una semplice routine che ha funzionato per me:

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 
2

ho provato alcune delle risposte di cui sopra , in particolare la risposta di Martin Thompson con cui ho riscontrato alcuni errori, quindi l'ho modificata come segue:

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 

Dopo fininshing questo, Goto Accesso Ribon> Crea> Macro Dal menu a discesa selezionare "EseguiCodice", quindi nel tipo nome della funzione "RelinkTables" che abbiamo digitato qui. Quindi salvare la macro con il nome "AutoExec". Ogni volta che apri il database, tutte le tabelle collegate verranno ricollegate al percorso originale. Questo è molto utile se si inseriscono i database in un supporto portatile.

Problemi correlati