• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 654
  • Last Modified:

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.

Thanks,
Doug
0
DougR_MS
Asked:
DougR_MS
1 Solution
 
InvolveITCommented:
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"

Syntax

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

The TransferDatabase method has the following arguments.

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

databasetype
      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.
databasename
      A string expression that's the full name, including the path, of the database you want to use to import, export, or link data.
objecttype      
              One of the following intrinsic constants:
acForm
acMacro
acModule
acQuery
acReport
acTable (default)
source      
              A string expression that's the name of the object whose data you want to import, export, or link.
destination
              A string expression that's the name of the imported, exported, or linked object in the destination database.
structureonly
              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.
saveloginid      
              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

      currentdb.tabledefs.refresh

Good Luck
0
 
stevbeCommented:
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

'cleanup
Set fld = Nothing
Set tdf = Nothing

End Function

Steve
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now