it would be easier to run an append query, or a SQL statement, from your
command button, as
Dim lngID As Long
lngID = Me!OrderID
DoCmd.RunCommand acCmdRecordsGoToNew
Me!OrderDate = Date
DoCmd.RunCommand acCmdSaveRecord
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO tblOrdersDetail (OrderID, ProductID, Quantity)
" _
& "SELECT " & Me!OrderID & ", [ProductID]," _
& "[Quantity]-([Quantity]*2) FROM tblOrdersDetail WHERE
tblOrdersDetail.OrderID=" _
& lngID
DoCmd.SetWarnings True
Me!MySubformControlName.Requery
i used the table and field names you provided in my test. but you need to
substitute the correct name in the requery action there at the end. i
believe you can also run the SQL inside a transaction, for more control of
the update - but i can't give you details on that. i learned about it here
in the newsgroups, but haven't tried it for myself yet.
hth
"Gordon" <(E-Mail Removed)> wrote in message
news:131B8404-C74E-4D5B-A194-(E-Mail Removed)...
> I have 2 tables: tblOrders and tblOrdersDetail.
> tblOrders has the following fields: OrderID, OrderDate.
> tbltOrdersDetail has these fields: OrdersDetailID, OrderID, ProductID,
Quantity
>
> What I am trying to do is, when an order is returned, I can click a button
on
> my frmOrder to automatically enter a new record in tblOrder, then new
> records in tblOrdersDetail, whose ProductIDs are the same as the original
> order, with each Quantity is a negative value of the original Quantity.
>
> I tried with the following code to complete the 2nd part of the work:
>
> Private Sub Return_Click()
>
> Dim db As DAO.Database, rs As DAO.Recordset
> Dim RtnOrderID As String, RtnProductID As String, strCriteria As
String
> Dim intCriteria As Integer, RtnQuantity As Integer
>
> Set db = CurrentDb
> Set rs = db.OpenRecordset("tblOrdersDetail", dbOpenDynaset)
> strCriteria = Forms![frmOrders]![SelectOrder]
> With rs
> .FindFirst "OrderID = '" & strCriteria & "'"
> Do While Not .NoMatch
> intCriteria = DLookup("OrdersDetailID", "tblOrdersDetail",
"OrderID = '" & strCriteria & "'")
> RtnOrderID = strCriteria & "R"
> RtnProductID = DLookup("ProductID", "tblOrdersDetail",
"OrdersDetailID =" & intCriteria)
> RtnQuantiry = -1 * DLookup("Quantity", "tblOrdersDetail",
"OrdersDetailID =" & intCriteria)
> .AddNew
> !OrderID = RtnOrderID
> !ProductID = RtnProductID
> !Quantity = RtnQuantity
> .Update
> .FindNext "OrderID = '" & strCriteria & "'"
> Loop
> End With
>
> End Sub
>
> When there is only one product under an order, the code works fine. But
> when there are more than two products under an order, the code can
> add as many rows as the original order to the tblOrdersDetail table. But
> each row is the same: copy the first ProductID, enter a negative value of
> the first Quantity. Can anyone tell what is wrong in my code? Thanks.
>
>
>
|