I've set up a transaction so that if anything happens, it will return to the state prior to the start of the transaction. What I'm expecting, is if there is an error (or the user hits the ESC key) the transaction will rollback.
When I've tested this, the rollback doesn't work. For example, the first SQLstmt below will run and when it starts the second, it may error. I would expect that the rollback would make the INSERT into statement reversed so that it is like nothing happened? All that happens now is that it errors, and follows the steps to exit sub.
Is there something I'm missing in my code? Do rollbacks not work if you've completed a query?
Help...
Thanks,
Carie![[sunshine] [sunshine] [sunshine]](/data/assets/smilies/sunshine.gif)
*************************
Private Sub cmdSettlePOs1_Click()
On Error GoTo SettlePOs1_Err
'Set up Transaction
Dim wrk As DAO.Workspace
Dim fInTrans As Boolean
fInTrans = False
Set wrk = DAO.Workspaces(0)
Dim intSettledCount As Long
Dim intLoopCount As Long
Dim intCountofBU As Long
Dim SQLstmt As String
DoCmd.OpenForm "20000_ProcessRunning_frm", acNormal
'Process 2021 Query Set
wrk.BeginTrans
fInTrans = True
intSettledCount = 1 'SetCounters
intLoopCount = 0
intCountofBU = 0
DoCmd.SetWarnings False
Do While intSettledCount <> 0
SQLstmt = "INSERT INTO [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl] ( [CountOfBU], [BU], [PO Num], [Trans Key], [Inv Num], [Inv Ln], [SumOfTrans Amt] ) " & _
"SELECT Count([100_MAIN_tbl].[BU]) AS [CountOfBU], [100_MAIN_tbl].[BU], [100_MAIN_tbl].[PO Num], [100_MAIN_tbl].[Trans Key], [100_MAIN_tbl].[Inv Num], " & _
"[100_MAIN_tbl].[Inv Ln], Sum([100_MAIN_tbl].[Trans Amt]) AS [SumOfTrans Amt] FROM [100_MAIN_tbl]" & _
"GROUP BY [100_MAIN_tbl].[BU], [100_MAIN_tbl].[PO Num], [100_MAIN_tbl].[Trans Key], [100_MAIN_tbl].[Inv Num], [100_MAIN_tbl].[Inv Ln], [100_MAIN_tbl].[Status] " & _
"HAVING ((([100_MAIN_tbl].[BU]) Is Not Null) AND (([100_MAIN_tbl].[PO Num]) Is Not Null) AND (([100_MAIN_tbl].[Trans Key]) Is Not Null) AND (([100_MAIN_tbl].[Inv Num]) Is Not Null) " & _
"AND (([100_MAIN_tbl].[Inv Ln]) Is Not Null) AND ((Sum([100_MAIN_tbl].[Trans Amt]))=0) AND (([100_MAIN_tbl].[Status]) Is Null))"
DoCmd.RunSQL SQLstmt
If IsNull(DSum("CountOfBU", "2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl"
) Then
intSettledCount = 0
Else: intCountofBU = DSum("CountOfBU", "2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl"
intSettledCount = intCountofBU
End If
If intSettledCount = 0 Then Exit Do
SQLstmt = "UPDATE [100_MAIN_tbl] INNER JOIN [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl] " & _
"ON ([100_MAIN_tbl].[Trans Key] = [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl].[Trans Key]) " & _
"AND ([100_MAIN_tbl].[Inv Ln] = [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl].[Inv Ln]) " & _
"AND ([100_MAIN_tbl].[Inv Num] = [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl].[Inv Num]) " & _
"AND ([100_MAIN_tbl].BU = [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl].BU) " & _
"AND ([100_MAIN_tbl].[PO Num] = [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl].[PO Num]) " & _
"SET [100_MAIN_tbl].Status = 'Settled', [100_MAIN_tbl].[Matched Primary Key] = Now() & ' - BU, PO, TransKey, InvNum, & InvLn Net to 0 '" & _
"WHERE ((([100_MAIN_tbl].Status) Is Null) AND (([100_MAIN_tbl].BU) Is Not Null))"
DoCmd.RunSQL SQLstmt
DoCmd.RunSQL "DELETE * From [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl]"
intLoopCount = intLoopCount + 1
'Check to make sure transactions matched properly
Me.TotalNETSettled = DSum("Net", "401_Check_Settled_100_Table_Totals"
If IsNull(Me.TotalNETSettled) Then
Me.TotalNETSettled = 0
End If
If Me.TotalNETSettled <> 0 Then
wrk.Rollback
MsgBox "The transactions matched did not equal zero. Please verify that there are no NULL values in the Trans Amt field." & vbCrLf & vbCrLf & _
"The matching has been cancelled.", vbOKOnly, "Matching Error"
Forms![2002_SettlePOs]![401_Check_Settled_100_Table_Totals_sfm].Requery
Me.TotalRecordsSettled = DSum("CountofBU", "401_Check_Settled_100_Table_Totals"
Me.TotalNETSettled = DSum("Net", "401_Check_Settled_100_Table_Totals"
If IsNull(Me.TotalNETSettled) Then
Me.TotalNETSettled = 0
End If
DoCmd.Close acForm, "20000_ProcessRunning_frm"
Exit Sub
End If
Loop
wrk.CommitTrans
fInTrans = False
DoCmd.SetWarnings True
'Requery Subform
Forms![2002_SettlePOs]![401_Check_Settled_100_Table_Totals_sfm].Requery
Me.TotalRecordsSettled = DSum("CountofBU", "401_Check_Settled_100_Table_Totals"
Me.TotalNETSettled = DSum("Net", "401_Check_Settled_100_Table_Totals"
If IsNull(Me.TotalNETSettled) Then
Me.TotalNETSettled = 0
End If
MsgBox "Process Complete!" & vbCrLf & vbCrLf & _
"Depending on the number of records processed, you may want to" & vbCrLf & _
"'Move Settled Transactions' and Compact & Repair the database.", vbOKOnly, ""
DoCmd.Close acForm, "20000_ProcessRunning_frm"
Exit Sub
SettlePOs1_Exit:
DoCmd.SetWarnings True
DoCmd.Close acForm, "20000_ProcessRunning_frm"
Exit Sub
SettlePOs1_Err:
If fInTrans Then
wrk.Rollback
End If
MsgBox Err.Number & ": " & Err.Source & Err.Description
Resume SettlePOs1_Exit
End Sub
***********************************
When I've tested this, the rollback doesn't work. For example, the first SQLstmt below will run and when it starts the second, it may error. I would expect that the rollback would make the INSERT into statement reversed so that it is like nothing happened? All that happens now is that it errors, and follows the steps to exit sub.
Is there something I'm missing in my code? Do rollbacks not work if you've completed a query?
Help...
Thanks,
Carie
![[sunshine] [sunshine] [sunshine]](/data/assets/smilies/sunshine.gif)
*************************
Private Sub cmdSettlePOs1_Click()
On Error GoTo SettlePOs1_Err
'Set up Transaction
Dim wrk As DAO.Workspace
Dim fInTrans As Boolean
fInTrans = False
Set wrk = DAO.Workspaces(0)
Dim intSettledCount As Long
Dim intLoopCount As Long
Dim intCountofBU As Long
Dim SQLstmt As String
DoCmd.OpenForm "20000_ProcessRunning_frm", acNormal
'Process 2021 Query Set
wrk.BeginTrans
fInTrans = True
intSettledCount = 1 'SetCounters
intLoopCount = 0
intCountofBU = 0
DoCmd.SetWarnings False
Do While intSettledCount <> 0
SQLstmt = "INSERT INTO [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl] ( [CountOfBU], [BU], [PO Num], [Trans Key], [Inv Num], [Inv Ln], [SumOfTrans Amt] ) " & _
"SELECT Count([100_MAIN_tbl].[BU]) AS [CountOfBU], [100_MAIN_tbl].[BU], [100_MAIN_tbl].[PO Num], [100_MAIN_tbl].[Trans Key], [100_MAIN_tbl].[Inv Num], " & _
"[100_MAIN_tbl].[Inv Ln], Sum([100_MAIN_tbl].[Trans Amt]) AS [SumOfTrans Amt] FROM [100_MAIN_tbl]" & _
"GROUP BY [100_MAIN_tbl].[BU], [100_MAIN_tbl].[PO Num], [100_MAIN_tbl].[Trans Key], [100_MAIN_tbl].[Inv Num], [100_MAIN_tbl].[Inv Ln], [100_MAIN_tbl].[Status] " & _
"HAVING ((([100_MAIN_tbl].[BU]) Is Not Null) AND (([100_MAIN_tbl].[PO Num]) Is Not Null) AND (([100_MAIN_tbl].[Trans Key]) Is Not Null) AND (([100_MAIN_tbl].[Inv Num]) Is Not Null) " & _
"AND (([100_MAIN_tbl].[Inv Ln]) Is Not Null) AND ((Sum([100_MAIN_tbl].[Trans Amt]))=0) AND (([100_MAIN_tbl].[Status]) Is Null))"
DoCmd.RunSQL SQLstmt
If IsNull(DSum("CountOfBU", "2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl"
intSettledCount = 0
Else: intCountofBU = DSum("CountOfBU", "2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl"
intSettledCount = intCountofBU
End If
If intSettledCount = 0 Then Exit Do
SQLstmt = "UPDATE [100_MAIN_tbl] INNER JOIN [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl] " & _
"ON ([100_MAIN_tbl].[Trans Key] = [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl].[Trans Key]) " & _
"AND ([100_MAIN_tbl].[Inv Ln] = [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl].[Inv Ln]) " & _
"AND ([100_MAIN_tbl].[Inv Num] = [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl].[Inv Num]) " & _
"AND ([100_MAIN_tbl].BU = [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl].BU) " & _
"AND ([100_MAIN_tbl].[PO Num] = [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl].[PO Num]) " & _
"SET [100_MAIN_tbl].Status = 'Settled', [100_MAIN_tbl].[Matched Primary Key] = Now() & ' - BU, PO, TransKey, InvNum, & InvLn Net to 0 '" & _
"WHERE ((([100_MAIN_tbl].Status) Is Null) AND (([100_MAIN_tbl].BU) Is Not Null))"
DoCmd.RunSQL SQLstmt
DoCmd.RunSQL "DELETE * From [2021_BU_PO_TransKey_InvNum_InvLn_Net0_tbl]"
intLoopCount = intLoopCount + 1
'Check to make sure transactions matched properly
Me.TotalNETSettled = DSum("Net", "401_Check_Settled_100_Table_Totals"
If IsNull(Me.TotalNETSettled) Then
Me.TotalNETSettled = 0
End If
If Me.TotalNETSettled <> 0 Then
wrk.Rollback
MsgBox "The transactions matched did not equal zero. Please verify that there are no NULL values in the Trans Amt field." & vbCrLf & vbCrLf & _
"The matching has been cancelled.", vbOKOnly, "Matching Error"
Forms![2002_SettlePOs]![401_Check_Settled_100_Table_Totals_sfm].Requery
Me.TotalRecordsSettled = DSum("CountofBU", "401_Check_Settled_100_Table_Totals"
Me.TotalNETSettled = DSum("Net", "401_Check_Settled_100_Table_Totals"
If IsNull(Me.TotalNETSettled) Then
Me.TotalNETSettled = 0
End If
DoCmd.Close acForm, "20000_ProcessRunning_frm"
Exit Sub
End If
Loop
wrk.CommitTrans
fInTrans = False
DoCmd.SetWarnings True
'Requery Subform
Forms![2002_SettlePOs]![401_Check_Settled_100_Table_Totals_sfm].Requery
Me.TotalRecordsSettled = DSum("CountofBU", "401_Check_Settled_100_Table_Totals"
Me.TotalNETSettled = DSum("Net", "401_Check_Settled_100_Table_Totals"
If IsNull(Me.TotalNETSettled) Then
Me.TotalNETSettled = 0
End If
MsgBox "Process Complete!" & vbCrLf & vbCrLf & _
"Depending on the number of records processed, you may want to" & vbCrLf & _
"'Move Settled Transactions' and Compact & Repair the database.", vbOKOnly, ""
DoCmd.Close acForm, "20000_ProcessRunning_frm"
Exit Sub
SettlePOs1_Exit:
DoCmd.SetWarnings True
DoCmd.Close acForm, "20000_ProcessRunning_frm"
Exit Sub
SettlePOs1_Err:
If fInTrans Then
wrk.Rollback
End If
MsgBox Err.Number & ": " & Err.Source & Err.Description
Resume SettlePOs1_Exit
End Sub
***********************************