Solved

transferspreadsheet fails within transaction

Posted on 2013-12-08
9
494 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
  • 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 36

Accepted Solution

by:
PatHartman earned 250 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 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 36

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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.

749 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