Looking for MS Access VBA code to import a table from another .mdb

I am looking for Access VBA code that will import a table from another Access mdb.

After importing the table, rename the table and then add a new column to that table all with VBA code so that it can be automated.

Any help would be great.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can use the TransferDatabase method
After that you can use the createfield method

This is from the Access '97 documentation:

The following example imports the NW Sales for April report from the Microsoft Access database NWSales.mdb into the Corporate Sales for April report in the current database:

DoCmd.TransferDatabase acImport, "Microsoft Access", _
      "C:\DBS\NWSales.mdb", acReport, "NW Sales for April", _
      "Corporate Sales for April"


DoCmd.TransferDatabase [transfertype], databasetype, databasename[, objecttype], source, destination[, structureonly][, saveloginid]

The TransferDatabase method has the following arguments.

Argument      Description
               One of the following intrinsic constants:
acImport (default)
If you leave this argument blank, the default constant (acImport) is assumed.

      A string expression that's the name of one of the types of databases you can use to import, export, or link data. In the Macro window, you can view the database types in the list for the Database Type action argument of the TransferDatabase action.
      A string expression that's the full name, including the path, of the database you want to use to import, export, or link data.
              One of the following intrinsic constants:
acTable (default)
              A string expression that's the name of the object whose data you want to import, export, or link.
              A string expression that's the name of the imported, exported, or linked object in the destination database.
              Use True (–1) to import or export only the structure of a database table. Use False (0) to import or export the structure of the table and its data.
If you leave this argument blank, the default (False) is assumed.
              Use True to store the login identification (ID) and password for an ODBC database in the connection string for a linked table from the database. If you do this, you don't have to log in each time you open the table. Use False if you don't want to store the login ID and password.
If you leave this argument blank, the default (False) is assumed.

And to create a new field:

      Dim tdfNew As TableDef

      Set tdfNew = currentdb.tabledefs("Your imported table name")

      ' Create and append new Field objects for the TableDef object.
      With tdfNew
            ' The CreateField method will set a default Size
            ' for a new Field object if one is not specified.
            .Fields.Append .CreateField("TextField", dbText)
                                .Fields.Append .CreateField("IntegerField", dbInteger)
            .Fields.Append .CreateField("DateField", dbDate)
      End With


Good Luck

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Public Function ImportTable()

Dim tdf as DAO.TableDef
Dim fld As DAO.Field

'use transferdatabase to pull the table, fill in parameters as appropriate
DoCmd.TransferDatabase ....

'get the table object
Set tdf = CurrentDB.TableDefs("MyTable")

'rename the table object
tdf.Name = "MyNewTable"

'crate a new field object
Set fld = tdf.CreateField("MyField", dbText)

'add the field object to your table object
tdf.Fields.Append fld

Set fld = Nothing
Set tdf = Nothing

End Function

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.