PDA

View Full Version : For Each Loop help - for each Items in an Order



paccopeland
10-24-2010, 02:36 PM
Please help! I want to click on a button that executes code that will loop through each of the items in an order. The order is split into the order header and the order details - the order details are shown within the order form in a subform. The subform contains all the order items ordered. I want to be able run code from within the order form to carry out an action for all the items in the subform.

I hoped that using the For Each Loop would be the way forward and that it would be quite straight forward. But I'm struggling to work out defining the Object and the Collection. I guess the Collection is the list of relevant order items and the Object is the specific order item - but I'm struggling to convert this to VBA.

Is the For Each Loop the most appropriate way to solve this? Can you help me with the code?

Thanks
Paul

Imdabaum
10-25-2010, 07:27 AM
So you have a form and subform, and you want to do something like this? Hopefully the psuedo code makes sense.

TotalOrders=Order.RecordCount

For i as Integer = 1 To TotalOrders
'perform your actions on the form/subform values
Move to Next Record
Next i

That's one way, if I know I am going to perform the same function on every record, I usually like to use the query or table recordset and create a function that takes in the parameters that will be affected.

For me, forms are for presentation. If I had to modify one record or an occassional few records, I might do the calculations against the form fields, but if you want to make changes across the board, I find it much easier to go directly to the source.