Function GetLinkedConnectStrings()
On Error GoTo Err_GetLinkedConnectStrings
Dim db As Database
Dim doc As Document
Dim tbf As TableDef
DoCmd.Hourglass True
Set db = CurrentDb
For Each tbf In db.TableDefs
DoEvents
If Not Left(tbf.Name, 4) = "MSys" Then
'ignore system tables
Debug.Print tbf.Name & "; " & tbf.Connect
End If
Next tbf
Exit_GetLinkedConnectStrings:
On Error Resume Next
DoCmd.Hourglass False
db.Close
Set db = Nothing
Exit Function
Err_GetLinkedConnectStrings:
Select Case Err
Case 0 'insert Errors you wish to ignore here
Resume Next
Case 3011 'object not found
Resume Next
Case 3045 'database already in use
Beep
MsgBox "The database is in use by someone else. " _
& "Go and evict them and try again.", , "You are not alone."
Case Else 'All other errors will trap
Beep
MsgBox "Error deleting tables.@" & Err.Number & "; " & Err.Description
Resume Exit_GetLinkedConnectStrings
End Select
Resume 0 'FOR TROUBLESHOOTING
End Function
Public Sub DeleteTableLinks(Optional strConnectString As String = "")
'If strConnectString is omited all links will be removed
Dim tdf As TableDef
For Each tdf In CurrentDb.TableDefs
If tdf.Connect <> "" Then 'Check for linked tables
'Check for pointed links
If InStr(1, tdf.Connect, strConnectString, vbTextCompare) > 0 Then
'Removing links
DoCmd.DeleteObject acTable, tdf.Name
DoCmd.Echo True, "Progress: Deleting link to table " & tdf.Name
End If
End If
Next tdf
End Sub
Public Sub RemakeTableLinks()
On Error GoTo Err_RemakeTableLinks
Dim dbs As Database
Dim tdf As TableDef
Dim strLinkSourceDB As String
Dim tdfCount As Long
Dim intCount As Long
' Need to go get database from user
' Replace this with a more appropriate FileFind if necessary
Select Case MsgBox("If you are connecting to the XXXX Server, click on Yes" & vbLf _
& "If you are connecting to the YYYY Server, click on No" _
, vbYesNoCancel, "Get Data From User")
Case Is = vbYes
strLinkSourceDB = "\\XXXX\Ash.mdb"
Case Is = vbNo
strLinkSourceDB = "\\YYYY\And.mdb"
Case Else
MsgBox "Option to Terminate selected.", , "No Link to Make"
Exit Sub
End Select
'Open source DB
Set dbs = OpenDatabase(strLinkSourceDB)
'Counting tables in the source DB
For Each tdf In dbs.TableDefs
If Left(tdf.Name, 4) <> "MSys" Then
'Do not link to the System tables
tdfCount = tdfCount + 1
End If
Next tdf
'Check all tables in source DB (dbs)
For Each tdf In dbs.TableDefs
If Left(tdf.Name, 4) <> "MSys" Then
'Do not link to the System tables
'Creating links
intCount = intCount + 1
DoCmd.TransferDatabase acLink, "Microsoft Access", strLinkSourceDB, acTable, tdf.Name, tdf.Name
DoCmd.Echo False, "Progress: Linking table " & intCount & " of " & tdfCount
End If
Next tdf
'Close source DB
dbs.Close
Set dbs = Nothing
Exit_RemakeTableLinks:
DoCmd.Echo True
Exit Sub
Err_RemakeTableLinks:
MsgBox Err.Description, , "mdlLinkedTable, Sub RemakeTableLinks " & Err.Number
Resume Exit_RemakeTableLinks
End Sub