PC Review


Reply
Thread Tools Rate Thread

Loop problem

 
 
=?Utf-8?B?R29yZG9u?=
Guest
Posts: n/a
 
      23rd May 2004
I have 2 tables: tblOrders and tblOrdersDetail.
tblOrders has the following fields: OrderID, OrderDate
tbltOrdersDetail has these fields: OrdersDetailID, OrderID, ProductID, Quantit

What I am trying to do is, when an order is returned, I can click a button o
my frmOrder to automatically enter a new record in tblOrder, then ne
records in tblOrdersDetail, whose ProductIDs are the same as the origina
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.Recordse
Dim RtnOrderID As String, RtnProductID As String, strCriteria As Strin
Dim intCriteria As Integer, RtnQuantity As Intege

Set db = CurrentD
Set rs = db.OpenRecordset("tblOrdersDetail", dbOpenDynaset
strCriteria = Forms![frmOrders]![SelectOrder
With r
.FindFirst "OrderID = '" & strCriteria & "'
Do While Not .NoMatc
intCriteria = DLookup("OrdersDetailID", "tblOrdersDetail", "OrderID = '" & strCriteria & "'"
RtnOrderID = strCriteria & "R
RtnProductID = DLookup("ProductID", "tblOrdersDetail", "OrdersDetailID =" & intCriteria
RtnQuantiry = -1 * DLookup("Quantity", "tblOrdersDetail", "OrdersDetailID =" & intCriteria
.AddNe
!OrderID = RtnOrderI
!ProductID = RtnProductI
!Quantity = RtnQuantit
.Updat
.FindNext "OrderID = '" & strCriteria & "'
Loo
End Wit

End Su

When there is only one product under an order, the code works fine. Bu
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. Bu
each row is the same: copy the first ProductID, enter a negative value o
the first Quantity. Can anyone tell what is wrong in my code? Thanks



 
Reply With Quote
 
 
 
 
tina
Guest
Posts: n/a
 
      24th May 2004
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.
>
>
>



 
Reply With Quote
 
=?Utf-8?B?R29yZG9u?=
Guest
Posts: n/a
 
      24th May 2004
Thanks Tina. But I can not figure out how to fit in your code
Now my solution is like this
1. create a query named [RtnOrderQuery]
INSERT INTO tblOrdersDetail ( OrderID, ProductID, Quantity
SELECT [OrderID] & "R" AS RtnOrder, tblOrdersDetail.ProductID, -1*[Quantity] AS RtnQua
FROM tblOrdersDetai
WHERE (((tblOrdersDetail.OrderID)=[forms]![frmOrders]![SelectOrder]))
2. then a simple code to run the query
Dim strSQL As Strin
strSQL = "RtnOrderQuery
Docmd.OpenQuery strSQL, acNormal, acEdi

It works just perfect. It was based on your suggestion. But I just could no
figure out how to code that query so that it does not physically exit. Can yo
tell me how to make that strSQL = "INCERT INTO ... SELECT ... FROM ... WHERE..."
It was awefully difficult for me to quote those stuff, especially with some criterias
Thanks.
 
Reply With Quote
 
tina
Guest
Posts: n/a
 
      24th May 2004
it's a text string. when you have a multiple line text string in VBA, you
have to enclose each text line in quotes, put a continuation character at
the end of each line (space then underscore), and concatenate each line with
the ampersand. references to a form have to be outside of the quotes.
i'm guessing you got confused looking at my previous post because of the
word wrap. here it is again:

DoCmd.RunSQL "INSERT INTO tblOrdersDetail " _
& "(OrderID, ProductID, Quantity) SELECT " _
& Me!OrderID & ", [ProductID], " _
& "[Quantity]-([Quantity]*2) FROM tblOrdersDetail " _
& "WHERE tblOrdersDetail.OrderID=" & lngID

as long as your solution is working, might as well stick with it. my
understanding is that a query object in the database has the advantage of
being already compiled at runtime.

hth


"Gordon" <(E-Mail Removed)> wrote in message
news:5E856539-AC84-495E-A95C-(E-Mail Removed)...
> Thanks Tina. But I can not figure out how to fit in your code.
> Now my solution is like this:
> 1. create a query named [RtnOrderQuery]:
> INSERT INTO tblOrdersDetail ( OrderID, ProductID, Quantity )
> SELECT [OrderID] & "R" AS RtnOrder,

tblOrdersDetail.ProductID, -1*[Quantity] AS RtnQuan
> FROM tblOrdersDetail
> WHERE (((tblOrdersDetail.OrderID)=[forms]![frmOrders]![SelectOrder]));
> 2. then a simple code to run the query:
> Dim strSQL As String
> strSQL = "RtnOrderQuery"
> Docmd.OpenQuery strSQL, acNormal, acEdit
>
> It works just perfect. It was based on your suggestion. But I just could

not
> figure out how to code that query so that it does not physically exit.

Can you
> tell me how to make that strSQL = "INCERT INTO ... SELECT ... FROM ...

WHERE..."?
> It was awefully difficult for me to quote those stuff, especially with

some criterias.
> Thanks.



 
Reply With Quote
 
=?Utf-8?B?R29yZG9u?=
Guest
Posts: n/a
 
      24th May 2004
Thanks again, Tina. Now I am more clearer on that.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop Within a Loop Problem derrick.perkins@gmail.com Microsoft Excel Programming 3 5th Dec 2007 02:23 PM
Vista Upgrade Problem - Windows Explorer Loop problem =?Utf-8?B?U3RldmUgUw==?= Windows Vista Installation 0 27th Jun 2006 05:15 PM
For Loop Problem Shapper Microsoft ASP .NET 1 4th Oct 2005 03:24 AM
Problem adding charts using Do-Loop Until loop =?Utf-8?B?Q2hyaXMgQnJvbWxleQ==?= Microsoft Excel Programming 2 23rd May 2005 02:31 PM
Interesting problem: My For loop doesn't want to loop! =?Utf-8?B?UnVkeQ==?= Microsoft VB .NET 3 4th Dec 2003 06:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:09 AM.