Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

How add linked tables to the back-end file of a split database via VBA code

I'm trying to find a way to add linked tables to the back-end file of a split database via VBA code.

Does anyone know of code that will do this?  I want to allow the user to browse to the external Access database file and select the two files (there will always be two), and have them appear as linked files in the working F/E file.
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

it is the same command as the export/import but use
docmd.TransferDatabase acLink  .........
instead

here is a function to browse for the accdb

Function getAccessFile()
    Dim fdObj As Object
    Set fdObj = Application.FileDialog(3) 'msoFileDialogFilePicker
    ' Lets start the file browse from our current directory
    With fdObj
        .InitialFileName = Environ("userprofile") & "\Documents\"
        .Filters.Clear
        .Filters.Add "Access Files", "*.accdb, *.mdb"
        .AllowMultiSelect = False
        .ButtonName = "Select File"
        .InitialView = 1 
        .Title = "Select file"
        If .Show Then
            getAccessFile = .SelectedItems(1)
            Else
            Exit Function
        End If
    End With
End Function

Open in new window


in the click event of a button

private sub cmdBrowse_Click()
dim dbFile as string

dbFile=getAccessFile()

if dbFile & ""<>"" then
  DoCmd.TransferDatabase acLink, "Microsoft Access", dbFile, acTable, <TableName1>,<TableName1>
  DoCmd.TransferDatabase acLink, "Microsoft Access", dbFile, acTable, <TableName2>,<TableName2>

end if

end sub

Open in new window

That won't work. Tables linked in the backend will be visible in the backend only, not from the frontend.

If you from the frontend wish access to other tables than those in your current backend, you must link those tables directly in the frontend.

/gustav
Avatar of SteveL13

ASKER

Gustav... you are correct.  I'm ok with the linked tables showing up in the front-end file.

Rey, I've copied your code.  The onclick event works fine except I had to change

DoCmd.TransferDatabase acLink, "Microsoft Access", dbFile, acTable, <TableName1>,<TableName1>
and
DoCmd.TransferDatabase acLink, "Microsoft Access", dbFile, acTable, <TableName2>,<TableName2>

to

DoCmd.TransferDatabase acLink, "Microsoft Access", dbFile, acTable, "TableName1","TableName1"
and
DoCmd.TransferDatabase acLink, "Microsoft Access", dbFile, acTable, "TableName2","TableName2"

But I don't know what to replace the "TableName1","TableName1" and "TableName2","TableName2" with.

I am able to navigate to the accdb file I want to get to and select it.  But after I've selected it I get...

User generated image
which I understand but I don't know how to get the real table names in place.
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes.  Will always only have two.
Absolute perfect!  Thank you very much!!!