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
LVL 1
TSIsolutionsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 Access MVP)Database Architect / Systems AnalystCommented:
And you might want to error trapping which include a wks.RollBack ...

mx
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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 Access MVP)Database Architect / Systems AnalystCommented:
@ 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 Access MVP)Database Architect / Systems AnalystCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.