Solved

Import Access tables using VB 6.0

Posted on 2008-06-20
8
373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

691 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