if statement comparing variable

M

Michael Haas

I'm trying to use a variable of retrieved via request from a form to
select records.I'm pretty sure I am having a basic syntax error or
something else basic.
I retrieve the variable from a prevous form:
section_id = request("section_id")

I then retrieve some records and run the compare, if i use the actual
value it works:
if rsArticles("tbl_articles.section_id") = "1" then

however if I substitute the variable section_id which equals 1 , it
fails the compare and moves on to the else:
if rsArticles("tbl_articles.section_id") = section_id then

if I call the section_id value elsewhere it shows that it is set to 1,
so I know it's being passed, <%=section_id%> returns 1

What am I missing?
 
R

Roji. P. Thomas

Have you tried an explicit conversion like

if rsArticles("tbl_articles.section_id") = CInt(section_id )
 
B

Bob Barrows

Michael said:
I'm trying to use a variable of retrieved via request from a form to
select records.I'm pretty sure I am having a basic syntax error or
something else basic.
I retrieve the variable from a prevous form:
section_id = request("section_id")

I then retrieve some records and run the compare, if i use the actual
value it works:
if rsArticles("tbl_articles.section_id") = "1" then

This line is not giving you an error?? Do you have On Error Resume Next in
your code? Comment it out. You will see an error generated by this line. You
cannot refer to a recordset field using the table name like this. You can
only use:

a) the ordinal position of the field in the Fields collection:
rsArticles(1).value ' refers to second field in collection
b) the column name or column alias
rsArticles("section_id")

If you have more than one field called "section_id" in the results, then:

1) If both fields contain the same information - Stop writing inefficient
code! Your goal should be to bring as little data across the network as
possible. Using "Select *" defeats that goal. Don't bring the same
information across the network twice!

2) If the fields contain dissimilar information, then you need to assign
aliases to the columns so they will have different names in the recordset:
Select tbl_articles.section_id AS articles_section_id, ...
This will allow you to refer to it as
rsArticles("articles_section_id")

Your following description is unclear, and I suspect the results are being
clouded by the error in your reference to the recordset field.

however if I substitute the variable section_id which equals 1 , it
fails the compare and moves on to the else:
if rsArticles("tbl_articles.section_id") = section_id then

if I call the section_id value elsewhere it shows that it is set to 1,
so I know it's being passed, <%=section_id%> returns 1

What am I missing?


Bob Barrows
 
M

Mike Haas

if rsArticles("tbl_articles.section_id") = "1" then
This line is not giving you an error?? Do you have On Error Resume Next in
your code? Comment it out. You will see an error generated by this line. You
cannot refer to a recordset field using the table name like this.

no errors, i was just missing CInt to make the if work for me. all of
the sql
code is called from a sql.asp file, uses alot of joins. In a few areas
the code
uses the table name. I put this if statement in the results of the
original
designers code so that i could restrict which section to show.
Sorry for the confusion I caused you with my inefficient code (passing
the
section I wanted to view from a form and then restricting the display on
that
page to just the desired results). Yes, i could have rewrote the stored
sql
statement to select my desired section or created a new stored sql to do
so.
But since I already ne the very little amount of data contained in the
table and
how often this page would actually be used, I just wanted a quick fix.
I think the person who posted the fix to my delima figured out I was
trying to
put a work around in place to make something work, that's why you posted
a
one line fix to my problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,828
Members
47,374
Latest member
anuragag27

Latest Threads

Top