• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 755
  • Last Modified:

Array Problem - Microsoft VBScript runtime error '800a0009'

This is the code on a page where I am trying to delete a number or records form a database and images from a file on server.  

The first page displays all the images with a check box beside each image to select it you want to delete the image.  When this page is submitted it goes to a page where the id numbers of each record are taken and stored in an array, when looping through the code to delete the images I get the following error:

(First 3 lines - just to check code)

Upperbound = 1
IDno(0) = 36 Select * From Images WHERE id =36
IDno(1) = 37 Select * From Images WHERE id =37

Microsoft VBScript runtime error '800a0009'

Subscript out of range: '2'

/cp/gallerydelete.asp, line 24


The Code on the page:

<%
dim rs, cn, i, j

gallery=request.Form("Gallery")
idno=Request.Form("chk")
idnos=split(idno,", ")      ' stores the values in the arrays                  
checkcount=ubound(idnos)+1
response.write ("Upperbound = " & Ubound(idnos))


if not checkcount <1 then

set cn = server.createobject("ADODB.Connection")
cn.provider = application("strProvider")
cn.open application("strConDB")      

Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
for i = 0 to checkcount
response.write("<br>IDno(" & i & ") = " & idnos(i) & "  ")    <<<<<<<< line with error
strSQL="Select *  From Images WHERE id =" & idnos(i)
response.write(strSQL)
set rs = cn.execute(strSQL)
objFSO.DeleteFile Server.mapPath("/galleryimages/") & "\" & rs.fields("Imagename"), False
checkcount=checkcount+1
next
Set objFSO = Nothing
rs.close
set rs = nothing

sSQL = "DELETE from Images WHERE Id IN(0,0" & Request("chk") & ")"
cn.execute(sSQL)

cn.close
set cn = nothing



%>
0
harris9999
Asked:
harris9999
  • 3
1 Solution
 
fritz_the_blankCommented:
1) don't forget that arrays start at 0
2) to loop through all results, try:

for i = LBound(idnos) to UBound(idnos)
  'your code
next

FtB
0
 
fritz_the_blankCommented:
Now the reason you get your error is as follows--

First you do this:

checkcount=ubound(idnos)+1


and then later you do this:

for i = 0 to checkcount
   response.write("<br>IDno(" & i & ") = " & idnos(i) & "  ")    

By doing so, you are telling the script to move one item more through the array then actually exists, so of course you are going to get a subscript out of range error.

FtB
0
 
harris9999Author Commented:
Cheers
0
 
fritz_the_blankCommented:
Glad to have helped.

FtB
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now