473,661 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Way to test if transactions are working?

TD
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_C lick

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(Ap pPath)

Set rst = db.OpenRecordse t(sql, dbOpenSnapshot)
'--------------------------------------------------------------------
DBEngine.BeginT rans

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.Commit Trans dbFlushOSCacheW rites

'--------------------------------------------------------------------

rst.Close
db.Close

Forms![frmInvMovement]![frmSubInvMoveme nt].Requery
Exit_cmdCommit_ Click:
Exit Sub

Err_cmdCommit_C lick:
DBEngine.Rollba ck
MsgBox Err.Description
Resume Exit_cmdCommit_ Click

End Sub

Jun 6 '06 #1
7 1943
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_C lick

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(Ap pPath)

Set rst = db.OpenRecordse t(sql, dbOpenSnapshot)
'--------------------------------------------------------------------
DBEngine.BeginT rans

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.Commit Trans dbFlushOSCacheW rites

'--------------------------------------------------------------------

rst.Close
db.Close

Forms![frmInvMovement]![frmSubInvMoveme nt].Requery
Exit_cmdCommit _Click:
Exit Sub

Err_cmdCommit_ Click:
DBEngine.Rollba ck
MsgBox Err.Description
Resume Exit_cmdCommit_ Click

End Sub


Jun 6 '06 #2
(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.Worksp aces(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.
Jun 6 '06 #3
Add "Exit Function" after "Set ws = nothing
Jun 6 '06 #4
TD

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_C lick

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 +
vbDefaultButton 2, " 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 +
vbDefaultButton 2, "WAIT!")

If Response = vbNo Then
GoTo Exit_cmdCommit_ Click:
End If

sql = ""
sql = sql & "SELECT tblInvMovement. DateMoved,
tblInvMovement. LotNumber, tblInvMovement. VendorPartNumbe r, "
sql = sql & "tblInvMovement .ProtecPartNumb er,
tblInvMovement. Description, tblInvMovement. Qnty, "
sql = sql & "tblInvMovement .From, tblInvMovement. To FROM
tblInvMovement; "

Set ws = DBEngine.Worksp aces(0)

AppPath = CurrentDb.Name
Set db = OpenDatabase(Ap pPath)

Set rst = db.OpenRecordse t(sql, dbOpenSnapshot)
'--------------------------------------------------------------------
ws.BeginTrans
On Error GoTo Err_Rollback

Do Until rst.EOF
db.Execute "INSERT INTO tblInventory ( Event_Date,
LotNumber, VendorPartNumbe r, ProtecPartNumbe r, 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 dbFlushOSCacheW rites

'--------------------------------------------------------------------

End If

Forms![frmInvMovement]![frmSubInvMoveme nt].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_C lick:
MsgBox Err.Description
Resume Exit_cmdCommit_ Click

End Sub

Jun 7 '06 #5

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.
Jun 7 '06 #6
TD
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.


Jun 7 '06 #7
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.


Jun 7 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
8644
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 'empty' values. I'm trying tot do the following. I have a continous sub form that lists transactions. On the top level form I have some text boxes to let the user specify the transactions between which dates should be listed. To do this I have...
8
5400
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 Access? My ASP reference talks about SQL Server and XA protocol databases but I have no idea as to whether Access is covered by the latter. Thanks
9
2054
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 someone please review my before and after code and tell me the proper way to add transactions to my code? Thanks, TD
3
4650
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 the move to the archive fails, I don't want the database to be updated (and if the database update fails, I don't want the file moved...) The logic I want is as follows: Begin transaction
1
1766
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 embedded transactions...something both Oracle and DB2 support. Can someone explain to me excactly what embedded transactions are and why they're challenging enough to not be included in Postgres or MySQL? I'm guessing it's some sort of...
2
2693
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 created a business logic layer to retrieve/update the data from the data access layer. All of this is working fine if there are no transactions involved. If I need to use transactions, I am not knowing how to do that. Lets say I have a webform...
12
2036
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 component that will handle payment transactions with certain external component (Payment Gateway) My query is regarding transaction handling inside the business logic component. Because this component is running on two machines and these two...
3
1370
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, Culture=neutral, PublicKeyToken=B77A5C561934E089 or noe of its dependencies. What should I do to fix this issue? Strange enough, this application works fine other boxes except one box
3
7082
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. 3. Presentation Layer (normal ASP.NET pages) The problem is that I have to handle database transactions which can span multiple tables. So where should I place the transaction code. I think I should do it in BLL. But how do I control the transaction at...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8754
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.