Public Function RelinkSQLTables()
On Error GoTo EH
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim strServer As String
Dim strDatabase As String
Dim strConnect As String
Dim strTblServer As String
Dim strTblLocal As String
Dim rsTables As DAO.Recordset
Dim sSQL As String
Set db = CurrentDb()
sSQL = "SELECT * FROM DatabaseTables where Not IsNull(RemoteName);"
Set rsTables = db.OpenRecordset(sSQL, dbOpenSnapshot)
Do Until rsTables.EOF
For Each tdf In db.TableDefs
If tdf.Name = rsTables!TableName Then
''Build the dsn-less connection string
db.TableDefs.Delete rsTables!TableName
Exit For
End If
Next
strServer = ""
strDatabase = ""
strConnect = ""
strServer = DLookup("[ServerName]", "qryDataLinks", "[TableName] = '" & rsTables!TableName & "'")
strDatabase = DLookup("[DatabaseName]", "qryDataLinks", "[TableName] = '" & rsTables!TableName & "'")
strConnect = "ODBC;Driver={SQL Native Client};Server=" & strServer & ";Database=" & strDatabase & ";Trusted_Connection=yes"
Set tdf = db.CreateTableDef(rsTables!TableName, dbAttachSavePWD, rsTables!RemoteName, strConnect)
db.TableDefs.Append tdf
rsTables.MoveNext
Loop
MsgBox "All SQL tables relinked", vbOKOnly + vbInformation, "Process Complete"
Exit_Sub:
Exit Function
EH:
Call ErrorManager(Err.Number, Err.Description)
Resume Exit_Sub
End Function
|