For Loop...

1. Senior Member
Join Date
Dec 1969
Posts
1,323

## For Loop...

ok, I have this loop<BR><BR>&#039;gather up values for Peirce transfer Credits<BR> Dim hid,arrayHid,ArrayTotal,Total,i<BR> <BR> hid = sPCredit<BR> arrayHid = Split(hid, ",") <BR> For i = 0 to Ubound(arrayHid) <BR> ArrayTotal = arrayHid(i) + Cint(ArrayTotal)<BR> Next<BR> &#039;Add Peirce Transfer Credits up<BR> Total = Cint(ArrayTotal)<BR> <BR>What I want to say is IF hid = the same number more than once, only use it once, then move to the next one...<BR><BR>I am getting values from the previous page, and they can throught like this<BR>(1,2,3) or<BR>(1,1,1,2,3, etc...)<BR>What I want to do is skip the second and third 1, so that when I add up the value, I only get 6 instead of 8. Make sense? Any thoughts on how?<BR><BR>Harry C

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

## Pretty easy...

...and we can dump some useless CINT&#039;s and unneeded variables from your code in the process:<BR><BR>&#060;%<BR>...<BR>arrayHid = Split(spCredit, ",") &#039; what do we need "hid" for???<BR>Total = 0 &#039; you *must* initialize this!<BR>prior = -999 &#039; and initialize the test variable, too...<BR>For i = 0 to Ubound(arrayHid) <BR>&nbsp; &nbsp; cur = arrayHid(i) &#039; current value<BR>&nbsp; &nbsp; If cur &#060;&#062; prior Then &#039; new value!<BR>&nbsp; &nbsp; &nbsp; &nbsp; Total = Total + cur &#039; add to total<BR>&nbsp; &nbsp; &nbsp; &nbsp; prior = cur &#039; update prior<BR>&nbsp; &nbsp; End If<BR>Next<BR>%&#062;<BR><BR>NOTE: This *ONLY* works if the values in arrayHid are *grouped*!<BR><BR>If you had, for example: 1,2,3,1,4,3,2,1,2,1<BR>Then this would not work. <BR><BR>If that might happen, then sort the array before running it through for the total.<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
1,323

## RE: Pretty easy...

ok, I am an idiot, I totally asked the wrong question, alright my array is sorted. What happend is that from the previous page an ID is taken, sometimes as I said it can be 1,1,1,2,3 or 1,2,3 Here is the code, ok, so for each iIremID i want to get the credits and add them up. But if I get two of them same IREM IDs, want to skip that one, and move to the next one. Make any sense? I know this just got more complicated. But this is my last step, and would be great if I got it done. Thanks!<BR><BR> iIremID=TRIM(Request.Form("IremID"))<BR> <BR> If iIremID = "" then &#039; Check to see if an ID is carried over from Previous Selection<BR> Response.Write "&nbsp;" &#039; If not, End<BR> <BR> Else &#039; If ID is found, begin creating recordset<BR><BR>cmd.CommandText = "SELECT irem.irem_course_cde, irem.irem_course_des, irem.irem_credit_hrs, peirce.course_code, groupCredit.groupID, groupCredit.credits " & _<BR>"FROM peirce INNER JOIN (irem INNER JOIN (groupCredit INNER JOIN PeirceIremJoin " & _<BR>"ON groupCredit.groupID = PeirceIremJoin.groupID) ON irem.courseID = PeirceIremJoin.iremID) " & _<BR>"ON peirce.PeirceID = PeirceIremJoin.peirceID " & _<BR>"WHERE (((groupCredit.groupID) IN (" & iIremID & ")))"<BR><BR> <BR> <BR> Set rsPeirce = Server.CreateObject("ADODB.Recordset")<BR> rsPeirce.CursorLocation = adUseClient<BR> rsPeirce.Open cmd,,adOpenDynamic,adLockOptimistic<BR><BR> rsPeirce.MoveFirst<BR> Do While Not rsPeirce.EOF<BR> <BR> &#039;set the recordsets to variables<BR> sPeirceClass=rsPeirce.Fields("course_code")<BR> sPCredit=rsPeirce.Fields("credits")<BR> sIremClass=rsPeirce.Fields("irem_course_cde")<BR> sICredit=rsPeirce.Fields("irem_credit_hrs")<BR> sIrem_Cred_Des=rsPeirce.Fields("irem_course_des")< BR> <BR> &#039;gather up values for Peirce transfer Credits<BR> Dim prior,arrayHid,ArrayTotal,Total,i,cur<BR> <BR> <BR> hid = sPCredit<BR> arrayHid = Split(hid, ",") <BR> For i = 0 to Ubound(arrayHid) <BR> ArrayTotal = arrayHid(i) + Cint(ArrayTotal)<BR> Next<BR> &#039;Add Peirce Transfer Credits up<BR> Total = Cint(ArrayTotal)<BR> <BR> <BR> <BR> &#039;write the values<BR> Response.write "&#060;tr&#062;&#060;td align=center&#062;" & sIremClass & "&#060;/td&#062;" & vbCrLf<BR> Response.Write "&#060;td align=center&#062;" & sIrem_Cred_Des & "&#060;/td&#062;" & vbCrLf<BR> Response.Write "&#060;td align=center&#062;" & sICredit & "&#060;/td&#062;" & vbCrLf<BR> Response.write "&#060;td align=center&#062;" & sPeirceClass & "&#060;/td&#062;" & vbCrLf<BR> Response.Write "&#060;input type=hidden name=ctlPCredits value=" & sPCredit & "&#062;" & vbCrLf<BR> <BR> rsPeirce.MoveNext<BR> Loop

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

## I'm getting lost...

Why is the "credits" field a comma delimited string???<BR><BR>sPCredit=rsPeirce.Fields("credits ")<BR>...<BR>hid = sPCredit<BR>arrayHid = Split(hid, ",") <BR><BR>????<BR><BR>How do you manage to relate the elements of that string to other things from the query????<BR><BR>And you&#039;ve got other strange code in there. Why are you using adUseClient for the cursor? What difference does it make? And why adOpenDynamic for the cursor type??? And why bother with a command object when you aren&#039;t using a stored procedure???<BR><BR>Tell you what, how about doing this.<BR><BR>*TEMPORARILY*<BR><BR>**************** ************<BR>&#060;%<BR>SQL = "SELECT irem.irem_course_cde, irem.irem_course_des, irem.irem_credit_hrs, peirce.course_code, groupCredit.groupID, groupCredit.credits " & _<BR>"FROM peirce INNER JOIN (irem INNER JOIN (groupCredit INNER JOIN PeirceIremJoin " & _<BR>"ON groupCredit.groupID = PeirceIremJoin.groupID) ON irem.courseID = PeirceIremJoin.iremID) " & _<BR>"ON peirce.PeirceID = PeirceIremJoin.peirceID " & _<BR>"WHERE (((groupCredit.groupID) IN (" & iIremID & ")))"<BR><BR>Response.Write "&#060;HR&#062;SQL: " & SQL & "&#060;HR&#062;" & vbNewLine<BR>Set rsPeirce = yourConnectionObject.Execute( SQL )<BR><BR>Response.Write "&#060;TABLE Border=1 CellPadding=5&#062;&#060;TR&#062;" & vbNewLine<BR>For fnum = 0 To rsPeirce.Fields.Count-1<BR>&nbsp; &nbsp; Response.Write "&#060;TH&#062;" & rsPeirce.Fields(fnum).Name & "&#060;/TH&#062;" & vbNewLine<BR>Next<BR>Response.Write "&#060;/TR&#062;" & vbNewLine<BR><BR>Do Until rsPeirce.EOF<BR>&nbsp; &nbsp; Response.Write "&#060;TR&#062;" & vbNewLine<BR>&nbsp; &nbsp; For fnum = 0 To rsPeirce.Fields.Count-1<BR>&nbsp; &nbsp; &nbsp; &nbsp; Response.Write "&#060;TD&#062;" & rsPeirce.Fields(fnum).Value & "&#060;/TD&#062;" & vbNewLine<BR>&nbsp; &nbsp; Next<BR>&nbsp; &nbsp; Response.Write "&#060;/TR&#062;" & vbNewLine<BR>&nbsp; &nbsp; rsPeirce.MoveNext<BR>Loop<BR>%&#062;<BR>&#060;/TABLE&#062;<BR><BR>********************<BR><BR>And send me what that dumps out on your browser screen (do a VIEW &#124 SOURCE in the browser to get the HTML and then copy/paste the HTML into an email message to bill.wilkinson@sun.com).<BR><BR>Let me see the raw data we are working with, first, so I can quit guessing.<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
•