How do I roll back the operation query executed by docmd. runsql statements?

Source: Internet
Author: User
Q: How do I roll back the operation query executed by docmd. runsql statements? A: You want to use Docmd. RUNSQL implements the transaction RollBack operation. Unfortunately, Access cannot use Docmd. RUNSQL to implement transaction rollback 4. in the help of runsql statement operations, the option is UseTrans.

Q: How do I roll back the operation query executed by docmd. runsql statements? A: You want to use Docmd. RUNSQL implements the transaction RollBack operation. Unfortunately, Access cannot use Docmd. RUNSQL to implement transaction rollback 4. in the help of runsql statement operations, the option is UseTrans.

Problem:

Docmd. runsql Statement RunOf Operation Query HowRollback?

Answer:

You want to use Docmd. RUNSQL to roll back a transaction) Operation

Unfortunately, Access cannot implement transaction rollback through Docmd. RUNSQL. Statement OperationIs UseTransaction. This option is used to confirm whether StatementTransactional Operation. If True is selected (True by default), all OperationWill be treated as a separate Atom OperationTo perform Operation; If False is selected OperationIt will not be treated as a transaction (Dirty Read may occur in the case of multiple users. However, these transactions are completed internally, and we cannot display them by declaring commit or rollback. Operation.

In my experience, ACCESS cannot use Docmd. OPENQUERY to display similar transactions. Operation. If you want to implement the transaction OperationThe unique usage is implemented through WorkSpaceObject. BeginTrans. In the Help File of Access VBA, you can find the following example:

'Ininbegintransvb

'To integrate this code

'Replace the data source and initial catalog values

'In the connection string

Public Sub Main ()

On Error GoTo ErrorHandler

'Recordset and connection variables

Dim Cnxn As ADODB. Connection

Dim strCnxn As String

Dim rstTitles As ADODB. Recordset

Dim strSQLTitles As String

'Record variables

Dim strTitle As String

Dim strMessage As String

'Open connection

StrCnxn = "Provider = 'sqloledb'; Data Source = 'mysqlserver ';"&_

"Initial Catalog = 'pubs'; Integrated Security = 'sspi ';"

Set Cnxn = New ADODB. Connection

Cnxn. Open strCnxn

'Open recordset dynamic to allow for changes

Set rstTitles = New ADODB. Recordset

StrSQLTitles = "Titles"

RstTitles. Open strSQLTitles, Cnxn, adOpenDynamic, adLockPessimistic, adCmdTable

Cnxn. BeginTrans

'Loop through recordset and prompt user

'To change the type for a specified title

RstTitles. MoveFirst

Do Until rstTitles. EOF

If Trim (rstTitles! Type) = "psychology" Then

StrTitle = rstTitles! Title

StrMessage = "Title:" & strTitle & vbCr &_

"Change type to self help? "

'If yes, change type for the specified title

If MsgBox (strMessage, vbYesNo) = vbYes Then

RstTitles! Type = "self_help"

RstTitles. Update

End If

End If

RstTitles. MoveNext

Loop

'Prompt user to commit all changes made

If MsgBox ("Save all changes? ", VbYesNo) = vbYes Then

Cnxn. CommitTrans

Else

Cnxn. RollbackTrans

End If

'Print recordset

RstTitles. Requery

RstTitles. MoveFirst

Do While Not rstTitles. EOF

Debug. Print rstTitles! Title & "-" & rstTitles! Type

RstTitles. MoveNext

Loop

'Restore original data as this is a demo

RstTitles. MoveFirst

Do Until rstTitles. EOF

If Trim (rstTitles! Type) = "self_help" Then

RstTitles! Type = "psychology"

RstTitles. Update

End If

RstTitles. MoveNext

Loop

'Clean up

RstTitles. Close

Cnxn. Close

Set rstTitles = Nothing

Set Cnxn = Nothing

Exit Sub

ErrorHandler:

'Clean up

If Not rstTitles Is Nothing Then

If rstTitles. State = adStateOpen Then rstTitles. Close

End If

Set rstTitles = Nothing

If Not Cnxn Is Nothing Then

If Cnxn. State = adStateOpen Then Cnxn. Close

End If

Set Cnxn = Nothing

If Err <> 0 Then

MsgBox Err. Source & "-->" & Err. Description, "Error"

End If

End Sub

'Enabledintransvb

Finally, we strongly recommend that you read the following documents. This document has a chapter: usage and definition of Transactions in Access.

Advanced Microsoft Jet SQL for Access 2000

Http://msdn.microsoft.com/library/default.asp? Url =/library/en-us/dnacc2k/html/acadvsql. asp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.