Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

Using Transactions in MS Access VBA Issue

Hi Experts,

I have an issue concerning using transactions in VBA.
I am trying to surround 2 queries that modify an existing table (Table1) in a transaction.

Table1 has data already in it and Field1 is set to Indexed and No Duplicates.

My code looks something like this:

BeginTrans

CurrentDB.Execute "DELETE * FROM Table1", dbFailOnError
CurrentDb.Execute "INSERT INTO Table1 (Field1, Field2) SELECT NewField1, NewField2 From Table2", dbFailOnError

CommitTrans

I am trying to DELETE all the records in Table1 and repopulate the table using Table2.
However I keep getting the following error:

"The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship.  Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again."

What I don't understand is if the data is suppose to be completely deleted, why would there be duplicates? Btw there are no duplicate values in Table2 so that's not the issue.

Is it because I am using transactions and because of that it doesn't see that the records will be deleted before they'll be inserted?

I've been working with workspaces for a bit now but I keep getting weird logic errors like that when I try to execute too many queries within a transaction.

I would really like to find a solution to the problem as the success of the execution of the queries in this procedure is really important as a whole.  

Please help! Regards,
TSI Solutions
0
TSIsolutions
Asked:
TSIsolutions
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Kelvin SparksCommented:
You need to create an ADODB connection to run these. Run the transaction within this connection. If you use the default DAO connection, Access uses a new connection for each part, and one query won't see the changes of the other until the commit.


Kelvin
0
 
Gustav BrockCIOCommented:
You need a common workspace:

Dim wks As DAO.WorkSpace
Dim dbs As DAO.Database

Set wks = DBEngine(0)
Set dbs = wks(0)

wks.BeginTrans
    dbs.Execute "DELETE * FROM Table1", dbFailOnError
    dbs.Execute "INSERT INTO Table1 (Field1, Field2) SELECT NewField1, NewField2 From Table2", dbFailOnError
wks.CommitTrans

/gustav
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
And you might want to error trapping which include a wks.RollBack ...

mx
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
TSIsolutionsAuthor Commented:
Hi Kelvin,

You were right about working in ADO and not DAO. I creates initally a workspace with DAO and it kept giving me errors.
With ADO it worked like a charm and had no issues.

Thanks again for the help... it was driving me crazy.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
@ TSIsolutions

FWIW ... I have worked with Transactions for years, running multiple actions queries within a Transaction.
With all due respect to Kevin Sparks, there is absolutely no need to create an ADODB connection to do this.  Gustav has shown the correct and simple way to do this.

mx
0
 
TSIsolutionsAuthor Commented:
Hi Joe,

Maybe it's something that I have to look at again however, I tested the specific case where records in a table are deleted and then records were inserted into the same table.

Working with transactions in a workspace using DAO, the INSERT would not work and I would get the error "would create duplicates values in the destination table". That should not have happened given that the table was completely empty.

Doing the same thing with ADO Workspace in transactions did not give me any issues. Hope that clarifies it.

Lawrence
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Well, I've been doing exactly what you are referring to ... along with a lot more complexity (in the same Transaction) .... and it works just fine.

I have a 'Report Import Manager' that a couple of people in our group use daily to auto load 6-7 Back End dbs on our server (over our 1Gb fiber optic WAN I might add) with reports we get from IT and/or out Metrics group. Some of the more complex auto load process have 15-20 action queries that all run in sequence under a single Transaction, including the sequence of steps you specified ... and a lot more.  There is no ADO involved. It's all DAO (long live DAO).

So ... yes, in the future you might want to revisit this .... just sayin' ...
In the meantime ... whatever works :-)
mx
0
 
Kelvin SparksCommented:
I used to use DAO, (my preferred coding style), until Scott advised me not to in this case

http://www.experts-exchange.com/Database/MS_Access/Q_24642612.html

As he states, not everyone is in this camp.

As with many things Access, there are many ways to solve the issue - with transactions I've found this to be the answer.

Kelvin
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now