For Loop Question

1. Senior Member
Join Date
Dec 1969
Posts
349

## For Loop Question

Am I right in assuming that in a For...Loop structure all of the statements are applied to the iLoop count? For example, if iLoop = 4, the statements within the For...Next would run for each of the 4 items? At what point would an Exit For be used? The reason I ask is I have two basic scenarios: one block that should run if NOT RS.EOF and one that should run if RS.EOF. The NOT RS.EOF runs perfect, but the RS.EOF does not. During the iterations of RS.EOF, the condition of NOT RS.EOF then become true (because an insert statement is run. <BR><BR>For iLoop = 0 to iCount - 1<BR>Command1<BR>Command2<BR>And so on...<BR><BR>Next<BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Welll....

&#062; For example, if iLoop = 4, the statements within the<BR>&#062; For...Next would run for each of the 4 items? <BR>...<BR>&#062; For iLoop = 0 to iCount - 1<BR><BR>Ummm...I *guess* you mean if iCount = 4, yes? If so, then yes, all the statements from FOR to NEXT would be executed 4 times (0, 1, 2, 3 values for iLoop). *Except* see below.<BR><BR>&#062; At what point would an Exit For be used? <BR><BR>Wherever you wanted it. In which case all statements after the EXIT FOR are not executed and, no matter how many times the loop has executed, the loop completely stops then and there.<BR><BR>&#062; The reason I ask is I have two basic scenarios: <BR>&#062; one block that should run if NOT RS.EOF and one that should <BR>&#062; run if RS.EOF. The NOT RS.EOF runs perfect, but the RS.EOF <BR>&#062; does not. <BR><BR>Yes? And how can we guess the problem from what you&#039;ve said and show us?<BR><BR>&#062; During the iterations of RS.EOF, the condition of NOT RS.EOF <BR>&#062; then become true (because an insert statement is run). <BR><BR>Ummm...that should *not* happen unless you open the recordset in adOpenDynamic mode, I *believe*. (Maybe adOpenKeyset, too. Not sure.)<BR><BR>Are you trying to *prevent* that or are you trying to take advantage of that?<BR><BR><BR><BR>

3. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## Are you opening a recordset

in a FOR loop??? is there no way you can open it only once???<BR><BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
349

## RE: Welll....

I need to take advantage of that...

5. Senior Member
Join Date
Dec 1969
Posts
349

## RE: Are you opening a recordset

If I open it once and the records change, I would not see the chnage would I?

6. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## You have to make up your mind

DO you or DONT you want to see the change??<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
349

## RE: You have to make up your mind

Yes, I want to see the change AND execute certain statements in the event of the change. If a record is added on the one side, I need to perform an update on the many side.

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## That doesn't make lots of sense...

9. Senior Member
Join Date
Dec 1969
Posts
349

## RE: That doesn't make lots of sense...

Here is the logical method I need to accomplish:<BR><BR>for each value in the field of the submitted form:<BR>see if value exists in table1 <BR>if yes then<BR>change id for the submitted value in table2 to match id of existing record<BR>if no then<BR>add record to table1<BR>change id in table2 to match id of new record<BR>

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## So why do you need dynamic...

If you simply grab the id of the new record when you add to table 1, then you can apply that id in your last step.<BR><BR>I think it would be a mistake to do this with a dynamic cursor.<BR><BR>&#060;%<BR>Set RS1 = conn.Execute( "SELECT * FROM table1 WHERE someField=" & Request("someValue") )<BR>If RS1.EOF Then<BR>&nbsp; &nbsp; Set RS2 = Server.CreateObject("ADODB.RecordSet")<BR>&nbsp; &nbsp; RS2.Open "table1",conn,adOpenKeyset,adLockPessimistic<BR>&n bsp; &nbsp; RS2.AddNew<BR>&nbsp; &nbsp; ...<BR>&nbsp; &nbsp; RS2.Update<BR>&nbsp; &nbsp; id = RS2("id")<BR>Else<BR>&nbsp; &nbsp; id = RS1("id")<BR>End If<BR>SQL = "UPDATE table2 SET id=" & id & " WHERE someT2Field = " & Request("someValue") <BR>%&#062;<BR><BR><BR>Or something along those lines.<BR><BR>If you are trying to avoid doing the outermost query for each of the form values, no problem. Just get all the matching records [using "WHERE someField IN ( ...list of values from form ...)" ] into an array, say, and loop through with the same logic otherwise. Means doing a match in VBS for the values from the form, but that&#039;s just tedious, not hard.<BR><BR><BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•