Solved

Rollback a RunSQL command in code

Posted on 1998-10-27
2
546 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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
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 …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

763 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

10 Experts available now in Live!

Get 1:1 Help Now