Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Import Access tables using VB 6.0

Posted on 2008-06-20
8
Medium Priority
?
377 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 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

Technology Partners: 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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
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…
Suggested Courses

610 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