Link to home
Start Free TrialLog in
Avatar of vj_mi
vj_mi

asked on

Import Access tables using VB 6.0

Hello,

I have a blank Access database. I open DB in Access then use menu option "File>Get External data->Import" and import the tables from another database to current db.

Now I want to automate this process using VB 6.0. How do I Import tables along with data, indexes, default values, contraints from an access database into another database?

Regards,
MI
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

acApp is your Access application object.
acApp.DoCmd.TransferDatabase acImport, "Microsoft Access", "C:\My Folder\MyDatabase.mdb", acTable, "MyTable"

Open in new window

Hmm, I see that you are a beginner, so perhaps I should explain a bit more.
The extended snippet uses early binding, so you need to set a reference to the Microsoft Access Object Library via Project/References in the VB menu.
Option Explicit
 
Private Sub Command1_Click()
    Dim acApp As Access.Application
    Set acApp = New Access.Application
    
    acApp.OpenCurrentDatabase "C:\My Folder\MyMainDatabase.mdb"
    acApp.DoCmd.TransferDatabase acImport, "Microsoft Access", "C:\My Folder\MyDatabase.mdb", acTable, "MyTable"
End Sub

Open in new window

Avatar of vj_mi
vj_mi

ASKER

Hi,

My source database is called Test.mdb and has one table Emp.
My target database has no tables.
When I run the below code, I get error: Cannot find object Emp but this table is existing. To check this, I used the following line after opendatabase method: acApp.DoCmd.OpenTable "Emp" and this works fine; so the table is existing.

  Dim acApp As Access.Application
    Set acApp = New Access.Application
   
    acApp.OpenCurrentDatabase "C:\Test.mdb"
   
    acApp.DoCmd.TransferDatabase acImport, "Microsoft Access", "C:\NewDB.mdb", acTable, "Emp"
    MsgBox "Done"

One another point: Why does it open MS-Access application?

Regards,
MI
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vj_mi

ASKER

It is still giving the same error. Is there any way I can check the tables collection in the source database?

Regards,
MI
This uses ADO and ADOX, so needs references to the Microsoft Active Data Objects and Microsoft ADO EXT for DDL and Security libraries.

Private Sub Command2_Click()
    Dim axCatalog As New ADOX.Catalog
    Dim axTable As ADOX.Table
    Dim adConnection As New ADODB.Connection
    Dim ConnString As String
    Dim strMessage As String
    
    Const strDatabaseName As String = "C:\NewDB.mdb"
    
    ConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strDatabaseName & ";  Persist Security Info=False"
    
    adConnection.Open ConnString
    Set axCatalog.ActiveConnection = adConnection
    For Each axTable In axCatalog.Tables
        Debug.Print axTable.Type, axTable.Name
    Next
End Sub

Open in new window

Avatar of vj_mi

ASKER

Hi,

Your code worked. My mistake: I swaped the source and destination database names. Now it is working. Thanks.

Regards,
MI
OK, thanks and good luck.