MS Access VBA – Import External Database Tables

The following procedure will import all the non-system tables from the specified database into the current database.

'---------------------------------------------------------------------------------------
' Procedure : ImportAllTbls
' Author    : CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Import all the tables from an external Access database
' Copyright : The following may be altered and reused as you wish so long as the
'             copyright notice is left unchanged (including Author, Website and
'             Copyright).  It may not be sold/resold or reposted on other sites (links
'             back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sExtDbPath - Full Path & Filename of the Database to import the tables from
'
' Usage Example:
' ~~~~~~~~~~~~~~~~
' ImportAllTbls "C:\Databases\development01.mdb"
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2009-Jan-07                 Initial Release
'---------------------------------------------------------------------------------------
Sub ImportAllTbls(sExtDbPath As String)
On Error GoTo Error_Handler
    Dim db      As DAO.Database
    Dim tdf     As DAO.TableDef
 
    Set db = OpenDatabase(sExtDbPath)
 
    For Each tdf In db.TableDefs 'Loop through all the table in the external database
        If Left(tdf.Name, 4) <> "MSys" Then 'Exclude System Tables
            On Error Resume Next
            Access.DoCmd.TransferDatabase acImport, "Microsoft Access", sExtDbPath, _
                                          acTable, tdf.Name, tdf.Name, False
        End If
    Next tdf
    db.Close
 
    Set db = Nothing 'Cleanup after ourselves

Exit Sub
 
Error_Handler:
    MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf & "Error Number: " & _
    Err.Number & vbCrLf & "Error Source: ImportAllTbls" & vbCrLf & "Error Description: " & _
    Err.Description, vbCritical, "An Error has Occured!"
    Exit Sub
End Sub

3 Comments to “MS Access VBA – Import External Database Tables”

  1. Hi All

    this ways to import all of table

    what about if I want import part of External Database Tables like :

    I have in database 1 table called “checkinout ” contan Id , time , type( checkin & checkout ) for ever and no form or modele it system software nobody use it

    the other database I need to import I have from this table Id , time , type( checkin & checkout ) that = any day

    not hole of table

    thanks

    • I’m not sure if I understand the question properly, but if all you want is to import part of the data from an external data into another database, this would be a perfect time to use an append query setting the criteria to fit your needs.

      Depending on your situation, I’d create a linked table to your external database with the full table and then use it to append its data into your new table setting your criteria based on your required date range. Once the Import is completed, simply delete the linked table (it only deletes the link, not the actual table or data).

  2. I got it! Thanks for the sweet code

Leave a Reply









Spam protection by WP Captcha-Free