Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Table Import/Export

Status
Not open for further replies.

proximity

Technical User
Sep 19, 2002
132
GB
Hi,

I have an access 2002 database with 15 tables in it. From time to time, I need to export these tables into another, identical database, which I use for experimenting on! And then I need to do the reverse - export the tables back into the source database. In each case, any existing tables will be over-written with the copied ones.

I realise this can be done with a shedload of DoCmd.TransferDatabase acImport or DoCmd.TransferDatabase acExport things. Indeed, there are many similar postings as this, but nothing quite fits the bill . . .

There must be a nice easy way of coding a command button that will do it all for me. Any ideas greatly, as ever, appreciated!

--
Steven
 
proximity,
Here is a general structure that can be used to Import or Export (this sample is Export) by enumerating the AllTables collection.
Code:
Sub ExportAllTableObjects()
Dim objTable As AccessObject
For Each objTable In CurrentData.AllTables
  DoCmd.TransferDatabase acExport, , "C:\TestDatabase.mdb", acTable, objTable.Name, objTable.Name
Next objTable
Set objTable = Nothing
End Sub

Hope this helps,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Be aware that CurrentData.AllTables returns all tables including system tables.

With this query you get linked (Type=6) and local (Type=1) tables, but not system tables.

SELECT MSysObjects.Name
FROM MSysObjects
WHERE (((MSysObjects.Name) Not Like "MSys*") AND ((MSysObjects.Type)=1 Or (MSysObjects.Type)=6));

Code:
Sub ExportAllTableObjects()
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
With rst
   .ActiveConnection = CurrentProject.Connection
   .CursrorLocation = adUseServer
   .CursorType = adOpenForwardOnly
   .LockType = adReadOnly
   .Source  = "SELECT [Name] FROM MSysObjects " & _
              "WHERE ([Name] Not Like "MSys*") AND (Type In (1, 6));"
   .Open
    While Not .EOF
      DoCmd.TransferDatabase acExport, , "C:\TestDatabase.mdb", acTable, .Fields(0), .Fields(0)
    Wend
    .Close
End With
Set rst = Nothing

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top