?
Solved

Rollback a RunSQL command in code

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
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 …
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

752 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