Solved

Copy table from one database to  same table in another database

Posted on 2016-09-15
9
75 Views
Last Modified: 2016-09-30
I used this part of code in Access 2010  to move records from four current tables in kind of history database that has the same four tables.
In example below that I used  it has one table I adjusted that for four tables. I repeated step 2 and 3 for every table. But got error after first table was moved it means after first step3. I must missing something after step3.


but I got error : "System resource exceeded"

Here is code.
Sub DoArchive()
On Error GoTo Err_DoArchive
  Dim ws As DAO.Workspace   'Current workspace (for transaction).
  Dim db As DAO.Database    'Inside the transaction.
  Dim bInTrans As Boolean   'Flag that transaction is active.
  Dim strSql As String      'Action query statements.
  Dim strMsg As String      'MsgBox message.

  'Step 1: Initialize database object inside a transaction.
  Set ws = DBEngine(0)
  ws.BeginTrans
  bInTrans = True
  Set db = ws(0)

  'Step 2: Execute the append.
  strSql = "INSERT INTO MyArchiveTable ( MyField, AnotherField, Field3 ) " & _
    "IN ""C:\My Documents\MyArchive.mdb"" " & _
    "SELECT SomeField, Field2, Field3 FROM MyTable WHERE (MyYesNoField = True);"
  db.Execute strSql, dbFailOnError

  'Step 3: Execute the delete.
  strSql = "DELETE FROM MyTable WHERE (MyYesNoField = True);"
  db.Execute strSql, dbFailOnError

  'Step 4: Get user confirmation to commit the change.
  strMsg = "Archive " & db.RecordsAffected & " record(s)?"
  If MsgBox(strMsg, vbOKCancel + vbQuestion, "Confirm") = vbOK Then
    ws.CommitTrans
    bInTrans = False
  End If

Exit_DoArchive:
  'Step 5: Clean up
  On Error Resume Next
  Set db = Nothing
  If bInTrans Then   'Rollback if the transaction is active.
    ws.Rollback
  End If
  Set ws = Nothing
Exit Sub

Err_DoArchive:
  MsgBox Err.Description, vbExclamation, "Archiving failed: Error " & Err.number
  Resume Exit_DoArchive
End Sub
0
Comment
Question by:Taras
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 34

Assisted Solution

by:ste5an
ste5an earned 250 total points
ID: 41800450
How big is that table? Do you execute this right after starting Access?

The error message indicates, that you have used too many resources at that point (handles etc.).
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
ID: 41800451
Do you ONLY repeat steps 2 and 3 for subsequent tables?
0
 

Author Comment

by:Taras
ID: 41800463
Yes I only repeat steps 2 and 3 for table tbl2 tbl3 and tbl4
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:Taras
ID: 41800466
All database is size of 200mb and I have around 20 tables.
But I am moving only those 4 the largest one.
0
 
LVL 34

Expert Comment

by:ste5an
ID: 41800485
DBEngine requires some resources, but there is no obvious reason for that.

Is your archive at a fixed location in the file system? Then I would consider using linked tables as a test.
0
 

Author Comment

by:Taras
ID: 41805348
I solved this by putting this part of code before my first step.

     DBEngine.SetOption dbMaxLocksPerFile, 200000

How ever I go an new error:

To ad some complicity I add to each of those four Parent table its child table.
Parent and Child table are in one to many relationship. Complete referential integrity imposed. Everything goes ok up to last table.
In this fourth table (parent table) I first insert records and it went through without error then  when I tried to  insert – copy record  in child table I got error:
You cannot add or change a record because a related record is Required in table...

I am sure that related records are in parent table not sure while I am getting message that they are not.
0
 
LVL 84
ID: 41805394
Are you using the same connection and transaction for all those processes?
0
 

Author Comment

by:Taras
ID: 41805540
Yes I am using same connection and transaction.
0
 
LVL 84
ID: 41806273
Be sure that you're properly filling the Foreign Key field in the child tables with the parent record's PK id value.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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