Hi,
I got this query and IF clause. What I want is first to check OrderID and UnitID from TblOrderUnit. If both OrderID and UnitID are exist, the strSQL should just update the QtyOrdered. If not exist, then strSQL should insert the record into the
TblOrderUnit.
The problem I am facing right now is if the OrderID and UnitID are exist, it DOES update the old QtyOrdered , but ALSO create new record with same OrderID and UnitID in TblOrderUnit . Why is this happening?
Also, because I use recordset to copy all the data from TblOrderUnit and I use tblOrderUnitRS.MoveFirst, the trouble comes when I create new order and there is no records in the TblOrderUnit (new database). Any solution to do this (without using recordset maybe)?
THanks!
I got this query and IF clause. What I want is first to check OrderID and UnitID from TblOrderUnit. If both OrderID and UnitID are exist, the strSQL should just update the QtyOrdered. If not exist, then strSQL should insert the record into the
TblOrderUnit.
The problem I am facing right now is if the OrderID and UnitID are exist, it DOES update the old QtyOrdered , but ALSO create new record with same OrderID and UnitID in TblOrderUnit . Why is this happening?
Also, because I use recordset to copy all the data from TblOrderUnit and I use tblOrderUnitRS.MoveFirst, the trouble comes when I create new order and there is no records in the TblOrderUnit (new database). Any solution to do this (without using recordset maybe)?
THanks!
Code:
qryTblOrderUnit = "SELECT * FROM TblOrderUnit"
Set tblOrderUnitRS = dbs.OpenRecordset(qryTblOrderUnit)
tblOrderUnitRS.MoveFirst
strSQL = "INSERT INTO TblOrderUnit([OrderunitID],[OrderID],[UnitID],[QtyOrdered],[UnitDeliveryDate])" _
& " VALUES (" & OrderunitID & ", " & OrderID & ", " & UnitID & ", " & QtyOrdered & ", #" & DeliveryDate & "# )"
Do While tblOrderUnitRS.EOF = False
If tblOrderUnitRS!OrderID = OrderID And tblOrderUnitRS!UnitID = UnitID Then
strSQL = "UPDATE TblOrderUnit SET QtyOrdered=QtyOrdered+" & QtyOrdered & " WHERE OrderID=" & OrderID & " AND UnitID=" & UnitID
Exit Do
End If
tblOrderUnitRS.MoveNext
Loop
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True