Solved

Rollback a RunSQL command in code

Posted on 1998-10-27
2
556 Views
Last Modified: 2012-06-27
I am trying to write code that can commit or rollback a query run via Docmd.RunSQL.  The following code doesn't throw up errors, but neither does it rollback the transaction.

Dim dbs As Database
Dim rstRecords As Recordset
Dim MyWorkspace As Workspace
Set MyWorkspace = DBEngine.Workspaces(0)
Set dbs = CurrentDb

On Error GoTo CancelMyTrans

MyWorkspace.BeginTrans

DoCmd.RunSQL "UPDATE tblNewTable SET tblNewTable.Company = ""Change"" ;", True

If MsgBox("Commit these records", vbYesNo) = vbYes Then
    MyWorkspace.CommitTrans
    Set MyWorkspace = Nothing
    Exit Sub
End If

CancelMyTrans:

MyWorkspace.Rollback
Set MyWorkspace = Nothing
MsgBox "All transactions rolled back"
0
Comment
Question by:pauloflaherty
2 Comments
 
LVL 9

Accepted Solution

by:
perove earned 150 total points
ID: 1966934
You can't do it to a rollback on the docmd object. Use this method instead:

Dim dbs As Database
Dim rstRecords As Recordset, qdf As QueryDef
Dim MyWorkspace As Workspace
Set MyWorkspace = DBEngine.Workspaces(0)
Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("query1")
On Error GoTo CancelMyTrans
qdf.SQL = "UPDATE Table1 SET Table1.f1 = 59;"
MyWorkspace.BeginTrans
qdf.Execute
'MyWorkspace.Databases(1).QueryDefs("1").Execute

If MsgBox("Commit these records", vbYesNo) = vbYes Then
    MyWorkspace.CommitTrans
    Set MyWorkspace = Nothing
    Close
    Exit Function
End If
Close

CancelMyTrans:

MyWorkspace.Rollback
Set MyWorkspace = Nothing
MsgBox "All transactions rolled back"

Just remember to create a query called query1 (or whatever)
It doesent matter what is in it 'cause you set the SQL by code

perove

0
 

Author Comment

by:pauloflaherty
ID: 1966935
Thanks - that works great.

Seems strange that it will not work with docmd.openquery or docmd.runsql, especially when the help file for runsql mentions an argument for using transactions - guess this is referring to something else.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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 …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now