Do we have transactions in MS-Access?
It looks like we do: MSDN - TRANSACTION Statement (Microsoft Access SQL)
Transactions are not started automatically. To start a transaction, you must do so explicitly using:
BEGIN TRANSACTION
Conclude a transaction by committing all work performed during the transaction:
COMMIT [TRANSACTION | WORK]
Conclude a transaction by rolling back all work performed during the transaction:
ROLLBACK [TRANSACTION | WORK]
Are we able to use MS Access transactions with acCmdSaveRecord?
No, not as a general rule. So the use of transactions is for VBA code, and sql or recordset code you have. Unfortunately, the transactions don't apply to forms.
However, you can as a work around:
start a transaction.
Create a recordset.
Shove (assign) the forms record source to the above created record set.
Now, the user can freely edit, change, add, delete to that form, but if you don't commit the record set after all is above and done, then the editing etc. will be ignored. It can be bit of a pain to set this up, and for sub forms, I never really cooked up a seamless coding approach that worked well.
So if you willing to create a recordset in code and then assign that reocrdset to the forms datasouce, the result is now a form that is bound to the transaction you started in code.
Access SQL Transactions?
Transactions are not supported in MSAccess when you are using them in a query. Try firing a function to create those queries, which you can do in a module and have it run on PageLoad or even an OnTimer event. You can use the BeginTrans and Rollback commands inside the module.
How to properly use transactions with workspaces in ms-access
This is pretty odd. DBEngine.Workspaces(0)
is the default workspace, and you are not supposed to close it. Normally you only close workspaces that you have opened yourself with DBEngine.CreateWorkspace
.
Your problems may result from mixing your own workspace variable with CurrentDb
, which isn't part of your workspace (not really sure about that).
When using the default workspace (either for possible rollback or for performance), I always use a separate database variable, that is defined as "child" of the workspace:
Dim WS As Workspace
Dim DB As Database
Set WS = DBEngine(0)
Set DB = WS.Databases(0)
WS.BeginTrans
DB.Execute "Stuff" ' Not CurrentDb
If "everything ok" Then
WS.CommitTrans
Else
WS.Rollback
End If
' With a local WS variable, even this is not necessary, but definitely no WS.Close here!
Set WS = Nothing
In your case you'd have to declare DB
as public variable, or pass it as parameter to testChildTransaction()
SQL query for last transaction on MS Access
To pull out the most recent transaction par customer, a correlated subquery should get the job done:
select t.*
from mytable t
where customerpaydate = (
select max(Customerpaydate) from mytable t1 where t1.customerid = t.customerid
)
How do I implement transactions in Access?
Researching these transactions, I found this link promising:
http://support.microsoft.com/kb/248011
Yet, there seem to be some other issues with it.
MS Access 2010: Adding transaction management into a form
you should use transactions only if you are inserting/updating multiple SQL statements which then make sense to for rollback. Also use transactions just before SQL execution and trap errors to find out which SQL statement triggers the failure.
pseudo:
- Do the validation
- Prepare SQL statements
- Prepare SQL Statement 2
- Begin Transaction
- Execute SQL statements
- Commit or rollback
in code it would be:
Private Sub mTrans()
Dim myDB As DAO.Database
Set myDB = CurrentDb
Dim SQL_SET As String
SQL_SET = "First sql statement"
On Error GoTo ERROR_SQL1:
DBEngine.BeginTrans
myDB.Execute SQL_SET, dbFailOnError
On Error GoTo ERROR_SQL2:
SQL_SET = "second sql statement..." 'either use the same variable or use SQL_SET1 for better overview
myDB.Execute SQL_SET, dbFailOnError
DBEngine.CommitTrans
EXIT_SUB:
On Error Resume Next
Set myDB = Nothing
Exit Sub
ERROR_SQL1:
DBEngine.Rollback
MsgBox "Error while executing sql_1. " & vbNewLine & "System msg: " & Err.description
GoTo EXIT_SUB
ERROR_SQL2:
DBEngine.Rollback
MsgBox "Error while executing sql_2. " & vbNewLine & "System msg: " & Err.description
GoTo EXIT_SUB
End Sub
Related Topics
Why Is Double.Nan Not Equal to Itself
Remove Item in Dictionary Based on Value
How to Upload Files Using Ajax to ASP.NET MVC Controller Action
How to Compare Two Images Using Byte Arrays
Visualizing an Ast Created with Antlr (In a .Net Environment)
How to Make Correct Date Format When Writing Data to Excel
Split String into Smaller Strings by Length Variable
Very Simple Definition of Initializecomponent(); Method
How to Find the Position of a Cursor in a Text Box? C#
Error Deserializing Xml to Object - Xmlns='' Was Not Expected
How to Set a Proxy for Webbrowser Control Without Effecting the System/Ie Proxy
How to Get Full Host Name + Port Number in Application_Start of Global.Aspx
Why Does the Async Keyword Exist
Dynamically Cross-Join Multiple Different-Size Collections Together in Linq (C#)