Solved

Import Access tables using VB 6.0

Posted on 2008-06-20
8
365 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now