Access/VBA Library


Link a table from another database to the current database.


LinkTable( TableName, databasePath [, baseTableName] [, databasePassword] )
Name Type Description
TableName String Required. Name of the table in the current database.
databasePath String Required. Full path to the external database from which the table must be linked.
baseTableName String Optional. The name of the table in the external database. When omitted the tableName argument is used.
Default: ""
databasePassword String Optional. The password of the external database.
Default: ""
Return value

Boolean : True when the table has been linked successfully, False otherwise.

Function LinkTable(TableName As String, _
                   databasePath As String, _
                   Optional ByVal baseTableName As String = "", _
                   Optional ByVal databasePassword As String = "") As Boolean
    Dim td As TableDef
    Dim db As Database
    Dim connectString As String
    Dim tableLinked As Boolean
    On Error Resume Next
    ' Delete tablename if exists
    Set db = CurrentDb
    db.TableDefs.Delete (TableName)
    On Error GoTo LinkTable_Error
    ' Build connection string
    connectString = ";DATABASE=" & databasePath
    If databasePassword <> "" Then
        connectString = connectString & ";PWD=" & databasePassword
    End If
    DoCmd.SetWarnings False
    ' Create tabledef and link using connectString

    Set td = db.CreateTableDef(TableName)
    With td
        .Connect = connectString
        If baseTableName = "" Then
            .SourceTableName = TableName
            .SourceTableName = baseTableName
        End If
    End With
    db.TableDefs.Append td
    ' Table successfully linked
    tableLinked = True
    DoCmd.SetWarnings True

    LinkTable = tableLinked
    Exit Function

    ShowError functionName:="LinkTable"
    Resume LinkTable_Exit
End Function


See also

  • ShowError