if then statement not working in .asp file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi. I've got one of those weird problems like when gravity stops working. It
may be because I have no idea what I'm doing. I am trying to compare a
previously entered value with a value coming from a form. Everything works
fine, but my if then statement (identical to others that do work) isn't
working. Here it is:

<%
dim SqlJunk

SqlJunk = "SELECT * FROM study1 WHERE ID = 652"

Set out = Server.CreateObject("ADODB.Recordset")
out.Open SqlJunk, cnn, 3,3

oldrecord=out("question2")
Response.Write oldrecord
Response.Write newrecord

if oldrecord = newrecord then
message = "It works!"
Response.Write message

end if
%>

So it writes the values 5 and 5; The values for the new record (from the
form) and the old record (from access table) respectively, but then fails to
write the message, despite the fact that the if then condition should be
satisfied. It DOES work as expected if < is used, but not if > is used. It
also works if I substitute a different variable from the form. So oldrecord
=5, but not really? What am I not getting? Help?
 
bazango said:
Hi. I've got one of those weird problems like when gravity stops working. It
may be because I have no idea what I'm doing. I am trying to compare a
previously entered value with a value coming from a form. Everything works
fine, but my if then statement (identical to others that do work) isn't
working. Here it is:

<%
dim SqlJunk

SqlJunk = "SELECT * FROM study1 WHERE ID = 652"

Set out = Server.CreateObject("ADODB.Recordset")
out.Open SqlJunk, cnn, 3,3

oldrecord=out("question2")
Response.Write oldrecord
Response.Write newrecord

if oldrecord = newrecord then
message = "It works!"
Response.Write message

end if
%>

So it writes the values 5 and 5; The values for the new record (from the
form) and the old record (from access table) respectively, but then fails to
write the message, despite the fact that the if then condition should be
satisfied. It DOES work as expected if < is used, but not if > is used. It
also works if I substitute a different variable from the form. So oldrecord
=5, but not really? What am I not getting? Help?

It
also works if I say "IF oldrecord = 5" or "IF newrecord = 5." What am I not
getting? Both variables = 5, but not really? Help?
 
You might be better off asking in an ASP or VBScript forum, as I suspect
your problem may be related to the lack of strongly-typed variables in
VBScript. As I understand it, in VBScript all variables are Variant, right?
So your 'newrecord', being the value of a text box, is a Variant of sub-type
String, while your 'oldrecord', being retrieved from the database, is
probably a Variant of sub-type Long Integer. Put another way, newrecord =
the string "5", and oldrecord = the number 5, and while "5" = "5", and 5 =
5, "5" <> 5.

If I'm right, something like this might work if VBScript supports the CStr()
function ...

if CStr(oldrecord) = newrecord then

If VBScript doesn't support the CStr() function, then possibly something
like this might work ...

if (oldrecord & "") = newrecord then
 
OMG. They both work! Just shows that I don't really know WTHeck I'm doing. So
glad you do! Thanks a million!
 
Back
Top