Solved

Copy table between MS Access files using VB

Posted on 2008-06-16
5
1,450 Views
Last Modified: 2012-05-05
I am having 2 MDB Access files say MDB_EMPTY and MDB_DATA which are having 1 table each.
Both are same tables say DB_ADDRESS. But DB_ADDRESS in MDB_EMPTY is having no data in it
and whereas MDB_DATA is having some data in it (say some 500 records).

Now I have to write a program in VB 6.0 to transfer those data from MDB_DATA to MDB_EMPTY.

Can anybody suggest in which way I have to do this?
0
Comment
Question by:siva_gk
[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
  • 2
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 21791380
The destination database does not need to be open. The source database does. If you dont open the database how do you expect to do a simple copy operation?

The only other way to "copy" a database object from one database to another with both being closed is to use a sql action query where you have to painstakenly use ADOX or such to retrieve the source objects table schema and recreate it in the other database. Then performa select into statement to copy the data. I honestly dont see why you cant have the 1 database opened via code for the easier methods I linked you to in my faq item.

This is from the first method in my faq and works without any problems having the destination database closed.

Option Explicit
'Add a reference to MS Access xx.0 Object Library
Private Sub Command1_Click()
    Dim oApp As Access.Application
    Set oApp = New Access.Application
    oApp.OpenCurrentDatabase "C:\Users\VB-Guru\Documents\RobDog888.mdb", False
    oApp.DoCmd.SetWarnings False
    oApp.DoCmd.CopyObject "C:\Users\VB-Guru\RobDog888.mdb", "NewTable", acTable, "Table1"
    oApp.DoCmd.SetWarnings True
    oApp.CloseCurrentDatabase
    oApp.Quit acQuitSaveAll
    Set oApp = Nothing
End Sub

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 21791387
How do I import/copy a database object from another database?
http://www.vbforums.com/showthread.php?t=409309
0
 
LVL 2

Expert Comment

by:ema20
ID: 21791724
First you need to open the source database to copy the data in it by making instance of the ADO connection class, Using Excute method of the Connection object via action query INSERT INTO and specify the destination within the INSERT INTO string. Please see the code Snippet below.

I hope it will help you..



'Using ADO Connection Class
'References Microsoft ActiveX Data Objects
Private Sub Command1_Click()
Dim MDB_Data As New ADODB.Connection, destination As String 
destination = "d:\MDB_DATA.mdb" 'Destination of Data
MDB_Data.CursorLocation = adUseClient
MDB_Data.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\MDB_DATA.mdb" 'Source of Data
MDB_Data.Execute "INSERT INTO DB_ADDRESS IN '" & destination & "' SELECT * from DB_ADDRESS"
End Sub

Open in new window

0
 
LVL 2

Accepted Solution

by:
ema20 earned 500 total points
ID: 21791801
sorry i've send the wrong source code for destination of data instead of MDB_DATA please replace it by destination = "d:\MDB_EMPTY". im sorry this my first time to answer question on this forums.
'Using ADO Connection Class
'References Microsoft ActiveX Data Objects
Private Sub Command1_Click()
Dim MDB_Data As New ADODB.Connection, destination As String 
destination = "d:\MDB_EMPTY.mdb" 'Destination of Data
MDB_Data.CursorLocation = adUseClient
MDB_Data.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\MDB_DATA.mdb" 'Source of Data
MDB_Data.Execute "INSERT INTO DB_ADDRESS IN '" & destination & "' SELECT * from DB_ADDRESS"
End Sub

Open in new window

0
 

Author Comment

by:siva_gk
ID: 21872085
thanks ema...

I already changed that in my ocde.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…

739 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