robsworld78
Registered User.
- Local time
- Today, 03:49
- Joined
- May 31, 2011
- Messages
- 99
Hi, I have a bit of an issue with SQL Update / Insert.
I have a form that runs an SQL insert when a field is popluated and it works fine. When this sql insert runs it can add anywhere from 1 new record to 50 new records depending whats on the form.
DoCmd.RunSQL "INSERT INTO Inventory (Product, OrderID, BarsLeftTemp, BarsGiven) " & _
"SELECT ProductID, " & [Forms]![CreateOrders]![OrdersOrdersSubform]![OrderID] & " AS OrderID, " & "BarsLeft, " & "BarsLeft " & _
"FROM VendorInventoryLevel " & _ "WHERE (BarsLeft>0 Or BarsLeft<0) AND VendorID=" & [Forms]![CreateOrders]![VendorID] & ";"
The insert is for details pertaining to an order. Now I have a button on the same form and when clicked I want it to run another SQL insert/update but don't know how to right the code.
The 2nd insert will contain exisiting records from the 1st insert as well as new records so it needs to update the existing and insert the new records, I can't have duplicates.
Example:
I start a new order and the 1st SQL insert runs and I get the following items.
Order# 235 (stored in orders table)
item1 - 10 (stored in ordersdetail table linked with orderID) (10 being quanity)
item2 - 10
item3 - 10
item4 - 10
Now when I press the button, if I have SQL insert like above it would do the following, if the quantity was 5 on these items. (it duplicates items)
item1 - 10
item1 - 05
item2 - 10
item3 - 10
item3 - 05
item4 - 10
item4 - 05
item5 - 05
and if i press the button with SQL update it does the following (only updates, didn't add item5)
item1 - 10, 05
item2 - 10,
item3 - 10, 05
item4 - 10, 05
I hope this makes sense in the end the final list should be as follows because it updated the items already on the order and inserted items not on the order, in this case being item5.
item1 - 10, 05
item2 - 10,
item3 - 10, 05
item4 - 10, 05
item5 - 05
I have a form that runs an SQL insert when a field is popluated and it works fine. When this sql insert runs it can add anywhere from 1 new record to 50 new records depending whats on the form.
DoCmd.RunSQL "INSERT INTO Inventory (Product, OrderID, BarsLeftTemp, BarsGiven) " & _
"SELECT ProductID, " & [Forms]![CreateOrders]![OrdersOrdersSubform]![OrderID] & " AS OrderID, " & "BarsLeft, " & "BarsLeft " & _
"FROM VendorInventoryLevel " & _ "WHERE (BarsLeft>0 Or BarsLeft<0) AND VendorID=" & [Forms]![CreateOrders]![VendorID] & ";"
The insert is for details pertaining to an order. Now I have a button on the same form and when clicked I want it to run another SQL insert/update but don't know how to right the code.
The 2nd insert will contain exisiting records from the 1st insert as well as new records so it needs to update the existing and insert the new records, I can't have duplicates.
Example:
I start a new order and the 1st SQL insert runs and I get the following items.
Order# 235 (stored in orders table)
item1 - 10 (stored in ordersdetail table linked with orderID) (10 being quanity)
item2 - 10
item3 - 10
item4 - 10
Now when I press the button, if I have SQL insert like above it would do the following, if the quantity was 5 on these items. (it duplicates items)
item1 - 10
item1 - 05
item2 - 10
item3 - 10
item3 - 05
item4 - 10
item4 - 05
item5 - 05
and if i press the button with SQL update it does the following (only updates, didn't add item5)
item1 - 10, 05
item2 - 10,
item3 - 10, 05
item4 - 10, 05
I hope this makes sense in the end the final list should be as follows because it updated the items already on the order and inserted items not on the order, in this case being item5.
item1 - 10, 05
item2 - 10,
item3 - 10, 05
item4 - 10, 05
item5 - 05