Click to See Complete Forum and Search --> : Subscript out of range: '[number: 1]'


marko_one
03-27-2003, 06:04 PM
Hi All,
I am having a problem with an array and using it more than once through a loop. If I run it so that I response.end after the first operation it runs and sisplays the results from the database no problem, as soon as I take out the response.end I get the error as shown in the subject.

I realizethe code below is badly formed, originally I written it properly, but had to alter it bit by bit to see if i could fix the errors, but I am now sick to death of it and need someone to help me because I dont understand why it will run once ok but no more.

Thanks in advance

<%

'Databse Driver
dbDriver = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="

'path to the databases
dbPath = server.mappath("db") & "\"

strConn = dbDriver & dbPath & "Total.mdb"

sql = "SELECT * From apptData;"
dim oConn
dim oRS

set oConn = server.createobject("ADODB.Connection")
set oRS = server.createobject("ADODB.Recordset")

oConn.Open strConn '

oRS.Open sql, oConn

%>
<table border="1", cellpadding="5">
<tr bgcolor="skyblue">
<th>Address</th>
</tr>
<%
Dim dtaAddress
Dim strAddress
Dim add1
Dim add2
Dim add3
Dim add4
Dim add5
Dim i
i = 0
While Not oRS.EOF
%>
<tr>
<%
strAddress = split(Trim(oRS("Address")), "#")
add1 = strAddress(0)
add2 = strAddress(1)
add3 = strAddress(2)
add4 = strAddress(3)
add5 = strAddress(4)

'Tried erase and just plain leaving the array
Redim strAddress(5)

'Response.Write(add1 & "<BR>")
'Response.Write(add2 & "<BR>")
'Response.Write(add3 & "<BR>")
'Response.Write(add4 & "<BR>")
'Response.Write(add5 & "<BR>")
'Response.End
dtaAddress = ""

if add1 <> "" Then
dtaAddress = dtaAddress & add1 & "<br>"
End If

if add2 <> "" Then
dtaAddress = dtaAddress & add2 & "<br>"
End If

If add3 <> "" Then
dtaAddress = dtaAddress & add3 & "<br>"
End If

if add4 <> "" Then
dtaAddress = dtaAddress & add4 & "<br>"
End If

If add5 <> "" Then
dtaAddress = dtaAddress & add5
End If
'response.Write(dtaAddress & "#")
'response.end

%>
<td><%=dtaAddress%>&nbsp;</td>
</tr>

<%
'response.end

oRS.MoveNext
i = i + 1
'response.end
Wend

%>

</table>
<%
oRS.Close
set oRS=nothing
oConn.close
set oConn=nothing

%>

marko_one
03-28-2003, 03:25 AM
Thanks for the post:

Logic:
I have a form which is collecting customer details, name address, telephone number etc.
The forms address is composed of 5 parts, Address1, Address2, 3, 4, 5. These values are taken and concatenated together with a '#' sign to separate values and finally stored in a single field of the database called 'Address'.

When I want to display these values as an address in a table, I go through the process is splitting up the string of the 'Address' field and storing each of the separate values in an array (strAddress). I then go through the array and store each of the indexed values in variables, so add1 = strAddress(0), add2 = strAddress(1), 2, 3, 4. I then check the values of add1, 2, 3, 4, 5 to see if it is an empty string. If it is then no action is taken, If it is not an empty string them the value extracted from the array has a "<br>" attached to it so that when I display it in a table it will be formed like :

My street name
My area
My town
My county
My Country

the contents of add1, 2, 3, 4, 5 are stored in a string (dtaAddress) and so eventually the string variable will be like:

dtaAddress = My street name<br>My area<br>My town<br>My county<br>My Country

and so when this dtaAddress populates the table cell it will form it as above

If I do a 'response.end' before the 'rs.Movenext' It will only iterate through once which does work and gives the required results, but if I try and do it without the 'response.end' it fails with the fdollowing error:

Error Type:
Microsoft VBScript runtime (0x800A0009)
Subscript out of range: '[number: 1]'
/salesData/showApptData.asp, line 99


Line 98 add1 = strAddress(0)
Line 99 add2 = strAddress(1)

I know it loops through the database once and then fails on the second index of the array.



Any ideas?

svagelis
03-28-2003, 04:10 AM
I think this should work :

While Not oRS.EOF
Redim strAddress
strAddress = split(Trim(oRS("Address")), "#")
dtaAddress = ""
for i = 0 to ubound(strAddress)
dtaAddress = dtaAddress & strAddress(i) & "<br>"
next

oRS.MoveNext

Wend

%>


---------------------------

I d use instead one line of code to do all that

dtaAddress = Replace(Trim(oRS("Address")), '#', '<BR>', 1)


Hope this helps !!!

sartzetakis vagelis
Software Developer
Athens Greece

DaiWelsh
03-28-2003, 04:27 AM
Yes, I agree. the probblem with the original code is almost cetainly that your code assumes that all address values in the database will ahve 5 parts seperated by #

strAddress = split(Trim(oRS("Address")), "#")
add1 = strAddress(0)
add2 = strAddress(1)
add3 = strAddress(2)
add4 = strAddress(3)
add5 = strAddress(4)

If it does not (e.g. if address field was empty or 'a#b#c') then there would not be five elements in the array strAddress fter the split, which is why your code falls over. To verify this add the following

strAddress = split(Trim(oRS("Address")), "#")
response.write "[" & Trim(oRS("Address")) & "]<br>"
add1 = strAddress(0)
add2 = strAddress(1)
add3 = strAddress(2)
add4 = strAddress(3)
add5 = strAddress(4)

and you should see the actual value of address that is in the database.

Sorry, svagelis for following on your answer but I thought it might help them to understand why your code works and theirs doesn't ;)

HTH,

Da

marko_one
03-28-2003, 04:53 AM
Thanks for the reply DaiWelsh,
I have already taken this into consideration and did in fact test it. so I had in the field

test##test1#test2#test3

this pops up with

test 'strAddress(0)'
'strAddress(1)
test1 'strAddress(2)
test2 'strAddress(3)
test3 'strAddress(4)

so even though nothing is actually in strAddress(1) it still populates it with a "" I presume?


I'll try the code that svagelis has sent, thanks for you input

marko_one
03-28-2003, 05:48 AM
OK here is what I get using the code supplied. Ihave incuded the complete ASP page so maybe someone will test it?
it needs a database called Total contained in the db directory, with a table in it called apptData, that table needs a field called Address.

<%@language="VBScript"%>
<HTML>
<HEAD>
<title>Appointments Data</title>
<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</head>
<body>
<hr>
<h3 align=center>Appointment Database</h3>
<hr><p>

<%
dbDriver = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="
dbPath = server.mappath("db") & "\"
strConn = dbDriver & dbPath & "Total.mdb"
sql = "SELECT * From apptData;"
dim oConn
dim oRS
set oConn = server.createobject("ADODB.Connection")
set oRS = server.createobject("ADODB.Recordset")
oConn.Open strConn
oRS.Open sql, oConn
%>
<table border="1", cellpadding="5" ID="Table1">
<tr bgcolor="skyblue">
<th>Address</th>
</tr>

<%
Dim dtaAddress
Dim strAddress
Dim i
While Not oRS.EOF
Redim strAddress
strAddress = split(Trim(oRS("Address")), "#")
dtaAddress = ""
for i = 0 to ubound(strAddress)
dtaAddress = dtaAddress & strAddress(i) & "<br>"
next
%>

<tr>
<td><%=dtaAddress%>&nbsp;</td>
</tr>

<%
oRS.MoveNext
Wend
%>

</table>
<%
oRS.Close
set oRS=nothing
oConn.close
set oConn=nothing

%>
</body>
</html>


I get the following error

Error Type:
Microsoft VBScript compilation (0x800A03ED)
Expected '('
/salesData/tmp.asp, line 35, column 16
Redim strAddress
---------------^


Thanks

Marko

DaiWelsh
03-28-2003, 06:11 AM
I believe ReDim expects the size to redim to as a parameter, but I am not sure why you are redimming anyway, the split statement should set up the array for you.

From the original point, if the first record works and the second fails, then what data is in the first two records? This is why I suggested outputting the value of the db field before the code that fails.

HTH,

Dai

svagelis
03-31-2003, 05:21 AM
i avoid using split function and any function that generetes data out to arrays because i dont have full control over it.

Well your problem is a simple one. You can use the replace function as i posted before which replaces # with '<BR>'.