Ryan said:
Hi Dirk,
how do i adapt this to copy the objects from a outside database into the
current one...including modules??
You'll use DoCmd.TransferDatabase instead of DoCmd.CopyObject to do the
actual copying, but you'll have to use a different method to enumerate the
objects in the source database. Do you want to copy all the objects in the
other database? This should work, though it's air code and may need some
tweaking:
'----- start of code -----
Dim strSourceDB As String
strSourceDB = "C:\Your\Path\To\YourSourceDB.mdb"
Dim dbSource As DAO.Database
Dim tdf As DAO.TableDef
Dim qdf As DAO.QueryDef
Dim cnt As DAO.Container
Dim doc As DAO.Document
Set dbSource = DBEngine.OpenDatabase(strSourceDB)
' Import tables.
For Each tdf In dbSource.TableDefs
If Left$(tdf.Name, 4) <> "MSys" Then
DoCmd.TransferDatabase _
acImport, _
"Microsoft Access", _
strSourceDB, _
acTable, _
tdf.Name, _
tdf.Name
End If
Next tdf
' Import queries.
For Each qdf In dbSource.QueryDefs
If Left$(qdf.Name, 1) <> "~" Then
DoCmd.TransferDatabase _
acImport, _
"Microsoft Access", _
strSourceDB, _
acQuery, _
qdf.Name, _
qdf.Name
End If
Next qdf
' Import forms.
Set cnt = dbSource.Containers("Forms")
For Each doc In cnt.Documents
DoCmd.TransferDatabase _
acImport, _
"Microsoft Access", _
strSourceDB, _
acForm, _
doc.Name, _
doc.Name
Next doc
Set cnt = Nothing
' Import reports.
Set cnt = dbSource.Containers("Reports")
For Each doc In cnt.Documents
DoCmd.TransferDatabase _
acImport, _
"Microsoft Access", _
strSourceDB, _
acReport, _
doc.Name, _
doc.Name
Next doc
Set cnt = Nothing
' Import modules.
Set cnt = dbSource.Containers("Modules")
For Each doc In cnt.Documents
DoCmd.TransferDatabase _
acImport, _
"Microsoft Access", _
strSourceDB, _
acModule, _
doc.Name, _
doc.Name
Next doc
Set cnt = Nothing
' Import macros.
Set cnt = dbSource.Containers("Scripts")
For Each doc In cnt.Documents
DoCmd.TransferDatabase _
acImport, _
"Microsoft Access", _
strSourceDB, _
acMacro, _
doc.Name, _
doc.Name
Next doc
Set cnt = Nothing
' NOTE: This code does not import relationships. The following
' Microsoft KB article gives code for that, which can easily be
' adapted to fit in this spot:
'
'
http://support.microsoft.com/kb/128157
' Clean up after all the imports.
dbSource.Close
Set dbSource = Nothing
'----- end of code -----