?
Solved

Rollback a RunSQL command in code

Posted on 1998-10-27
2
Medium Priority
?
610 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 600 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Suggested Courses

850 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