直到VB6中的问题

时间:2018-03-27 15:16:12

标签: loops transactions vb6 iteration recordset

我在VB6中遇到过一些问题。其中2个问题似乎是由我使用Dim strAccount As String On Error GoTo PayBalance_Err If dtaEmployees.Recordset.RecordCount = 0 Then Exit Sub End If If MsgBox("Are you sure you would like to pay all outstanding balances? This change cannot be undone.", vbYesNo, "Confirm") = vbNo Then Exit Sub Else Screen.MousePointer = vbHourglass dtaEmployees.Recordset.MoveFirst If dtaEmployees.Recordset.RecordCount > 0 Then Do Until dtaEmployees.Recordset.EOF dbsPeriod.BeginTrans strAccount = dtaEmployees.Recordset.Fields(1) If GotBatch = False Then Batch_Number = Get_Batch_Number() Batch_Line = 0 GotBatch = True End If Batch_Line = Batch_Line + 1 Dim rs As DAO.Recordset sql = "SELECT * FROM [Ledger Transactions] WHERE [Account_Code] = '" & strAccount & "' AND [Outstanding] > 0" Set rs = dbsPeriod.OpenRecordset(sql, dbOpenDynaset) sql = "UPDATE [Ledger] SET [Sales_Balance] = 0, [Date_Last_Cash_S] = " & Format(Now, "dd/MM/yyyy") & " WHERE Account_Code = '" & strAccount & "'" dbsPeriod.Execute sql sql = "UPDATE [Ledger Transactions] SET [Outstanding] = 0 WHERE [Account_Code] = '" & strAccount & "'" dbsPeriod.Execute sql Dim GoodsValue, VAT, TotalValue As Currency GoodsValue = dtaEmployees.Recordset.Fields(3) / 1.2 VAT = GoodsValue / 5 TotalValue = dtaEmployees.Recordset.Fields(3) Dim pPeriod, pYear As String pPeriod = Mid(DB_Period_Name$, Len(DB_Period_Name$) - 7, 2) pYear = Mid(DB_Period_Name$, Len(DB_Period_Name$) - 5, 2) sql = "INSERT INTO [Ledger Transactions]([Batch_Number], [Batch_Line], [Account_Code], [Reference], [Description], [Goods_Value], " & _ "[VAT_Value], [Total_Value], [Outstanding], [Document_Date], [Period], [Year], [Type], [Sort_Order]) VALUES(" & Batch_Number & ", " & _ Batch_Line & ", '" & strAccount & "', 'PAYROLL', '" & txtDescription.Text & "', " & GoodsValue & ", " & VAT & ", " & TotalValue & _ ", 0, " & Format(Now, "dd/MM/yyyy") & ", '" & pPeriod & "', '" & pYear & "', 'P', 1)" dbsPeriod.Execute sql rs.MoveFirst Dim TotalPayment As Currency TotalPayment = 0 Do Until rs.EOF TotalPayment = TotalPayment + rs.Fields(10) rs.MoveNext Loop rs.MoveFirst Do Until rs.EOF GoodsValue = rs.Fields(8) VAT = rs.Fields(9) sql = "INSERT INTO [Allocations]([Payment], [Account_Code], [Sales_Or_Purchase], [Payment_Batch_Number], [Payment_Batch_Line], " & _ "[Payment_Reference], [Payment_Date], [Payment_Value], [Invoice_Batch_Number], [Invoice_Batch_Line], [Invoice_Type], [Invoice_Date], " & _ "[Invoice_Goods], [Invoice_VAT], [Date_Allocated], [Who_Allocated], [Goods_Allocated], [Discount], [VAT_Allocated]) " & _ "VALUES(True, '" & strAccount & "', False, " & Batch_Number & ", " & Batch_Line & ", 'PAYROLL', " & _ Format(Now, "dd/MM/yyyy") & ", " & rs.Fields(10) & ", " & Batch_Number & ", " & Batch_Line & ", 'I', " & _ Format(Now, "dd/MM/yyyy") & ", " & GoodsValue & ", " & VAT & ", " & Format(Now, "dd/MM/yyyy") & ", '" & User_ID$ & "', " & _ GoodsValue & ", 0, " & VAT & ")" dbsPeriod.Execute sql sql = "INSERT INTO [Audit Header]([Batch_Number], [Batch_Line], [Account_Code], [Sales_Or_Purchase], [Type], [Reference], " & _ "[Description], [Goods_Value], [VAT_Value], [Total_Value], [Document_Date], [VAT_Percentage_1], [Goods_1], [VAT_1], " & _ "[Post_Date], [Who_Posted], [SOPS_Provisional_Due]) " & _ "VALUES(" & Batch_Number & ", " & Batch_Line & ", '" & strAccount & "', False, 'I', 'PAYROLL', '" & txtDescription.Text & "', " & _ GoodsValue & ", " & VAT & ", " & rs.Fields(10) & ", " & Format(Now, "dd/MM/yyyy") & ", 20, " & GoodsValue & ", " & VAT & _ ", " & Format(Now, "dd/MM/yyyy") & ", '" & User_ID$ & "', " & Format(Now, "dd/MM/yyyy") & ")" dbsPeriod.Execute sql rs.MoveNext Loop rs.MoveFirst sql = "INSERT INTO [Allocations]([Payment], [Account_Code], [Sales_Or_Purchase], [Payment_Batch_Number], [Payment_Batch_Line], " & _ "[Payment_Reference], [Payment_Date], [Payment_Value], [Invoice_Batch_Number], [Invoice_Batch_Line], [Invoice_Type], [Invoice_Date], " & _ "[Invoice_Goods], [Invoice_VAT], [Date_Allocated], [Who_Allocated], [Goods_Allocated], [Discount], [VAT_Allocated]) " & _ "VALUES(True, '" & strAccount & "', False, " & Batch_Number & ", " & Batch_Line & ", 'PAYROLL', " & _ Format(Now, "dd/MM/yyyy") & ", " & rs.Fields(10) & ", " & Batch_Number & ", " & Batch_Line & ", 'P', " & _ Format(Now, "dd/MM/yyyy") & ", " & GoodsValue & ", " & VAT & ", " & Format(Now, "dd/MM/yyyy") & ", '" & User_ID$ & "', " & _ GoodsValue & ", 0, " & VAT & ")" dbsPeriod.Execute sql sql = "INSERT INTO [Audit Header]([Batch_Number], [Batch_Line], [Account_Code], [Sales_Or_Purchase], [Type], [Reference], " & _ "[Goods_Value], [Total_Value], [Document_Date], " & _ "[Post_Date], [Who_Posted], [SOPS_Provisional_Due]) VALUES(" & Batch_Number & ", " & Batch_Line & ", '" & strAccount & _ "', False, 'P', 'Payment', " & rs.Fields(10) & ", " & rs.Fields(10) & ", " & _ Format(Now, "dd/MM/yyyy") & ", " & Format(Now, "dd/MM/yyyy") & ", '" & User_ID$ & "', " & _ Format(Now, "dd/MM/yyyy") & ")" dbsPeriod.Execute sql sql = "INSERT INTO [Audit Lines]([Batch_Number], [Batch_Line], [Batch_Line_Item], [Account_Code], [Sales_Or_Purchase], " & _ "[Nominal_Code], [Reference], [Description], [Goods_Value], [VAT_Value])" & _ " VALUES(" & Batch_Number & ", " & Batch_Line & ", 1, '" & strAccount & "', False, '" & Cmb_Code.Text & "'" & _ ", 'PAYROLL', '" & txtDescription.Text & "', " & GoodsValue & ", " & VAT & ")" dbsPeriod.Execute sql dbsPeriod.CommitTrans dtaEmployees.Recordset.MoveNext Loop End If End If Screen.MousePointer = vbDefault MsgBox "Payment(s) complete!", vbOKOnly, "Success" dtaEmployees.Refresh grdEmployees.Redraw = True Exit Sub PayBalance_Err: Screen.MousePointer = vbDefault MsgBox "An error has occurred. " & err.Description, vbCritical, "Error" dbsPeriod.Rollback 循环引起的。

这些问题来自以下代码

dtaEmployees.Recordset.RecordCount

在我的测试数据中,dbsPeriod.Rollback为2.循环的第一次迭代正常,数据使用事务正确插入。

但是,循环的第二次迭代存在一些问题。

第一个是交易未被设定。每个SQL语句都是单独执行的,在我的错误陷阱中,我调用rs.MoveFirst并且出现错误

  

您尝试在未先使用BeginTrans

的情况下提交或回滚事务

但是,我在代码的第3行设置了交易?

第二个问题发生在此之前。我收到以下消息

  

发生了错误。对象无效或不再设置。

当代码到达行rs时。 rs记录集再次设置在循环的顶部,所以我不确定为什么它说不是?这两行之间没有使用{{1}},所以我不确定它为什么会出错?肯定有数据匹配该查询,因为我已在Access本身运行它。

如果有人能够帮我解决其中一个我非常感激的问题,我已经被困了好几个小时。

2 个答案:

答案 0 :(得分:0)

对于第一个错误,事务在循环内开始并提交。而不是:

If dtaEmployees.Recordset.RecordCount > 0 Then    
    Do Until dtaEmployees.Recordset.EOF
        dbsPeriod.BeginTrans

        '... 

        dbsPeriod.CommitTrans
        dtaEmployees.RecordSet.MoveNext
    Loop
End If
你可能想要这个:

If dtaEmployees.Recordset.RecordCount > 0 Then
    dbsPeriod.BeginTrans
    Do Until dtaEmployees.Recordset.EOF

        '... 

        dtaEmployees.RecordSet.MoveNext
    Loop
    dbsPeriod.CommitTrans
End If

除此之外,问题中提到的ROLLBACK代码不包含在样本中,所以我可以建议。

此外,我强烈怀疑整个事情可以简化为单次调用数据库,完全不需要循环。

对于第二个问题,跳过的代码很重要。任何东西都会破坏记录集,或者集合首先失败。如果您正在使用On Error Resume Next,则应停止。它隐藏了你的错误,使调试变得困难。

最后,构建SQL字符串的方式很容易受到注入攻击。我希望您非常确定您的数据源,以这种糟糕的方式构建SQL字符串。了解如何使用ADO.Parameter对象。

答案 1 :(得分:0)

虽然它无法解决您的交易问题(您确定只打开了一个数据库吗?),但Handlebars问题可能会通过在每个数据库末尾添加Handlerbars.compile()来解决循环。