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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with If VALUE <> VALUE2 THEN 1

Status
Not open for further replies.

ComputerCop911

Programmer
Jul 30, 2003
97
CA
ok, I am running a script that requires the user to input data into a form, and I want the form to check if the data exists in ANY record in the field. but when I use this code:

if Request.Form(&quot;VALUE1&quot;) <> recordset(&quot;VALUE2&quot;) then

response.write(&quot;The data you entered is invalid.&quot;)

else
------
then it continues with the code if the data exists. However, it only checks the first record! it doesnt check every record to see if the data is anywhere else.

can someone help?

ComputerCop911
ASP/HTML Programmer
 
You could write another query to retrieve rows with the VALUE1; if you get some then print the error message; if not then proceed.

Or you could loop through the recordset, testing each record; if you find one that matches print the error message; otherwise proceed.

Code:
result = &quot;OK&quot;
While Not recordset.EOF
   if Request.Form(&quot;VALUE1&quot;) = recordset(&quot;VALUE2&quot;) then
       result = &quot;BAD&quot;
       Break
   EndIf
   
   recordset.moveNext
Wend



If result = &quot;BAD&quot; Then
   response.write(&quot;The data you entered is invalid.&quot;)
Else
   recordset.moveFirst
   'proceed
EndIf

Hope this is clear. This is not code, just pseudocode.
 
Do it like this.

Do While not recordset.EOF
if Request.Form(&quot;VALUE1&quot;) <> recordset(&quot;VALUE2&quot;) then

response.write(&quot;The data you entered is invalid.&quot;)

else
recordset.movenext
Loop
 
spazman,

thanks it worked perfectly![2thumbsup]

ComputerCop911
ASP/HTML Programmer
 
oops, sorry I was looking at the wrong page...

now it says

Microsoft VBScript compilation error '800a040e'

'loop' without 'do'

the Do is there, I did it like you said..

ComputerCop911
ASP/HTML Programmer
 
ok, i fiddled with it, and it works...sort of.

it looks like its working, but then after a while it says Script timed out. it only scans about 139 records (or i think slightly less).

I think either 139 records is too much for it, or it makes itself go into an infinite loop.

this is the code

Do While not recordset.EOF

If Request.form(&quot;value1&quot;) <> recordset(&quot;value2&quot;) then
rsFlights.MoveNext()
Else
Response.Write(&quot;The data you entered was invalid.&quot;)
End If
Loop

not sure if its an infinite loop or if 139 records needs more than 90 seconds (my hosts limit) to execute it.

ComputerCop911
ASP/HTML Programmer
 
ok, i tried executing it again, and it displayed that Your data is invalid thing oVer and OVER and OVer again, so I know its an infinite loop.

now can someone help me FIX it? please?

ComputerCop911
ASP/HTML Programmer
 
first of all if ur looping then:
Do While not recordset.EOF

If Request.form(&quot;value1&quot;) <> recordset(&quot;value2&quot;) then
rsFlights.MoveNext()
Else
Response.Write(&quot;The data you entered was invalid.&quot;)
End If
recordset.movenext
Loop

note the movenext comand.

next why are u going through all the records? may i know what u r trying to achieve?


Known is handfull, Unknown is worldfull
 
I want the form results to be checked with the database to see if there is a value in a field that is the same value as the form results.

I want it to check ALL the records to see if the value is there, and STOP looping when it comes to the end, or stop when it finds it.

but with my first code, it would only check the first record. now with your guy's code, it goes into an infinite loop.

ComputerCop911
ASP/HTML Programmer
 
this is the code, sorry forgot to add it

Do While not recordset.EOF

If Request.form(&quot;value1&quot;) <> recordset(&quot;value2&quot;) then
recordset.MoveNext()
Else
Response.Write(&quot;The data you entered was invalid.&quot;)
End If
Loop

ComputerCop911
ASP/HTML Programmer
 
then the best way is to execute an sql:
sql=&quot;select * from table where TableColumn='Value'&quot;

rs.open sql,Connection 'rs is a recordset object

if not rs.oef and not rs.bof then
response.write &quot;Value is there&quot;
else
response.write &quot;Value is not there&quot;
end if

regrading the infinite loop please check my previous post...


Known is handfull, Unknown is worldfull
 
now it says &quot;object does not support this property: oef&quot;

ComputerCop911
ASP/HTML Programmer
 
well did u try my sql method? urs seems to be a workaround that will take server time. and that oef is a typo, sorry for that...

Known is handfull, Unknown is worldfull
 
Let me guess. Is this what you have?

<%
Do While not recordset.EOF
If recordset(&quot;value2&quot;) <> Request.form(&quot;value1&quot;) then
Response.Write(&quot;The data you entered was invalid.&quot;)
Else
recordset.MoveNext()
End If
Loop
%>
 
This is the quickest and easiest way to see if a match is true.

<%
db = &quot;Provider=Microsoft.Jet.OLEDB.4.0; Data Source=whatever.mdb&quot;

Value1 = request(&quot;Value1&quot;)

set rs = server.createobject(&quot;adodb.recordset&quot;)
sql = &quot;select * from table&quot;
sql = sql & &quot; where Field1 = '&quot; & Value1 & &quot;'&quot;
rs.open sql, db
if rs.eof then
response.write &quot;The data you entered is invalid.&quot;
end if
rs.close
set rs = nothing
%>
 
I just went

sql=select * from table where value='&quot;& value2 &&quot;'&quot;

and then went if value1 <> value2 then
&quot;data is invalid&quot;

(This of course isnt legit ASP code, its just a brief sum-up of what I did.)

ComputerCop911
ASP/HTML Programmer
 
>>sql=select * from table where value='&quot;& value2 &&quot;'&quot;
>>
>>and then went if value1 <> value2 then
>>&quot;data is invalid&quot;

So is the field named value or value1?

Didn't you just want to check to see if there were any matches and if not display your message? Maybe I'm missing some of your code, but it seems that what you're doing is looking for a match and then double checking to see if each record doesn't match.

With the method I proposed, the code would simple see if the recordset makes it to eof (end of file). If it did it would display the message, but there was no double checking.

Hey if it works for you then that's great though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top