Dynamically linking Tables in Access

hianupam

New Member
Joined
Mar 28, 2002
Messages
37
I am trying to come up with a MDB that has linked tables to two other MDB's
The only problem is that the path to the two MDB's keep changing. So I am thinking of coming up with a form that asks the location for the two mdb's and links to all the tables to it. Any Idea's, Starting points will be greatly appreciated.
THanks,
Anupam
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Here is the solution that I came up with.

I created a form with two Command buttons that promts the users to select an input and an output database. And then once I had the paths i proceded to link them.

Check to see that the input and output databases are not the same
Code:
Function chkSameDB()
  If Me.txtDispInputDb.Value = Me.txtDispOutputDb.Value Then
    chkSameDB = True
  Else
    chkSameDB = False
  End If
End Function

Check to see that the file selected is actually an access file
Code:
Function chkFname(infile) As Boolean
    If Right(infile, 3) <> "mdb" Then
        chkFname = False
    Else
        chkFname = True
    End If
End Function

Open a dialog box to help user select a input file and ...
Code:
Private Sub cmdInput_Click()
    cmnDialog.ShowOpen
    g_inputfile = cmnDialog.FileName
    If chkFname(g_inputfile) Then
        Me.txtDispInputDb.Value = g_inputfile
    Else
        MsgBox ("Please select an Access Database")
    End If
End Sub

...an output file
Code:
Private Sub cmdOutput_Click()
    cmnDialog.ShowOpen
    g_outputfile = cmnDialog.FileName
    If chkFname(g_outputfile) Then
        Me.txtDispOutputDb.Value = g_outputfile
    Else
        MsgBox ("Please select an Access Database")
    End If
End Sub

Here is where everything comes together
Code:
Private Sub cmdLinkFiles_Click()

Set db1 = DBEngine(0).OpenDatabase(Me.txtDispInputDb.Value)
Set db2 = DBEngine(0).OpenDatabase(Me.txtDispOutputDb.Value)
Set db = CurrentDb
If chkSameDB Then
  MsgBox ("Input and Output Databases cannot be the same")
  Me.txtDispInputDb.SetFocus
Else
For Each tbl In db.TableDefs
    tblname = tbl.Name
    If Left(tblname, 4) <> "MSys" Then
        DoCmd.DeleteObject acTable, tblname
    End If
Next tbl
For Each tbl In db1.TableDefs
    tblname = tbl.Name
    If Left(tblname, 4) <> "MSys" Then
        Set tdf = db.CreateTableDef(tblname)
        tdf.Connect = ";DATABASE=" & Me.txtDispInputDb.Value
        tdf.SourceTableName = tblname
        'MsgBox (tdf.SourceTableName)
        db.TableDefs.Append tdf
        RefreshDatabaseWindow
    End If
Next tbl
For Each tbl In db2.TableDefs
    tblname = tbl.Name
    If Left(tblname, 4) <> "MSys" Then
        Set tdf = db.CreateTableDef("OutPut_" & tblname)
        tdf.Connect = ";DATABASE=" & Me.txtDispOutputDb.Value
        tdf.SourceTableName = tblname
        'MsgBox (tdf.SourceTableName)
        db.TableDefs.Append tdf
        RefreshDatabaseWindow
    End If
Next tbl
End If
MsgBox ("Tables Linked,Please Click on the Tables Tab to View Them")
End Sub
 
Upvote 0
Glad you got it to work. You may want to try putting the call to chkSameDB before you do your Set statements - this will make it faster and avoid memory problems that may occur.

-Russell
 
Upvote 0

Forum statistics

Threads
1,223,534
Messages
6,172,893
Members
452,488
Latest member
jamesgeorgewalker

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