Link table

QUINABA

Board Regular
Joined
Jul 18, 2002
Messages
127
Good Friday to all!

Is it possible to link a table from database "A" to "B" while being in database "C" utilizing a macro? I usually run a bunch of adhoc reports using the same tables and normally would have to create duplicate mdb's to be able to multi-task.

Thanks. :cool:
 
Thanks Peter, you are awesome! It works great!

Hate to be a pest, what's the best way to modify when linking multiple tables from the same mdb or perhaps linking multiple tables from multiple mdb's.
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
call it from another sub and pass the values for table names that way.
Code:
Sub MakeLinks()
Dim strSourceTable As String
Dim strSourceDB As String
Dim strTargetDB As String
Dim strTargetTable As String

strSourceDB = "C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb" 'path to source DB
strTargetDB = "C:\DB1.mdb" ' Database to put link in

strSourceTable = "Employees" ' table to link from
strTargetTable = "Employees" ' name for linked table
Call linkTable(strSourceDB, strSourceTable, strTargetDB, strTargetTable)

strSourceTable = "Customers" ' table to link from
strTargetTable = "Customers" ' name for linked table
Call linkTable(strSourceDB, strSourceTable, strTargetDB, strTargetTable)

strSourceTable = "orders" ' table to link from
strTargetTable = "orders" ' name for linked table
Call linkTable(strSourceDB, strSourceTable, strTargetDB, strTargetTable)

End Sub

Sub linkTable(strSourceDB, strSourceTable, strTargetDB, strTargetTable As String)
Dim strConnect As String
Dim dbsTemp As Database
Dim tdfLinked As TableDef

Set dbsTemp = OpenDatabase(strTargetDB)
strConnect = ";DATABASE=" & strSourceDB
Set tdfLinked = dbsTemp.CreateTableDef(strTargetTable)
tdfLinked.Connect = strConnect
tdfLinked.SourceTableName = strSourceTable
dbsTemp.TableDefs.Append tdfLinked

Set tdfLinked = Nothing
dbsTemp.Close
Set dbsTemp = Nothing
End Sub

Peter
 
Upvote 0

Forum statistics

Threads
1,221,780
Messages
6,161,887
Members
451,730
Latest member
BudgetGirl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top