looping problem

Results 1 to 2 of 2

Thread: looping problem

  1. #1
    Join Date
    Dec 1969

    Default looping problem

    I am creating a search clause looping through a form field i am able to get the individual form fields value using for loop but when I am trying to build my search clause for sql I always get the last field value ....<BR>I need to get all the fields value and if some field is blank then do not take that field in building the search clause for sql...any ideas how can I do it...<BR>I am looping my foem as follow<BR><BR><BR>for x=0 to ubound(search_field) <BR> &#039;if search_value(x) &#060;&#062; "" then <BR> &#039;Response.Write " search_value not blank"<BR> &#039;if search_clause &#060;&#062; "" then <BR> search_clause=search_clause&","<BR> search_clause=search_field(x)&"=&#039;"&(trim(sear ch_value(x)))&"&#039;"<BR> &#039;Response.Write search_clause&"searchclause for zee"<BR> strSrchClause = search_clause<BR> Response.Write "<BR>" & strSrchClause & "strSrchClause"<BR> <BR> &#039;end if <BR> end if <BR> next

  2. #2
    Join Date
    Dec 1969

    Default Nope...faulty 'if' logic...

    Well, first of all, that code as you show it can&#039;t possibly even compile, as you have an "end if" but all the "if" statements are commented out.<BR><BR>I will *assume* that the second "if" should *not* be commented out. If that is true, then your bug is in the "if".<BR><BR>It helps to indent your code properly. Let&#039;s do that and get rid of the extraneous crap at the same time:<BR><BR>&#060;%<BR>...<BR>for x=0 to ubound(search_field) <BR>&nbsp; &nbsp; if search_clause &#060;&#062; "" then <BR>&nbsp; &nbsp; &nbsp; &nbsp; search_clause=search_clause&","<BR>&nbsp; &nbsp; &nbsp; &nbsp; search_clause=search_field(x)&"=&#039;"&(trim(sear ch_value(x)))&"&#039;"<BR>&nbsp; &nbsp; end if <BR>next <BR>%&#062;<BR><BR>Do you see it? You will *only* execute the code that tacks stuff onto the end of "search_clause" *IF* there is already something *in* "search_clause". But if it starts out empty, then there is *never* anything in it, so you never tack anything on!<BR><BR>Do yourself a favor and simplify, simplify, simplify. There really isn&#039;t any need for the "if", you know. You could do this:<BR><BR>&#060;%<BR>...<BR>for x=0 to ubound(search_field) <BR>&nbsp; &nbsp; search_clause=search_clause & "," & search_field(x) & "=&#039;" & (trim(search_value(x))) & "&#039;"<BR>next <BR>&#039; now everything is fine except we have a comma on the front end...<BR>search_clause = Mid( search_clause, 2 ) &#039; get rid of that front-end comma!<BR>...<BR>%&#062;<BR><BR>Presto. No "if" needed. Isn&#039;t that simpler?<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