Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

transferspreadsheet fails within transaction

Posted on 2013-12-08
9
Medium Priority
?
514 Views
Last Modified: 2013-12-09
hey guys,

when i use DoCmd.TransferSpreadsheet within a transaction i get an error

transactions
DBEngine.Workspaces(0).BeginTrans
Call DoCmd.TransferSpreadsheet(acImport, acSpreadsheetTypeExcel9, "LOCALImport_Activity_tbl", strActivitiesImportFile, True)
DBEngine.Workspaces(0).CommitTrans

Open in new window


Question --> 1) i believe it is impossible to use DoCmd.TransferSpreadsheet within a transaction. is this true?
2) could yall explain why this is impossible? i believe it's because the tables are locked against external transactions so that it Access can roll back transactions within Access (sorry a simplified explanation!)
0
Comment
Question by:developingprogrammer
[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 61

Expert Comment

by:mbizup
ID: 39705780
Are you saying that you are recieving that error with just those three lines of code?

If so, then 'something else' is tying up your table.

These three lines work without issue for me:

DBEngine.Workspaces(0).BeginTrans
Call DoCmd.TransferSpreadsheet(acImport, , "tblTest", "C:\Users\myname\Desktop\testfile.xls", True)
DBEngine.Workspaces(0).CommitTrans

Open in new window

0
 

Author Comment

by:developingprogrammer
ID: 39706312
hi mbizup!

thanks for that! i didn't think other things were affecting the code but as per your suggestion i retried it and i realised that the cause of the problem is this specifically

    DBEngine.Workspaces(0).BeginTrans
            Call CurrentDb.Execute("DELETE * FROM LOCALImport_Activity_tbl;", dbFailOnError)
            
            Call DoCmd.TransferSpreadsheet(acImport, acSpreadsheetTypeExcel9, "LOCALImport_Activity_tbl", strActivitiesImportFile, True)
    DBEngine.Workspaces(0).CommitTrans

Open in new window


hrmm... i'm just trying to think why would it cause a problem? i really can't understand it. works perfectly if i don't put transactions
0
 
LVL 39

Accepted Solution

by:
PatHartman earned 1000 total points
ID: 39706337
It is possible that the Delete is causing the problem because it creates its own transaction and  that would make a transaction within a transaction.  Remember that when you run an action query from the GUI, you get a message at the end that allows you to undo the changes.  That tells you that the action happens within a transaction.  I'm not sure how using the Execute method impacts the procedure since you don't get the roll back option but it looks like the transaction is still happening.

If you really need to keep the old data if the new data doesn't get imported, you may need to back it up yourself and put it back if necessary.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 1000 total points
ID: 39706363
OK - here's what's happening (my understanding):

Both of those statements affect the same table in different ways.  The delete statement removes records; the transferspreadsheet statements adds records.

IF you were deleting records from a very large table, it might take a while.  You wouldn't want your records from the spreadsheet to be inserted *before* the deletion is complete, right?  Because if that happened some or all of your inserted records would get deleted --> unpredictable results.

So my understanding of transactions is that they protect against that sort of situation.

1.  You begin a transaction
2.   You initiate a delete query on LOCALImport_Activity_tbl
3.   Access LOCKS that table specifically for your delete query, but doesn't commit the deletion -- which you can roll back.  No other data changes (action queries, transfer spreadheet, etc) can be run against the LOCALImport_Activity_tbl at this time.
4.   You commit the transaction (CommitTrans), and the lock on the table is released

The problem with your code is that you are trying to run TransferSpreadheet, which is effectively an INSERT query at stage 3, when the table is locked specifically for the DELETE query, before that transaction has been committed (so it fails -- which is a good thing :-)  )

The correct placement for the TransferSpreadsheet would be either on it's own or in a *separate* transaction  AFTER the commitTrans statement for the DELETE query.
0
 

Author Comment

by:developingprogrammer
ID: 39706422
whao mbizup and Pat, your explanation is really great and i really, really enjoy learning from you all!! it's so enlightening = )

i think what Pat says and what you're saying is quite similar with some slight differences (kindly feel free to correct me if i'm wrong)

Pat is saying the problem is a transaction in a transaction.
mbizup is saying that there is a modification to a transaction-locked table

hrmm i think mbizup to prove that your postulate is correct, i will have to try and run an action query against the table to see if it fails or not. at the moment i'm kinda time strapped and i guess understanding the effect for this case doesn't really change the outcome that much however i'm definitely bearing this in mind - the locking of tables for transactions.

Pat i think what you're saying makes sense and the point about the GUI i think is a really really good one. i didn't think of interpreting the GUI's "undo" as putting the action query in a transcation - but now i do = )

ok guys, your 2 postulates are definitely enlightening enough for me as a business application programmer - if i were in academia / research then i'll go deeper - but i'm learning to embrace my constraints and focus on getting the app up and running! thanks for spending the time to share mbizup and Pat! = ))
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39706477
<guys>

gals  

(saying that with a light spirit intended -- we're a bit of a minority on technical forums, and it is very nice to see Pat as a newcomer here.)
0
 

Author Comment

by:developingprogrammer
ID: 39706507
haha yes i've seen other experts address you as Miriam but without consent i still address you as mbizup for respect of the anonymity the nick provides! = PP

what i didn't know that Pat is a gal as well! i thought Patrick = ) great to be taught by the gals of EE!! = ))
0
 
LVL 39

Expert Comment

by:PatHartman
ID: 39706596
Thanks Miriam.  Have we met somewhere else?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39706958
Pat - definitely through the MVP community, possibly elsewhere.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

604 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