[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Copy data from one mdb to other mdb through vb6

Posted on 2004-10-28
Medium Priority
Last Modified: 2008-03-17
The code resides in VB.
I need to copy data from 4 tables from one mdb to other. 3 of the tables have same structure in both the databases. One table has additional fields in second database.

What is the best way of doing it through VB? Currently I used the following method

---Set objMsAccess = CreateObject("Access.Application")
---objMsAccess.OpenCurrentDatabase f_strOldDbPath, True
---Set l_rstOld = objMsAccess.CurrentDb.OpenRecordset("Select .. From tblSubset")

---Open the second database in the same way as above
---Loop through the recordset and insert records one by one to the table in other database

I know this is not the best way of doing. Is this way of connection is better than OLEDB or ODBC? Can anyone suggest a better method?

1) The two databases can be of different versions (eg: one is 'access97' and the other is 'access2000'). The code should copy the data from one version to other.

Thanks in Advance.
Question by:Coolcld
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

Expert Comment

ID: 12431161
Hi Coolcld

Public Sub CopyObject( _
  eType As AcObjectType, _
  strName As String, _
  strNewName As String, _
  strDatabase As String)

  On Error GoTo PROC_ERR
  ' Overwrite existing object
  DoCmd.SetWarnings False
  DoCmd.CopyObject strDatabase, strNewName, eType, strName
  DoCmd.SetWarnings True

  Exit Sub
  MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
  Resume PROC_EXIT
End Sub

Cheers Sash

Expert Comment

ID: 12431183
Ingnore my previous post

1. Open the database that you want to copy the data to.
2. Link the tables from the source database
3. Write a simple insert query to extract the data from the linked table to the destination table.

insert into [table name] (field list) select * from [source table name]
LVL 41

Accepted Solution

shanesuebsahakarn earned 600 total points
ID: 12431648
OpenCurrentDatabase opens an instance of Access to execute your code, so it is probably not the best way.

A better way would be to use a DAO connection.

Dim db As DAO.Database
Dim wks As DAO.WorkSpace

Set wks=CreateWorkspace("","admin","")
Set db=wks.OpenDatabase("C:\MyDb.mdb")

db.Execute "INSERT INTO MyTable IN 'C:\MyOtherDB' (Field1, Field2) SELECT Field1, Field2 FROM MyTable"

Author Comment

ID: 12431726
Hi SashP,
I need to link & delink the tables at run time right? Because the source database will be available only at run time. i.e. Users will select the source database.

Author Comment

ID: 12431755
Hi shanesuebsahakarn ,

Can we use ADODB instead of DAO? Which one is best?

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

656 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