Solved

Import Access tables using VB 6.0

Posted on 2008-06-20
8
370 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Opening Remote & Local Data Connection 2 57
clicking a shape in a frame array vb6 3 45
Help me. 3 54
IF ELSE Statement in Excel Macro VBA 16 62
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 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