?
Solved

transferspreadsheet fails within transaction

Posted on 2013-12-08
9
Medium Priority
?
508 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 38

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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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 38

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

770 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