【发布时间】:2018-03-27 15:16:11
【问题描述】:
我在 VB6 中遇到了一些问题。其中 2 个问题似乎是由我使用 Do Until 循环引起的。
这些问题来自以下代码
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 为 2。循环的第一次迭代工作正常,数据使用事务正确插入。
但是,循环的第二次迭代存在一些问题。
首先是没有设置交易。每个 SQL 语句都是单独执行的,在我的错误陷阱中我调用 dbsPeriod.Rollback 并且有一个错误说
您尝试在未先使用 BeginTrans 的情况下提交或回滚事务
但是,我是在代码的第 3 行设置事务?
第二个问题发生在此之前。我收到以下消息
发生了错误。对象无效或不再设置。
当代码到达rs.MoveFirst 行时。 rs 记录集再次被设置在循环的顶部,所以我不确定为什么它说它不是? rs 没有在这两行之间使用,所以我不确定为什么会出错?肯定有与该查询匹配的数据,因为我已在 Access 本身中运行它。
如果有人能够帮助我解决其中一个问题,我将不胜感激,我已经为此困扰了好几个小时。
【问题讨论】:
标签: loops transactions vb6 iteration recordset