Help with Transactions

B

Barry

Hello:
I am trying to add new records from a Quotes table and associated
QuoteDetails table to a WorkOrder table and associated WorkOrderDetails
table. I thought transactions but, perhaps I am wrong. The WorkOrderDetails
table will not allow the addition of records unless there is an associated
redord in WorkOrders. So, the transaction fails. If I commit the single
record from WorkOrders and there is some error after in the WorkOrderDetails,
how would I then recover from this situation? I hope that I have adequately
explained the problem and I look forward to some discussion about the method
to better implement this type of coding.
Thanks,
Barry
 
T

Tom van Stiphout

On Thu, 4 Mar 2010 13:30:01 -0800, Barry

Show us some code. How are you copying hte data from the Quote* tables
to the WorkOrder* tables?

Also consider if this is really a good idea. I remember making this
mistake once myself. When I came to my senses I simply added a Status
field that would turn the Quote status to a Workorder status.

-Tom.
Microsoft Access MVP
 
B

Barry

Tom:
I understand however, it has been requested that the original quote be
available after having been changed to a WorkOrder.

It's more about the methodology and how to get around the problem of not
being able to add a record to the detail side without the one side having a
record. Although, I'm sure that my code could use help as well.

Currently I am using recordsets as opposed to INSERT INTO but that can be
changed. An example is below:

On Error GoTo errorTransaction

Dim myWrk As DAO.Workspace,db as DAO.database
Dim SQLQuote As String, SQLQuotePart As String, SQLQuoteLabor As String
Dim rstQuote As DAO.Recordset, rstQuotePart As DAO.Recordset,
rstQuoteLabor As DAO.Recordset
Dim rstWO As DAO.Recordset, rstWOPart As DAO.Recordset, rstWOLabor As
DAO.Recordset
Dim frm As Form

Set frm = Forms!frmQuotes
Set myWrk = DBEngine.Workspaces(0)
Set db = CurrentDb

myWrk.BeginTrans

SQLQuote = "Select * " & vbCrLf & _
"FROM tblQuotes " & vbCrLf & _
"WHERE " & "[tblQuotes].[QuoteID] = " & Forms!frmQuotes![QuoteID]

Set rstQuote = db.OpenRecordset(SQLQuote, dbReadOnly)
Set rstWO = db.OpenRecordset("tblWorkOrders", dbOpenDynaset)

If Not rstQuote.NoMatch Then
If rstQuote!ChangeToWorkOrder = False Then
rstWO.AddNew
rstWO!CustomerID = rstQuote!CustomerID
rstWO!ProblemDescription = rstQuote!ProblemDescription
rstWO!CarID = rstQuote!CarID
rstWO!SalesTaxRate = rstQuote!SalesTaxRate
rstWO!QuoteID = rstQuote!QuoteID
rstWO.Update
End If
Else
GoTo errorTransaction
End If

SQLQuotePart = "Select * " & vbCrLf & _
"FROM tblQuoteParts " & vbCrLf & _
"WHERE " & "[tblQuoteParts.QuoteID] = " & frm!QuoteID

Set rstQuotePart = db.OpenRecordset(SQLQuotePart, dbReadOnly)
Set rstWOPart = db.OpenRecordset("tblWorkOrderParts", dbOpenDynaset)

If Not rstQuotePart.NoMatch Then
If rstQuote!ChangeToWorkOrder = False Then
Do Until rstQuotePart.EOF
rstWOPart.AddNew
rstWOPart!PartCategoryID = rstQuotePart!CategoryID
rstWOPart!PartID = rstQuotePart!PartID
rstWOPart!Qty = rstQuotePart!Qty
rstWOPart!Price = rstQuotePart!Price
rstWOPart.Update
Loop
Else
GoTo errorTransaction
End If
End If

myWrk.CommitTrans

errorTransaction:
myWrk.Rollback
MsgBox Err.Number & " " & Err.Description
MsgBox "Transaction was not completed successfully", vbCritical, "Problem
with batch run"
GoTo exitTransaction

exitTransaction:
db.Close
myWrk.Close


Well, it's late here and I will check for responses in the AM. Thanks so
much.
Barry
 
T

Tom van Stiphout

On Thu, 4 Mar 2010 22:24:01 -0800, Barry

That's a good reason for having two tables.

In a good database design with enforced relations, you simply CANNOT
have Child records before you have Parent records. Same in biology :)

It appears you're doing that correctly: first copy parent record to
parent record, then copy details records to details records. Something
else must be afoot - hard to determine without working with the actual
db.

-Tom.
Microsoft Access MVP


Tom:
I understand however, it has been requested that the original quote be
available after having been changed to a WorkOrder.

It's more about the methodology and how to get around the problem of not
being able to add a record to the detail side without the one side having a
record. Although, I'm sure that my code could use help as well.

Currently I am using recordsets as opposed to INSERT INTO but that can be
changed. An example is below:

On Error GoTo errorTransaction

Dim myWrk As DAO.Workspace,db as DAO.database
Dim SQLQuote As String, SQLQuotePart As String, SQLQuoteLabor As String
Dim rstQuote As DAO.Recordset, rstQuotePart As DAO.Recordset,
rstQuoteLabor As DAO.Recordset
Dim rstWO As DAO.Recordset, rstWOPart As DAO.Recordset, rstWOLabor As
DAO.Recordset
Dim frm As Form

Set frm = Forms!frmQuotes
Set myWrk = DBEngine.Workspaces(0)
Set db = CurrentDb

myWrk.BeginTrans

SQLQuote = "Select * " & vbCrLf & _
"FROM tblQuotes " & vbCrLf & _
"WHERE " & "[tblQuotes].[QuoteID] = " & Forms!frmQuotes![QuoteID]

Set rstQuote = db.OpenRecordset(SQLQuote, dbReadOnly)
Set rstWO = db.OpenRecordset("tblWorkOrders", dbOpenDynaset)

If Not rstQuote.NoMatch Then
If rstQuote!ChangeToWorkOrder = False Then
rstWO.AddNew
rstWO!CustomerID = rstQuote!CustomerID
rstWO!ProblemDescription = rstQuote!ProblemDescription
rstWO!CarID = rstQuote!CarID
rstWO!SalesTaxRate = rstQuote!SalesTaxRate
rstWO!QuoteID = rstQuote!QuoteID
rstWO.Update
End If
Else
GoTo errorTransaction
End If

SQLQuotePart = "Select * " & vbCrLf & _
"FROM tblQuoteParts " & vbCrLf & _
"WHERE " & "[tblQuoteParts.QuoteID] = " & frm!QuoteID

Set rstQuotePart = db.OpenRecordset(SQLQuotePart, dbReadOnly)
Set rstWOPart = db.OpenRecordset("tblWorkOrderParts", dbOpenDynaset)

If Not rstQuotePart.NoMatch Then
If rstQuote!ChangeToWorkOrder = False Then
Do Until rstQuotePart.EOF
rstWOPart.AddNew
rstWOPart!PartCategoryID = rstQuotePart!CategoryID
rstWOPart!PartID = rstQuotePart!PartID
rstWOPart!Qty = rstQuotePart!Qty
rstWOPart!Price = rstQuotePart!Price
rstWOPart.Update
Loop
Else
GoTo errorTransaction
End If
End If

myWrk.CommitTrans

errorTransaction:
myWrk.Rollback
MsgBox Err.Number & " " & Err.Description
MsgBox "Transaction was not completed successfully", vbCritical, "Problem
with batch run"
GoTo exitTransaction

exitTransaction:
db.Close
myWrk.Close


Well, it's late here and I will check for responses in the AM. Thanks so
much.
Barry

Tom van Stiphout said:
On Thu, 4 Mar 2010 13:30:01 -0800, Barry

Show us some code. How are you copying hte data from the Quote* tables
to the WorkOrder* tables?

Also consider if this is really a good idea. I remember making this
mistake once myself. When I came to my senses I simply added a Status
field that would turn the Quote status to a Workorder status.

-Tom.
Microsoft Access MVP



.
 
B

Barry

Tom:
Thanks for getting back. I understand about the way the records are saved,
and I can commit the record on the one side and then add the details on the
many side. I am just concerned that if there is an error in the details side
that I'll be stuck with a record without details and I'm not sure how to
clean up the mess. Or, perhaps there's a better way to look at this whole
situation. I agree it would be easier to just change the status to WorkOrder
but, that's not an option. Anybody with a gem of an idea or example is
welcome to chime in with their opinion.
Thanks,
Barry

Tom van Stiphout said:
On Thu, 4 Mar 2010 22:24:01 -0800, Barry

That's a good reason for having two tables.

In a good database design with enforced relations, you simply CANNOT
have Child records before you have Parent records. Same in biology :)

It appears you're doing that correctly: first copy parent record to
parent record, then copy details records to details records. Something
else must be afoot - hard to determine without working with the actual
db.

-Tom.
Microsoft Access MVP


Tom:
I understand however, it has been requested that the original quote be
available after having been changed to a WorkOrder.

It's more about the methodology and how to get around the problem of not
being able to add a record to the detail side without the one side having a
record. Although, I'm sure that my code could use help as well.

Currently I am using recordsets as opposed to INSERT INTO but that can be
changed. An example is below:

On Error GoTo errorTransaction

Dim myWrk As DAO.Workspace,db as DAO.database
Dim SQLQuote As String, SQLQuotePart As String, SQLQuoteLabor As String
Dim rstQuote As DAO.Recordset, rstQuotePart As DAO.Recordset,
rstQuoteLabor As DAO.Recordset
Dim rstWO As DAO.Recordset, rstWOPart As DAO.Recordset, rstWOLabor As
DAO.Recordset
Dim frm As Form

Set frm = Forms!frmQuotes
Set myWrk = DBEngine.Workspaces(0)
Set db = CurrentDb

myWrk.BeginTrans

SQLQuote = "Select * " & vbCrLf & _
"FROM tblQuotes " & vbCrLf & _
"WHERE " & "[tblQuotes].[QuoteID] = " & Forms!frmQuotes![QuoteID]

Set rstQuote = db.OpenRecordset(SQLQuote, dbReadOnly)
Set rstWO = db.OpenRecordset("tblWorkOrders", dbOpenDynaset)

If Not rstQuote.NoMatch Then
If rstQuote!ChangeToWorkOrder = False Then
rstWO.AddNew
rstWO!CustomerID = rstQuote!CustomerID
rstWO!ProblemDescription = rstQuote!ProblemDescription
rstWO!CarID = rstQuote!CarID
rstWO!SalesTaxRate = rstQuote!SalesTaxRate
rstWO!QuoteID = rstQuote!QuoteID
rstWO.Update
End If
Else
GoTo errorTransaction
End If

SQLQuotePart = "Select * " & vbCrLf & _
"FROM tblQuoteParts " & vbCrLf & _
"WHERE " & "[tblQuoteParts.QuoteID] = " & frm!QuoteID

Set rstQuotePart = db.OpenRecordset(SQLQuotePart, dbReadOnly)
Set rstWOPart = db.OpenRecordset("tblWorkOrderParts", dbOpenDynaset)

If Not rstQuotePart.NoMatch Then
If rstQuote!ChangeToWorkOrder = False Then
Do Until rstQuotePart.EOF
rstWOPart.AddNew
rstWOPart!PartCategoryID = rstQuotePart!CategoryID
rstWOPart!PartID = rstQuotePart!PartID
rstWOPart!Qty = rstQuotePart!Qty
rstWOPart!Price = rstQuotePart!Price
rstWOPart.Update
Loop
Else
GoTo errorTransaction
End If
End If

myWrk.CommitTrans

errorTransaction:
myWrk.Rollback
MsgBox Err.Number & " " & Err.Description
MsgBox "Transaction was not completed successfully", vbCritical, "Problem
with batch run"
GoTo exitTransaction

exitTransaction:
db.Close
myWrk.Close


Well, it's late here and I will check for responses in the AM. Thanks so
much.
Barry

Tom van Stiphout said:
On Thu, 4 Mar 2010 13:30:01 -0800, Barry

Show us some code. How are you copying hte data from the Quote* tables
to the WorkOrder* tables?

Also consider if this is really a good idea. I remember making this
mistake once myself. When I came to my senses I simply added a Status
field that would turn the Quote status to a Workorder status.

-Tom.
Microsoft Access MVP



Hello:
I am trying to add new records from a Quotes table and associated
QuoteDetails table to a WorkOrder table and associated WorkOrderDetails
table. I thought transactions but, perhaps I am wrong. The WorkOrderDetails
table will not allow the addition of records unless there is an associated
redord in WorkOrders. So, the transaction fails. If I commit the single
record from WorkOrders and there is some error after in the WorkOrderDetails,
how would I then recover from this situation? I hope that I have adequately
explained the problem and I look forward to some discussion about the method
to better implement this type of coding.
Thanks,
Barry
.
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top