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

Force an array to accept variable in declaration

Status
Not open for further replies.

lazyrunner50

Programmer
Joined
Jul 30, 2004
Messages
63
Location
US
I am trying to declare an array given the number of results in the resultset, but whenever I put any sort of variable in the declaration of the array, it gives me this error:

Code:
Microsoft VBScript compilation error '800a0402' 

Expected integer constant

I've tried numerous ways to do this, but none of them work:
Code:
Dim imageArray(result.RecordCount)
There are two problems with this one. First - when I printed out result.RecordCount it gave me -1 even though I was able to loop through the recordset and print out all the values. Second, it had the problem with the variable in the declaration

Code:
if not result.EOF then
while not result.EOF
i=i+1
result.movenext()
wend
DIM imageArray(i)
Got the same error as the first method, though when I printed out i, I got 88...

Code:
const someVar=88
DIM imageArray(someVar)
Even tried this, but it didn't work.
 
the -1 problem can be solved by using a clientside cursor...the array problem can be solved by doing this:
Code:
Dim imageArray()
Dim intCount

intCount = result.RecordCount

ReDim imageArray(intCount)
 
dim myArr

set myArr = rs.getRows()

numRecs = uBOund(myArr,2) + 1

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
I'm sorry,I guess that I wasn't paying attention. What exactly are you using the array for?

dim myArr(), rsArr
set rsArr = rs.getRows()
redim myArr(uBOund(rsArr,2) + 1)


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
I am trying to create a slideshow with images from my vacation. I am storing the image name along with an auto incremented column in the database. Since it is fairly involved, it might be easier to do this in pseudocode

Code:
If Array exists in Session Then <--This is for each successive page load
Get the array from Session object
Parse it (I saw in another faq that arrays are passed as comma-delimited strings)
Get indexNumber from Session
If indexNumber <= array length Then  <-- Make sure the end of the array has not been passed
<img src='<%=array(indexNumber)& '.jpg'%>'  <--Display image
sleep 10
indexNumber++
Response.Redirect("theCurrentPage.asp")
End If
Else <-- This is executed the first time the page loads
Connect to DB and get recordset
Put recordset into array
indexNumber=0
<img src='<%=array(indexNumber)& '.jpg'%>'  <--Display image
sleep 10
indexNumber++
Put array into session
Put index number into session
Response.Redirect("theCurrentPage.asp")
End If
 
Getting this message:

Code:
Microsoft VBScript runtime error '800a01a8' 

Object required 

/India/SlideShowPage2Backup.asp, line 22

This is line 22:

Code:
Set rsArr = result.getRows()

...and this is the declaration of result
Code:
sql = "SELECT pictureName, id FROM india_pics WHERE picType='" & request("cityAbbr") & "' ORDER BY id"
Set result = Conn.execute(sql)
 
You dont need a SET if you are assinging an array... only use SET for object assignments.
 
This still doesn't work!!

I've tried both of these:
Code:
Dim myArray(120)

This declaration above works, but the one below doesn't:

Code:
Dim myArray(), rsArr
rsArr = result.getRows()
Redim myArray(uBOund(rsArr,2) + 1)

However, I'm not getting the object required error from before (thanks Sheco). It is simply not printing anything
 
well try doing a response.write after you redim to see what happened... something like:

Dim myArray(), rsArr()
rsArr = result.getRows()
Redim myArray(uBOund(rsArr,2) + 1)
Response.Write uBound(myArray)
 
Woo hoo! Cool! I finally got it working. Turns out after all this
Code:
rsArr = result.getRows()
Redim myArray(uBOund(rsArr,2) + 1)
The recordset is at the end, so when I went to do this:
Code:
while not result.EOF
myArray(i) = result("pictureName")
i=i+1
result.movenext()
wend
It never executed...

Solved that by doing a MoveFirst on the recordset. Thanks everyone. Now, for the remaining problems on the site...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top