473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Whats is the Transactional difference between Docmd.RunSQL and CurrentDb.Execute SQL?

TheSmileyCoder
2,322 Expert Mod 2GB
I have been playing around with transactions and one thing got me wondering. First is the code I am using:

Expand|Select|Wrap|Line Numbers
  1. 'Start transaction
  2.         'Get default Workspace.
  3.             Dim wrkDefault As Workspace
  4.             Set wrkDefault = DBEngine.Workspaces(0)
  5.  
  6.         'Start of transaction
  7.             wrkDefault.BeginTrans
  8.             Dim strSQL As String
  9.             strSQL = "UPDATE tbl_Trans SET tx_Field='jjj' WHERE ID=2"
  10.             'DoCmd.RunSQL strSQL
  11.             CurrentDb.Execute strSQL
  12.             If vbYes = MsgBox("Commit changes?", vbYesNo) Then
  13.                 wrkDefault.CommitTrans
  14.                 Else
  15.                 wrkDefault.Rollback
  16.             End If
Now If I use CurrentDB.Execute strSQL then it works as expected and rolls back the changes I made.
However, If I use DoCmd.RunSQL strSQL the rollback does not take effect.

Now I have also tried both:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL strSQL,True
  2. DoCmd.RunSQL strSQL,False
With the same results.

Can anyone explain this, or point out something I am missing?
Apr 26 '11 #1
4 9879
ADezii
8,834 Expert 8TB
Not sure about the RunSQL approach, but I rarely see it used in the context of a Transaction. The following approach should always work:
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim wrkDefault As DAO.Workspace
  3. Set wrkDefault = DBEngine.Workspaces(0)
  4.  
  5. wrkDefault.BeginTrans
  6.  
  7. strSQL = "UPDATE [Order Details] SET [UnitPrice] = [UnitPrice] * 1.1"
  8.  
  9. CurrentDb.Execute strSQL, dbFailOnError
  10.  
  11. If vbYes = MsgBox("Commit changes?", vbYesNo) Then
  12.   wrkDefault.CommitTrans
  13. Else
  14.   wrkDefault.Rollback
  15. End If
Apr 26 '11 #2
TheSmileyCoder
2,322 Expert Mod 2GB
Hi Adezii and thank you for your prompt reply. I did read it earlier today, but was hoping someone else had more to offer. As I said in the post, I did get the transaction to work using currentDB.Execute strSQL. I guess for a long time I have just been used to using Docmd.RunSQL, so I was curious as why I couldn't get that to work, since it actually specifically takes an argument to indicate whether or not it should be considered part of an transaction.

If I get the time, I will try to research if there is a performance difference between Docmd.RunSQL and CurrentDB.Execute.
Apr 26 '11 #3
ADezii
8,834 Expert 8TB
From the little research that I have done, it appears that CurrentDB.Execute is significantly faster than DoCmd.RunSQL since it is a Method of the Jet Database Engine, and runs the SQL directly.
Apr 27 '11 #4
NeoPa
32,556 Expert Mod 16PB
I haven't got much to add I'm afraid Smiley, but I did read the RunSQL action topic in the Help system which included :
Select Yes to include this query in a transaction. Select No if you don't want to use a transaction. The default is Yes. Prior to Access 97, Access always included the query in a transaction when you ran this action by starting with a BeginTrans method, executing the SQL statement, and then concluding with a CommitTrans method. If you select No for this argument, the query may run faster.

From this I conclude that it probably aught to work exactly as you have used it, but for some reason doesn't :-S

As for which works faster, I would expect the requirement to handle transactions to make a significant difference (whichever method is employed), but I'd be surprised if the two methods differed noticeably in performance outside of that. I say this in the full confidence that my expectations are of little importance, as you will determine the truth of the matter with your testing very shortly :-)
Apr 27 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jim | last post by:
I am using Access 97 on a PC running Windows NT 4.0 SP6a. I have some code (shown below) intended to add a set of records to one table (tblGradeProps) when a new record is created in another...
4
by: Rotsj | last post by:
Hi, i try to execute an update query from within a form, but i get the message: run time error '3144'. Syntax error on the update statement when i try something like this: DoCmd.RunSQL...
0
by: Andy | last post by:
Hello, I am running an sql statement that INSERTS INTO a table. If I run the query using docmd.runSQL, it works fine - new records are added to the table and duplicate records are disregared (...
3
by: Pathfinder | last post by:
Hi All I am trying to run the below but I receive the following error "runsql action requires an argument consisting of an SQL statment" Dim MySQL$ MySQL$ = "Select * from mytablename"...
7
by: Richard Hollenbeck | last post by:
Help! I don't know why this isn't working: Private Sub Combo9_Change() Dim UsersCourseSelection As String UsersCourseSelection = Me("Combo9").Value Combo13.Visible = True 'the following...
8
by: RC | last post by:
In my Access 2002 form, I have a combo box and on the AfterUpdate event I use DoCmd.RunSQL ("UPDATE .... to update records in a table. When it starts to run I get a message "You are about to...
7
by: Jimmer | last post by:
What's better/ faster/ more efficient. Using Docmd.RunSql or Execute to run sql? When is one better than the other or inappropriate to use? Thanks. jim
2
by: ben | last post by:
I have the following code in a VBA module: DoCmd.RunSql "Update tData Set sd = Log(Strike/Price) where symbol = '" & symbol & "'" This statement worked fine, and was using the built in math...
0
by: dstyles7 | last post by:
there are two list boxes in a form i have a command populate button that takes the values from the list box and inserts the primary key value from the selected list box into a access table. The...
1
MitchR
by: MitchR | last post by:
Hello Folks; I have run myself into a hole I cannot get of and need some assistance. I have a Form "Frm_ATandT_Activation_Center" that has a sub form "Frm_ATandT_Activation"....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.