Got a couple of functions that you may be able to use. The first (AttachTable_MSJet) is used for dynamically attaching MS Access/Jet tables to your Access database. The second (AttachTable_ClientServer) is used for attaching Client-Server tables (including SQL Server) tables to your database.
And they both work - tried 'em out myself first!
Here's the MS-Jet Attach function:
Public Function AttachTable_MSJet(strTableNameParm As String, _
strDataSourcePathFilenameParm As String) As Boolean
Dim cat As New ADOX.Catalog
Dim tbl As New ADOX.Table
Dim db As DAO.Database
Dim strWhereCond As String
strWhereCond = ""
strWhereCond = strWhereCond & "Name = '" & strTableNameParm & "'"
strWhereCond = strWhereCond & " AND Type = 6"
If Not IsNull(DLookup("Name", "MSysObjects", strWhereCond)) Then
Set db = CurrentDb
db.TableDefs.Delete strTableNameParm
db.Close
End If
cat.ActiveConnection = CurrentProject.Connection
tbl.Name = strTableNameParm
Set tbl.ParentCatalog = cat
tbl.Properties("Jet OLEDB:Create Link") = True
tbl.Properties("Jet OLEDB:Link Datasource") = strDataSourcePathFilenameParm
tbl.Properties("Jet OLEDB:Remote Table Name") = strTableNameParm
' Append the table to the collection
cat.Tables.Append tbl
Set cat = Nothing
End Function
And here's the Client-Server Attach Function:
Public Function AttachTable_ClientServer(strDBDriverParm As String, _
strDBServerNameParm As String, _
strDBNameParm As String, _
strTableNameParm As String, _
strUserIDParm As String, _
strPasswordParm As String)
Dim cat As New ADOX.Catalog
Dim tbl As New ADOX.Table
Dim strWhereCond As String
Dim db As DAO.Database
strWhereCond = ""
strWhereCond = strWhereCond & "Name = '" & strTableNameParm & "'"
strWhereCond = strWhereCond & " AND Type = 4"
If Not IsNull(DLookup("Name", "MSysObjects", strWhereCond)) Then
Set db = CurrentDb
db.TableDefs.Delete strTableNameParm
db.Close
End If
cat.ActiveConnection = CurrentProject.Connection
tbl.Name = strTableNameParm
Set tbl.ParentCatalog = cat
tbl.Properties("Jet OLEDB:Create Link") = True
tbl.Properties("Jet OLEDB:Link Provider String") = _
"ODBC" & _
";Driver=" & strDBDriverParm & _
";Server=" & strDBServerNameParm & _
";Database=" & strDBNameParm & _
";UID=" & strUserIDParm & _
";pwd=" & strPasswordParm
tbl.Properties("Jet OLEDB:Cache Link Name/Password") = True
tbl.Properties("Jet OLEDB:Remote Table Name") = strTableNameParm
' Append the table to the collection
cat.Tables.Append tbl
Set cat = Nothing
End Function
Neither of these require you to set up an ODBC data source via Control Panel. Here's a few notes on the functions:
- While I used ADOX to do the attach, I can't find the ADOX code to do a detach, thus
the "Dim db As DAO.Database", "Set db = ..." commands.
- In order to run these BE SURE that you have the 3 following references in your Tools | References
checked:
- Microsoft DAO 3.6 Object library
- Microsoft ADO Ext. 2.7 for DDL and Security
- Microsoft ActiveX Data Objects 2.1 Library
- There could also be different versions of any of these libraries
- The "Cache Link Name/Password" found in the Client-Server function will prevent any
additional prompts for user-id/passwords from the user when a screen opens, etc.
- The DLookup() function along with the "db.TableDefs.Delete" is used to
delete the link if the table is already linked. Type 1 in MSysObjects is used for
native MS-Access tables, Type 4 is used for ODBC-linked tables, and Type 6 is used
for MS-Jet/Access linked tables.
Here's a couple of sample calls to these functions:
Call AttachTable_MSJet("c:\My Documents\Sampledb.mdb", "tblCustomers")
Call AttachTable_ClientServer("SQL Server", "CORP_SERVER", "CustomerDB", "CUSTOMER_TABLE", "sa", "")
By the way, if anyone knows how to detach a linked table using
ADO or ADOX, I'm VERY welcome to ideas, too! :)
Hope you may find some of this useful. I use this code in TONS of my MS-Access applications. Plus, after forgetting to set up ODBC DSN connections on user workstations during an initial deployment, this helps perpetuate my forgetfulness. ;)
Good luck and happy programming!
Warren
:D