for x =...next, how to assign variable with the X value?

  • Thread starter news.cablevision.qc.ca
  • Start date
N

news.cablevision.qc.ca

Hi!

I want, using a form, enter a number and then, read that number of records
in a database and store information in variables. The only way I see to do
this is to do a FOR...NEXT to the number entered in the form, and then
include the database reading into it.

THE PROBLEM:

<%
for x = 1 to [Selected Number]
mySQL = "SELECT * FROM table "
set ObjectRecordset=ObjetConnection.execute(mySQL)
FieldValue = ObjectRecordset.fields("field")
next
%>

With this example, let's say the [Selected Number] is 3, the script will
read the first 3 lines but the variable FieldValue will be overwritten at
each pass of the FOR...NEXT...

So, I would need to use FieldValue1, FieldValue2, FieldValue3. As the
number entered can be anything, I cannot use static variables. I would need
to use the variable FieldValue followed by the value of x...
FieldValue[x]
Then the result should be FieldValue1, FieldValue2, etc... until the number
entered is reached.

Is anybody can give me the way to do this?

Thanks!!!
Andrew
 
A

Aaron [SQL Server MVP]

Have you thought about using an array?

dim FieldValues([Selected Number])
for x = 1 to ...
... are you REALLY running a database query in every iteration in this
loop????????
FieldValues(x) = whatever
next
 
J

Jeff Cochran

Hi!

I want, using a form, enter a number and then, read that number of records
in a database and store information in variables. The only way I see to do
this is to do a FOR...NEXT to the number entered in the form, and then
include the database reading into it.

THE PROBLEM:

<%
for x = 1 to [Selected Number]
mySQL = "SELECT * FROM table "
set ObjectRecordset=ObjetConnection.execute(mySQL)
FieldValue = ObjectRecordset.fields("field")
next
%>

With this example, let's say the [Selected Number] is 3, the script will
read the first 3 lines but the variable FieldValue will be overwritten at
each pass of the FOR...NEXT...

So, I would need to use FieldValue1, FieldValue2, FieldValue3. As the
number entered can be anything, I cannot use static variables. I would need
to use the variable FieldValue followed by the value of x...
FieldValue[x]
Then the result should be FieldValue1, FieldValue2, etc... until the number
entered is reached.

Is anybody can give me the way to do this?

Bleh! Return only relevant records, you can use the TOP query in some
databases, or loop through the first records of your recordset.
Whatever you do, issuing the same query three times in a loop is
wasteful. Basically, you query the dataabse to return every single
record, then select a single value out of all those, then repeat two
more times. Learn to use a WHERE clause.

Jeff
 

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,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top