Below is the code I have under a "Commit" button on a bound form. It
works fine but this is the first time I have used transactions and I
would like someone to review it and let me know if there is any thing I
am doing wrong. Also, is there a way to test this to see what happens
during a rollback?
Thanks,
Thomas
Private Sub cmdCommit_Click()
On Error GoTo Err_cmdCommit_Click
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim sql As String
Dim AppPath As String
sql = ""
sql = sql & "SELECT tblInvMovement.DateMoved,
tblInvMovement.LotNumber, tblInvMovement.VPartNumber, "
sql = sql & "tblInvMovement.PPartNumber,
tblInvMovement.Description, tblInvMovement.Qnty, "
sql = sql & "tblInvMovement.From, tblInvMovement.To FROM
tblInvMovement;"
AppPath = CurrentDb.Name
Set db = OpenDatabase(AppPath)
Set rst = db.OpenRecordset(sql, dbOpenSnapshot)
'--------------------------------------------------------------------
DBEngine.BeginTrans
Do Until rst.EOF
db.Execute "INSERT INTO tblInventory ( Event_Date,
LotNumber, VPartNumber, PPartNumber, Description, " & _
"" & rst(6) & ", " & rst(7) & ") VALUES ('" &
rst(0) & "', '" & rst(1) & "', '" & rst(2) & "', " & _
"'" & rst(3) & "', '" & rst(4) & "', '" &
(-rst(5)) & "', '" & rst(5) & "');", dbFailOnError
rst.MoveNext
Loop
db.Execute "DELETE * FROM tblInvMovement;", dbFailOnError
DBEngine.CommitTrans dbFlushOSCacheWrites
'--------------------------------------------------------------------
rst.Close
db.Close
Forms![frmInvMovement]![frmSubInvMovement].Requery
Exit_cmdCommit_Click:
Exit Sub
Err_cmdCommit_Click:
DBEngine.Rollback
MsgBox Err.Description
Resume Exit_cmdCommit_Click
End Sub 7 1934
On 6 Jun 2006 06:40:36 -0700, "TD" <dl**@adsi-sc.com> wrote:
Your code seems OK.
To test a rollback, cause an error in the loop. See Err.Raise.
The more fundamental point: why use this loop, rather than a single
insert statement:
insert into tblInventory (...)
select ... from tblInvMovement
-Tom. Below is the code I have under a "Commit" button on a bound form. It works fine but this is the first time I have used transactions and I would like someone to review it and let me know if there is any thing I am doing wrong. Also, is there a way to test this to see what happens during a rollback?
Thanks, Thomas
Private Sub cmdCommit_Click() On Error GoTo Err_cmdCommit_Click
Dim db As DAO.Database Dim rst As DAO.Recordset Dim sql As String Dim AppPath As String
sql = "" sql = sql & "SELECT tblInvMovement.DateMoved, tblInvMovement.LotNumber, tblInvMovement.VPartNumber, " sql = sql & "tblInvMovement.PPartNumber, tblInvMovement.Description, tblInvMovement.Qnty, " sql = sql & "tblInvMovement.From, tblInvMovement.To FROM tblInvMovement;"
AppPath = CurrentDb.Name Set db = OpenDatabase(AppPath)
Set rst = db.OpenRecordset(sql, dbOpenSnapshot)
'-------------------------------------------------------------------- DBEngine.BeginTrans
Do Until rst.EOF db.Execute "INSERT INTO tblInventory ( Event_Date, LotNumber, VPartNumber, PPartNumber, Description, " & _ "" & rst(6) & ", " & rst(7) & ") VALUES ('" & rst(0) & "', '" & rst(1) & "', '" & rst(2) & "', " & _ "'" & rst(3) & "', '" & rst(4) & "', '" & (-rst(5)) & "', '" & rst(5) & "');", dbFailOnError rst.MoveNext Loop
db.Execute "DELETE * FROM tblInvMovement;", dbFailOnError
DBEngine.CommitTrans dbFlushOSCacheWrites
'--------------------------------------------------------------------
rst.Close db.Close
Forms![frmInvMovement]![frmSubInvMovement].Requery
Exit_cmdCommit_Click: Exit Sub
Err_cmdCommit_Click: DBEngine.Rollback MsgBox Err.Description Resume Exit_cmdCommit_Click
End Sub
(I originally posted this answer in reponse to "References to CurrenbDb",
but it seems relevant to what you're doing.)
Been a long time since I've done transactions in Access, but the example in
help these days (which matches my memory of how I was doing it), shows use
of BeginTrans et al off the Workspace object.
The general approach is this:
Public Function testws()
Dim ws As Workspace
Set ws = DBEngine.Workspaces(0)
On Error GoTo Generic_Error
'prep work here
ws.BeginTrans
On Error GoTo RollMeBack
'insert
'update
'delete
'whatever
'finished, commit
ws.CommitTrans
Exit_testws:
Set ws = Nothing
Generic_Error:
MsgBox err.Description
Resume Exit_testws
RollMeBack:
MsgBox err.Description, vbCritical, "Rolling Back After Error"
ws.Rollback
Resume Exit_testws
End Function
You can get much more detailed and even nest transactions several layers
deep, though I can't remember why on earth I thought I needed to do that any
more.
Does this help?
Someone more current on Transactions and Access could explain whether it is
better in any way to perform transactions off of a database object or a
workspace object. The Help says that BeginTrans et al apply to database or
workspace, but I've only ever used them from a workspace object, I believe.
Add "Exit Function" after "Set ws = nothing
How does this look? Just want to be sure I'm doing it correctly!
Thanks,
TD
Private Sub cmdCommit_Click()
On Error GoTo Err_cmdCommit_Click
Dim ws As DAO.Workspace
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim sql As String
Dim AppPath As String
Dim Response As String
Response = MsgBox("The movements will be added to the inventory" &
vbCrLf & " and the handheld device will be cleared! " & vbCrLf &
" Continue? ", vbYesNo +
vbDefaultButton2, " Are you sure? ")
If Response = vbNo Then
GoTo Exit_cmdCommit_Click:
End If
If Response = vbYes Then
Response = MsgBox("Have you printed the movements?" & vbCrLf &
" Continue? ", vbYesNo +
vbDefaultButton2, "WAIT!")
If Response = vbNo Then
GoTo Exit_cmdCommit_Click:
End If
sql = ""
sql = sql & "SELECT tblInvMovement.DateMoved,
tblInvMovement.LotNumber, tblInvMovement.VendorPartNumber, "
sql = sql & "tblInvMovement.ProtecPartNumber,
tblInvMovement.Description, tblInvMovement.Qnty, "
sql = sql & "tblInvMovement.From, tblInvMovement.To FROM
tblInvMovement;"
Set ws = DBEngine.Workspaces(0)
AppPath = CurrentDb.Name
Set db = OpenDatabase(AppPath)
Set rst = db.OpenRecordset(sql, dbOpenSnapshot)
'--------------------------------------------------------------------
ws.BeginTrans
On Error GoTo Err_Rollback
Do Until rst.EOF
db.Execute "INSERT INTO tblInventory ( Event_Date,
LotNumber, VendorPartNumber, ProtecPartNumber, Description, " & _
"" & rst(6) & ", " & rst(7) & ") VALUES ('" &
rst(0) & "', '" & rst(1) & "', '" & rst(2) & "', " & _
"'" & rst(3) & "', '" & rst(4) & "', '" &
(-rst(5)) & "', '" & rst(5) & "');", dbFailOnError
rst.MoveNext
Loop
db.Execute "DELETE * FROM tblInvMovement;", dbFailOnError
ws.CommitTrans dbFlushOSCacheWrites
'--------------------------------------------------------------------
End If
Forms![frmInvMovement]![frmSubInvMovement].Requery
Exit_cmdCommit_Click:
rst.Close
db.Close
ws.Close
Exit Sub
Err_Rollback:
MsgBox Err.Description, vbCritical, "An error occurred, rolling
back changes"
Resume Exit_cmdCommit_Click
Err_cmdCommit_Click:
MsgBox Err.Description
Resume Exit_cmdCommit_Click
End Sub
On 7-Jun-2006, "TD" <dl**@adsi-sc.com> wrote: Err_Rollback: MsgBox Err.Description, vbCritical, "An error occurred, rolling back changes" Resume Exit_cmdCommit_Click
Looks about right. One or two trimups:
Make sure you get a ws.Rollback in there, presumably just below the MsgBox
in the code I quoted above.
Also, watch placement of .Close statements. In this code:
Exit_cmdCommit_Click: rst.Close db.Close ws.Close Exit Sub
I would use
Exit_cmdCommit_Click:
Set rst = nothing
Set db = nothing
Set ws = nothing
Exit Sub
Then, put your .Close statements inline just after the last line of code
that uses the object. It's more important to destroy than to close, so you
destroy regardless of the path you take, but it's sufficient to close on the
normal execution path.
I stand to be corrected, but I believe that you will have no problems from
not closing, while you can definitely have problems from not destroying.
THANKS!!!
Rick Wannall wrote: On 7-Jun-2006, "TD" <dl**@adsi-sc.com> wrote:
Err_Rollback: MsgBox Err.Description, vbCritical, "An error occurred, rolling back changes" Resume Exit_cmdCommit_Click
Looks about right. One or two trimups:
Make sure you get a ws.Rollback in there, presumably just below the MsgBox in the code I quoted above.
Also, watch placement of .Close statements. In this code:
Exit_cmdCommit_Click: rst.Close db.Close ws.Close Exit Sub
I would use
Exit_cmdCommit_Click: Set rst = nothing Set db = nothing Set ws = nothing Exit Sub
Then, put your .Close statements inline just after the last line of code that uses the object. It's more important to destroy than to close, so you destroy regardless of the path you take, but it's sufficient to close on the normal execution path.
I stand to be corrected, but I believe that you will have no problems from not closing, while you can definitely have problems from not destroying.
The best way I know to test Rollbacks, rather than conjuring up error
conditions, is to set a break point in your code at the commit and when
it reaches that point change the commit to a rollback (dont forget to
change it back) and let the code proceed. Then go to the tables and
check they have the same records as before you started.
TD wrote: THANKS!!!
Rick Wannall wrote: On 7-Jun-2006, "TD" <dl**@adsi-sc.com> wrote:
Err_Rollback: MsgBox Err.Description, vbCritical, "An error occurred, rolling back changes" Resume Exit_cmdCommit_Click
Looks about right. One or two trimups:
Make sure you get a ws.Rollback in there, presumably just below the MsgBox in the code I quoted above.
Also, watch placement of .Close statements. In this code:
Exit_cmdCommit_Click: rst.Close db.Close ws.Close Exit Sub
I would use
Exit_cmdCommit_Click: Set rst = nothing Set db = nothing Set ws = nothing Exit Sub
Then, put your .Close statements inline just after the last line of code that uses the object. It's more important to destroy than to close, so you destroy regardless of the path you take, but it's sufficient to close on the normal execution path.
I stand to be corrected, but I believe that you will have no problems from not closing, while you can definitely have problems from not destroying. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gertjan van Heijst |
last post by:
Hi,
I really hope someone can help me because I've already spend 2 days on
this problem and I'm not getting anywhere. I think the problem is that
I don't really understand how text boxes store...
|
by: David Bray |
last post by:
I have an Access database on an ISP's web-space which is accessed
through ASP - all standard stuff.
Can anyone tell me whether ASP's object.context methods for
transactions will work with...
|
by: TD |
last post by:
I am trying to add transactions to my code. The original code worked
fine until I followed an example to setup transactions, now the code
does strange things, but no error messages.
Could...
|
by: Ace Calhoon |
last post by:
Hello,
I have a VBA/Database application which reads files, analyzes them,
updates a database, and then moves them to an archive. I would like to
make this an atomic transaction -- that is, if...
|
by: John Wells |
last post by:
On this page: http://www.compiere.org/technology/independence.html, the
project leader of Compiere (a popular ERP package) states that the move to
Postgres failed because of lack of support of...
|
by: Sridhar |
last post by:
Hi,
I am trying to implement sql transactions. But I am not knowing how to
do that. I created a data access layer which contains methods to
select/insert/update tables in a database. I have also...
|
by: Rami |
last post by:
I have some requirement for an automated payment system. The system has
four machines setup as follows:
1- Two machines have a clustered database.
2- Two machines have a .net business logic...
|
by: Ken |
last post by:
I used the classes (SqlCommand, SqlConnection) of part of System.Data
namespace.
I got an error which mentioned "Could not load file or assembly assembly
'System.Transactions, Version=2.0.0.0,...
|
by: psycho |
last post by:
I am working on an N-tier application using following components:
1. Data Access Layer using DLINQ which consists of Data Context class
and Table Mapping classes.
2. Business Logic Layer....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |