RobDog888
Jun 5th, 2006, 07:57 AM
There are two easy methods for transfering/copying database objects between databases.
The first is the .CopyObject method of the DoCmd object:
expression.CopyObject DestinationDatabase, NewName, SourceObjectType, SourceObjectName
DestinationDatabase: [Optional] Variant. The first argument is the path and file name for the database you want to copy the object into. To select the current database, leave this argument blank.
NewName: [Optional] The new String name of the object if you want to perform a renaming. To keep the original name then just leave this argument blank.
SourceObjectType: [Optional] The type of Access Object to be copied...
• acDataAccessPage
• acDefault default
• acDiagram
• acForm
• acFunction
• acMacro
• acModule
• acQuery
• acReport
• acServerView
• acStoredProcedure
• acTable
SourceObjectName: [Optional] String. The source name of the object to be copied.
Access 2003 VBA Code Example:
Public Sub ImportTable()
'Turn off any warning messages
Application.DoCmd.SetWarnings False
Application.DoCmd.CopyObject "DestinationDatabase", "NewName", acTable, "SourceObjectName"
'Turn warning messages back on
Application.DoCmd.SetWarnings True
End Sub
The other is the .TransferDatabase method also of the DoCmd object:
expression.TransferDatabase(TransferType, DatabaseType, DatabaseName, ObjectType, Source, Destination, StructureOnly, StoreLogin)
TransferType: [Optional] Integer Constant.
• acExport
• acImport default
• acLink
DatabaseType: [Optional] Variant. Name of the type of database used for the import.
• Microsoft Access default
• Jet 2.x
• Jet 3.x
• dBase III
• dBase IV
• dBase 5.0
• Paradox 3.x
• Paradox 4.x
• Paradox 5.x
• Paradox 7.x
• ODBC Databases
• WSS
DatabaseName: [Optional] Variant. Full name and filepath of the database being used for the transfer type.
ObjectType: [Optional] Integer Constant. The type of object whose data you want to import, export, or link with.
• acDataAccessPage
• acDefault
• acDiagram
• acForm
• acFunction
• acMacro
• acModule
• vacQuery
• acReport
• acServerView
• acStoredProcedure
• acTable default
Source: [Optional] Variant. Source name of the object to be transfered or linked to.
Destination: [Optional] Variant. The name of the imported, exported, or linked object in the destination database.
StructureOnly: [Optional] Variant. False (default). True to import or export only the structure of a database table and False to import or export the structure of the table and its data.
StoreLogin: [Optional] Variant. 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. This argument is available only in Visual Basic.
Access 2003 VBA Code Example:
Option Explicit
Public Sub ImportTable()
'Turn off any warning messages
Application.DoCmd.SetWarnings False
Application.DoCmd.TransferDatabase acImport, "Microsoft Access", "D:\DB2ImportFrom.mdb", acTable, "Table1", "ImportedTableName", False
'Turn warning messages back on
Application.DoCmd.SetWarnings True
End Sub
The first is the .CopyObject method of the DoCmd object:
expression.CopyObject DestinationDatabase, NewName, SourceObjectType, SourceObjectName
DestinationDatabase: [Optional] Variant. The first argument is the path and file name for the database you want to copy the object into. To select the current database, leave this argument blank.
NewName: [Optional] The new String name of the object if you want to perform a renaming. To keep the original name then just leave this argument blank.
SourceObjectType: [Optional] The type of Access Object to be copied...
• acDataAccessPage
• acDefault default
• acDiagram
• acForm
• acFunction
• acMacro
• acModule
• acQuery
• acReport
• acServerView
• acStoredProcedure
• acTable
SourceObjectName: [Optional] String. The source name of the object to be copied.
Access 2003 VBA Code Example:
Public Sub ImportTable()
'Turn off any warning messages
Application.DoCmd.SetWarnings False
Application.DoCmd.CopyObject "DestinationDatabase", "NewName", acTable, "SourceObjectName"
'Turn warning messages back on
Application.DoCmd.SetWarnings True
End Sub
The other is the .TransferDatabase method also of the DoCmd object:
expression.TransferDatabase(TransferType, DatabaseType, DatabaseName, ObjectType, Source, Destination, StructureOnly, StoreLogin)
TransferType: [Optional] Integer Constant.
• acExport
• acImport default
• acLink
DatabaseType: [Optional] Variant. Name of the type of database used for the import.
• Microsoft Access default
• Jet 2.x
• Jet 3.x
• dBase III
• dBase IV
• dBase 5.0
• Paradox 3.x
• Paradox 4.x
• Paradox 5.x
• Paradox 7.x
• ODBC Databases
• WSS
DatabaseName: [Optional] Variant. Full name and filepath of the database being used for the transfer type.
ObjectType: [Optional] Integer Constant. The type of object whose data you want to import, export, or link with.
• acDataAccessPage
• acDefault
• acDiagram
• acForm
• acFunction
• acMacro
• acModule
• vacQuery
• acReport
• acServerView
• acStoredProcedure
• acTable default
Source: [Optional] Variant. Source name of the object to be transfered or linked to.
Destination: [Optional] Variant. The name of the imported, exported, or linked object in the destination database.
StructureOnly: [Optional] Variant. False (default). True to import or export only the structure of a database table and False to import or export the structure of the table and its data.
StoreLogin: [Optional] Variant. 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. This argument is available only in Visual Basic.
Access 2003 VBA Code Example:
Option Explicit
Public Sub ImportTable()
'Turn off any warning messages
Application.DoCmd.SetWarnings False
Application.DoCmd.TransferDatabase acImport, "Microsoft Access", "D:\DB2ImportFrom.mdb", acTable, "Table1", "ImportedTableName", False
'Turn warning messages back on
Application.DoCmd.SetWarnings True
End Sub