У меня есть этот код, который делает цикл по счетам и вставляет все счета в TblQdHeaderF
(этот код работает правильно).
А внутри первого цикла у меня есть еще один цикл для вставки InvoicesDetails
в TblQdBodyF
, но этот код работает в первом операторе только в первом цикле.
Он зацикливается как обычно, но работает только первый цикл и первый оператор и вставляет одну запись в таблицу.
Это мой код:
CurrentDb.Execute "Delete * from TblQdHeaderF"
CurrentDb.Execute "Delete * from TblQdBodyF"
Dim x As Integer
Dim i As Integer
x = 1
Dim Frs As DAO.Recordset
Set Frs = Xdb.OpenRecordset("Select * from TblFatoraHead")
Frs.MoveFirst
Do While Not Frs.EOF
Dim Q1 As Date
Dim Q2 As String
Dim Q3 As Double
Q3 = DLookup("CurrPrice", "TblCurrencies", "[CurrID]=" & Frs.Fields("CurrId").Value & "")
Q1 = Frs.Fields("FatoraDate").Value
Q2 = "record" & Frs.Fields("FatoraId1").Value
CurrentDb.Execute "Insert Into TblQdHeaderF ([QdId], [QdDate], [QdDetails]) values" _
& " (" & x & ", #" & Q1 & "#, '" & Q2 & "')"
x = x + 1
Dim x1 As Integer
Dim i1 As Integer
x1 = 1
Dim Frs1 As DAO.Recordset
Set Frs1 = Xdb.OpenRecordset("Select * from TblFatoraDetails Where [FatoraId]= " & Frs.Fields("FatoraId1").Value & "")
Frs1.MoveFirst
Dim FTotal As Variant
Dim HTotal As Variant
FTotal = 0
HTotal = 0
Do While Not Frs1.EOF
FTotal = FTotal + (Frs1.Fields("Price").Value * Frs1.Fields("Qn").Value)
HTotal = HTotal + (Frs1.Fields("Hasm").Value)
Frs1.MoveNext
Loop
HTotal = HTotal + Frs.Fields("FHasm").Value
If HTotal = 0 Then
CurrentDb.Execute "Insert Into TblQdBodyF ([QdBodyId], [QdId], [Madeen], [Daen], [AccId], [CurrId]" _
& " ,[CurrValue], [MadeenCurrValue], [DaenCurrValue],[Details]) values" _
& " (" & x1 & ", " & x & ", " & FTotal & ", 0, " & Frs.Fields("AccId").Value & "," _
& "" & Frs.Fields("CurrId").Value & ", " & Q3 & ", " & FTotal * Q3 & ", 0 , '" & Q2 & "')"
CurrentDb.Execute "Insert Into TblQdBodyF ([QdBodyId], [QdId], [Madeen], [Daen], [AccId], [CurrId]" _
& " ,[CurrValue], [MadeenCurrValue], [DaenCurrValue],[Details]) values" _
& " (" & x1 & ", " & x & ", 0, " & FTotal & ", " & Frs.Fields("AccId").Value & "," _
& "" & Frs.Fields("CurrId").Value & ", " & Q3 & ",0 , " & FTotal * Q3 & " , '" & Q2 & "')"
Else
FTotal = FTotal - HTotal
CurrentDb.Execute "Insert Into TblQdBodyF ([QdBodyId], [QdId], [Madeen], [Daen], [AccId], [CurrId]" _
& " ,[CurrValue], [MadeenCurrValue], [DaenCurrValue],[Details]) values" _
& " (" & x1 & ", " & x & ", " & FTotal & ", 0, " & Frs.Fields("AccId").Value & "," _
& "" & Frs.Fields("CurrId").Value & ", " & Q3 & ", " & FTotal * Q3 & ", 0 , '" & Q2 & "')"
CurrentDb.Execute "Insert Into TblQdBodyF ([QdBodyId], [QdId], [Madeen], [Daen], [AccId], [CurrId]" _
& " ,[CurrValue], [MadeenCurrValue], [DaenCurrValue],[Details]) values" _
& " (" & x1 & ", " & x & ", 0, " & FTotal & ", " & Frs.Fields("AccId").Value & "," _
& "" & Frs.Fields("CurrId").Value & ", " & Q3 & ",0 , " & FTotal * Q3 & " , '" & Q2 & "')"
End If
Frs.MoveNext
Loop
End Sub