I have a procedure that opens two record sets and updates rs2.[demand] with rs1.[SumOfLQORD]. It works good except for this problem: If rs1 does not have a match in rs2 the value in rs2 is not updated. I need the value rs2.[demand] to be set to zero if no match is found.
Here is the code:
Public Sub subLoadDemand()
' update 'demand' field in tblPartNmbr with
' qryOrdersGrouped.[SumOfLQORD]
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Set db = CurrentDb()
Set rs1 = db.OpenRecordset("qryOrdersGrouped")
Set rs2 = db.OpenRecordset("tblPartNmbr")
rs1.MoveFirst
Do Until rs1.EOF
' if matching rec is found then update field
' in second rec to value
If rs2.RecordCount = 0 Then Exit Sub
rs2.MoveFirst
Do Until rs2.EOF
If rs1![PartNmbr] = rs2![FGItem] Then
rs2.Edit
' ***** need to add code to set value to zero if match
' ***** not found
rs2![demand] = rs1![SumOfLQORD] / 4
rs2.Update
End If
rs2.MoveNext
Loop
rs1.MoveNext
Loop
rs2.MoveFirst
Debug.Print rs2![demand]
rs1.MoveFirst
Debug.Print rs1![PartNmbr]
' close tables
rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing
End Sub
Thanks so much!
Here is the code:
Public Sub subLoadDemand()
' update 'demand' field in tblPartNmbr with
' qryOrdersGrouped.[SumOfLQORD]
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Set db = CurrentDb()
Set rs1 = db.OpenRecordset("qryOrdersGrouped")
Set rs2 = db.OpenRecordset("tblPartNmbr")
rs1.MoveFirst
Do Until rs1.EOF
' if matching rec is found then update field
' in second rec to value
If rs2.RecordCount = 0 Then Exit Sub
rs2.MoveFirst
Do Until rs2.EOF
If rs1![PartNmbr] = rs2![FGItem] Then
rs2.Edit
' ***** need to add code to set value to zero if match
' ***** not found
rs2![demand] = rs1![SumOfLQORD] / 4
rs2.Update
End If
rs2.MoveNext
Loop
rs1.MoveNext
Loop
rs2.MoveFirst
Debug.Print rs2![demand]
rs1.MoveFirst
Debug.Print rs1![PartNmbr]
' close tables
rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing
End Sub
Thanks so much!