Ever taken over a database that was a complete mess and quickly wanted to determine what Back-End data sources were involved? Below is a simple function to enumerate the Back-End data sources in a given database.
Public Sub ListBESources()
On Error Resume Next
Dim colTables As New Collection
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim sBackEnd As String
Dim BE As Variant
Set db = CurrentDb
'Loop through the TableDefs Collection.
For Each tdf In db.TableDefs
'Ensure the table is a linked table.
If Left$(tdf.Connect, 10) = ";DATABASE=" Then
'Get the path/filename of the linked back-end
sBackEnd = Mid(tdf.Connect, 11)
'Ensure we have a valid string to add to our collection
If Len(sBackEnd & "") > 0 Then
colTables.Add Item:=sBackEnd, Key:=sBackEnd
End If
End If
Next tdf
On Error GoTo 0
Debug.Print colTables.Count & " Data Source(s) found:"
For Each BE In colTables
Debug.Print BE
Next BE
End Sub
If you prefer a standalone function to get a collection and then use another function to process the results, then you could do something along the lines of:
Public Function ListBESources() As Collection
On Error Resume Next
Dim colTables As Collection
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim sBackEnd As String
Set colTables = New Collection
Set db = CurrentDb
'Loop through the TableDefs Collection.
For Each tdf In db.TableDefs
'Ensure the table is a linked table.
If Left$(tdf.Connect, 10) = ";DATABASE=" Then
'Get the path/filename of the linked back-end
sBackEnd = Mid(tdf.Connect, 11)
'Ensure we have a valid string to add to our collection
If Len(sBackEnd & "") > 0 Then
colTables.Add Item:=sBackEnd, Key:=sBackEnd
End If
End If
Next tdf
Set ListBESources = colTables
End Function
and then to use it, you could call it in the following manner:
Public Sub TestMe()
Dim colTables As Collection
Dim BE As Variant
Set colTables = ListBESources
Debug.Print colTables.Count & " Data Source(s) found:"
For Each BE In colTables
Debug.Print BE
Next BE
End Sub
Please note that the above was very quickly put together to meet an immediate need and has not been fully tested and proper error handling and variable cleanup also needs to be implemented. That said, the basic principle is sound!
Nice, but I would also check if
td.Connectd like "ODBC;*"in order to catch “other” backends, like Oracle, SQL Server…And by the way, testing with “Like” is much faster than testing with “Left”.
I use this query to populate my Jet/ACE relink form.
SELECT MSysObjects.Database, “” AS NewDBName
FROM MSysObjects
GROUP BY MSysObjects.Database, “”
HAVING (((MSysObjects.Database) Is Not Null));
One of the more annoying things about the linked tables manager is the inability to sort by connection so if you have multiple BE’s it is a real pain to relink all the tables in one of them. My form does that by using this query to let the user select the BE he wants to relink.
I have tried to implement this as it would meet my requirement to have vba to find Backend. However, the definition for BE is not defined. I tried Dim BE as Variant and still get an error.
Thanks,
Dave in AZ
Not sure what to say. I tested and you are right I forgot to add the Dim statement, but once I add ‘Dim BE As Variant’ it compiles and works fine for me. What error do you receive?