Solved

Import Access tables using VB 6.0

Posted on 2008-06-20
8
371 Views
Last Modified: 2009-03-10
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
0
Comment
Question by:vj_mi
  • 5
  • 3
8 Comments
 
LVL 76

Expert Comment

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

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21830094
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

0
 

Author Comment

by:vj_mi
ID: 21830605
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
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 21831050
Sorry. Missed the destination table name off the end.

acApp.DoCmd.TransferDatabase acImport, "Microsoft Access", "C:\NewDB.mdb", acTable, "Emp", "Emp"

The OpenCurrentDatabase does that. There is no parameter to stop it. You can hide it again immediately, but don't forget to quit the (invisible) application
    Dim acApp As Access.Application
    Set acApp = New Access.Application
   
    acApp.OpenCurrentDatabase "C:\Test.mdb"
    acApp.Visible = False
    acApp.DoCmd.TransferDatabase acImport, "Microsoft Access", "C:\NewDB.mdb", acTable, "Emp", "Emp"
    MsgBox "Done"
    acApp.Quit

Open in new window

0
 

Author Comment

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

Regards,
MI
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21836785
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

0
 

Author Comment

by:vj_mi
ID: 21837138
Hi,

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

Regards,
MI
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21837143
OK, thanks and good luck.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question