Hi,
I'm trying to add items received to my inventory table. If the item is already in the table, I just want to update the number and cost etc. If the item is not in the inventory table I want to add it. My problem is determining which item in the source table is already in the target table so I can either update of add. Can anyone tell me how to find an item number in target table by looping through the source table?
Here's the code I've written which doesn't work.
Dim I As Integer
Dim db As Database
Dim rs As Recordset
Dim rs2 As Recordset
Set rs = Nothing
Set rs2 = Nothing
Set db = CurrentDb
Set rs2 = db.OpenRecordset("Inv_Recd2")
Set rs = db.OpenRecordset("Inv_Recd")
rs.MoveFirst
' loop through table Inv_Recd looking for ItemNumbers in Inv_Recd2. If the item number
' is found then edit the record with new info. If item is not found, add it.
For I = 0 To rs.RecordCount - 1
If rs2.Fields("ItemName") = rs.Fields("ItemName") Then
rs2.Edit
rs2.Fields("Cost") = rs.Fields("Cost")
rs2.Fields("Received") = (rs2.Fields("Received") + rs.Fields("Received"))
rs2.Fields("Price") = rs.Fields("Price")
rs2.Fields("Dept") = rs.Fields("Dept")
rs2.Fields("RPrice") = rs.Fields("RPrice")
rs2.Update
Else
rs2.AddNew
rs2.Fields("ItemNum") = rs.Fields("ItemNum")
rs2.Fields("ItemName") = rs.Fields("ItemName")
rs2.Fields("Cost") = rs.Fields("Cost")
rs2.Fields("Received") = rs.Fields("Received")
rs2.Fields("Price") = rs.Fields("Price")
rs2.Fields("Dept") = rs.Fields("Dept")
rs2.Fields("RPrice") = rs.Fields("RPrice")
rs2.Update
End If
rs.MoveNext
Next I
Errhandler: MsgBox Err.Number & Err.Description
rs.Close
Set rs = Nothing
rs2.Close
Set rs2 = Nothing
db.Close
Set db = Nothing
End Sub
I'm trying to add items received to my inventory table. If the item is already in the table, I just want to update the number and cost etc. If the item is not in the inventory table I want to add it. My problem is determining which item in the source table is already in the target table so I can either update of add. Can anyone tell me how to find an item number in target table by looping through the source table?
Here's the code I've written which doesn't work.
Dim I As Integer
Dim db As Database
Dim rs As Recordset
Dim rs2 As Recordset
Set rs = Nothing
Set rs2 = Nothing
Set db = CurrentDb
Set rs2 = db.OpenRecordset("Inv_Recd2")
Set rs = db.OpenRecordset("Inv_Recd")
rs.MoveFirst
' loop through table Inv_Recd looking for ItemNumbers in Inv_Recd2. If the item number
' is found then edit the record with new info. If item is not found, add it.
For I = 0 To rs.RecordCount - 1
If rs2.Fields("ItemName") = rs.Fields("ItemName") Then
rs2.Edit
rs2.Fields("Cost") = rs.Fields("Cost")
rs2.Fields("Received") = (rs2.Fields("Received") + rs.Fields("Received"))
rs2.Fields("Price") = rs.Fields("Price")
rs2.Fields("Dept") = rs.Fields("Dept")
rs2.Fields("RPrice") = rs.Fields("RPrice")
rs2.Update
Else
rs2.AddNew
rs2.Fields("ItemNum") = rs.Fields("ItemNum")
rs2.Fields("ItemName") = rs.Fields("ItemName")
rs2.Fields("Cost") = rs.Fields("Cost")
rs2.Fields("Received") = rs.Fields("Received")
rs2.Fields("Price") = rs.Fields("Price")
rs2.Fields("Dept") = rs.Fields("Dept")
rs2.Fields("RPrice") = rs.Fields("RPrice")
rs2.Update
End If
rs.MoveNext
Next I
Errhandler: MsgBox Err.Number & Err.Description
rs.Close
Set rs = Nothing
rs2.Close
Set rs2 = Nothing
db.Close
Set db = Nothing
End Sub