Ciao,
- ho un database diviso in dati e form
- sono entrambi nella stessa cartella e li uso in una rete aziendale
- il percorso è assoluto rispetto al percorso di rete
- Il database form è in formato accde
Ho necessità di usare questo database temporaneamente in un pc NON collegato alla rete e che NON ha Microsoft Acces installato ma solo la runtime 2019
Ho chiesto aiuto all'intelligenza artificiale e mi ha proposto questo codice
Option Compare Database
Option Explicit
Public Sub RelinkBackendTables()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim strCurrentFEPath As String
Dim strBackendDBName As String
Dim strExpectedBackendPath As String
Dim strOldConnect As String
Dim strNewConnect As String
Dim blnRelinkNeeded As Boolean
Dim intRelinkedTables As Integer
Set db = CurrentDb
strCurrentFEPath = CurrentProject.Path
strBackendDBName = "un.milione.dati.accdb"
strExpectedBackendPath = strCurrentFEPath & "\" & strBackendDBName
blnRelinkNeeded = False
intRelinkedTables = 0
On Error GoTo ErrorHandler
' Verifica se il file del backend esiste nel percorso atteso
If Dir(strExpectedBackendPath) = "" Then
MsgBox "Il database backend ('" & strBackendDBName & "') non è stato trovato nella stessa cartella del frontend." & _
vbCrLf & "Assicurati che entrambi i file siano nella stessa directory.", vbCritical, "Errore Backend non trovato"
GoTo Exit_Sub
End If
' Controlla se è necessario ricollegare le tabelle
For Each tdf In db.TableDefs
If tdf.Attributes And 16 Then ' Controlla se è una tabella collegata (dbLink = 16)
strOldConnect = tdf.Connect
' Verifica se il percorso della tabella collegata è già corretto
If InStr(1, strOldConnect, strExpectedBackendPath, vbTextCompare) = 0 Then
blnRelinkNeeded = True
Exit For ' Trovata almeno una tabella con percorso errato, ricollegheremo tutte
End If
End If
Next tdf
If blnRelinkNeeded Then
' Ricollega tutte le tabelle al nuovo percorso
For Each tdf In db.TableDefs
If tdf.Attributes And 16 Then
strNewConnect = ";DATABASE=" & strExpectedBackendPath
tdf.Connect = strNewConnect
tdf.RefreshLink
intRelinkedTables = intRelinkedTables + 1
' Debug.Print "Ricollegata tabella: " & tdf.Name & " a " & strExpectedBackendPath
End If
Next tdf
MsgBox "Tabelle ricollegate con successo. " & intRelinkedTables & " tabelle aggiornate.", vbInformation, "Ricollegamento Completato"
Else
MsgBox "Le tabelle sono già correttamente collegate al database backend.", vbInformation, "Nessun Ricollegamento Necessario"
End If
Exit_Sub:
Set tdf = Nothing
Set db = Nothing
Exit Sub
ErrorHandler:
MsgBox "Si è verificato un errore durante il ricollegamento delle tabelle: " & Err.Description & _
vbCrLf & "(Tabella: " & tdf.Name & ")", vbCritical, "Errore di Ricollegamento"
Resume Exit_Sub
End Sub
Come istruzioni finali dato che ho una maschera che si avvia all'apertura del database mi dice di applicare un'azione "Su caricamento"
tutto fatto come da istruzioni ma non funziona.
Ottengo il messaggio "Le tabelle sono già correttamente collegate al database backend" ma così non è.
Provando ad aprire una maschera va in errore di runtime 2467
Suggerimenti su cosa devo fare?
Grazie
Luca