Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF Statement & Loop 1

Status
Not open for further replies.

anastasia

Programmer
Dec 13, 2000
111
GB
Hi, I am using the following VBScript code within an ASP page. The code is as follows:

<% Do while NOT RS.EOF %>
<% IF RS(&quot;Times&quot;) = mytime THEN %>
<% errorForm &quot;Correct. &quot; %>
<% ELSE %>
<% errorForm &quot;Not Correct. &quot; %>
<% END IF %>
<%
RS.MoveNext
Loop
%>

Further above in the page I have connected to the database. Other IF statements are before this and work fine. The mytime variable contains a time that a user enters in a form and this is compared to times held in the database, field name is called Times. I am trying to loop through these times and if the time entered by the user is not equal to any times held in the database then the appropriate message is displayed. However the above code seems only to check the first time in the database table and does not continue through the rest.?

I can't see anything wrong in the code, any ideas?

thanks.
 
Try sticking this code directly above what you have posted:
Code:
if not (rs.eof and rs.bof) then
  rs.movefirst
end if

If it only does one iteration then, then chances are you only have one record in the recordset because the code you posted looks rosey.

Just a note -- cash all those <%'s and %>'s except for two that would completely encapsulate the code fragment. They are not needed, and it will make your code a little more readable.

good luck! :)
Paul Prewett
 
Thank you link9 and gsc1ugs for your reply. I have implemented that extra code and end up with correct coming up where the time entered equlas the time in the database and not correct coming up for all the other times, so I know this code is working however I just want the time entered by the user to be checked if it exists in the database and if so the time entered is correct. The way it is working in the above it displays both correct and not correct for all records in the database. I guess I have to use an Exists function somewhere?
 
You might try just setting a variable, and then checking the value of that upon exit of the loop

ex)
Code:
<% 
dim found
found = false
Do while NOT RS.EOF
  IF RS(&quot;Times&quot;) = mytime  THEN
    found = true
  RS.MoveNext
Loop

if found then
  errorForm &quot;Correct. &quot;
else
  errorForm &quot;Not Correct. &quot;
end if

%>

good luck! :)
Paul Prewett
 
You might try just setting a variable, and then checking the value of that upon exit of the loop

ex)
Code:
<% 
dim found
found = false
Do while NOT RS.EOF
  IF RS(&quot;Times&quot;) = mytime  THEN
    found = true
  end if
  RS.MoveNext
Loop

if found then
  errorForm &quot;Correct. &quot;
else
  errorForm &quot;Not Correct. &quot;
end if

%>

good luck! :)
Paul Prewett

Think I hit the submit button once in error -- If so, please ignore that first post.
 
Thank you link9 for the above code it works perfect.

However I want the top part before the else to do nothing, I dont want anything to be displayed to the user if mytimes = times. I only want the bottom half to display to the user if mytime doesnt equal times. I have tried if found then end if then else but get an error.

if found then
errorForm &quot;Correct. &quot;
else
errorForm &quot;Not Correct. &quot;
end if

%>




 
replace the entire if statement with this:
Code:
if not found then
  errorForm &quot;Not Correct. &quot;
end if

just leave off the else altogether and test for 'not' --
 
Thanks for your reply link9 I now have the following code:

<%
dim found
found = false
Do while NOT RS.EOF
IF RS(&quot;Times&quot;) = mytime THEN
found = true
end if
RS.MoveNext
Loop

if not found then
errorForm &quot;Not Correct. &quot;
end if

%>

When this code is run it always comes up with the error Not correct even though there is a match in the database.

How do I test in the code for not?

Thanks again.

 
Well, you already are -- you are asking if 'not' found...

Boolean expression. And the wrong one at that. I'm sorry... that was my mistake. Take out the 'not' --

if found then
errorForm &quot;Not Correct. &quot;
end if

-paul
 
Thanks again for your reply, but unfortunately when I remove the not as above it does not display the Not Correct when it should so it is now doing the complete opposite to what it was doing before?
 
Ok, above, you said that the following code works perfect...

if found then
errorForm &quot;Correct. &quot;
else
errorForm &quot;Not Correct. &quot;
end if

Take out the action that you do not wish to occur...
 
Thanks paul. The code works fine, the reason it has not been working is that I had two loops within the code, I was trying to carry out two error checks in loops in the one connection so I took the other out and the code above works great.

Thanks you hve been very patient.

I will give you my vote.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top