Subscript out of range message

I'm new to ASP, and I need help with finding the values of the array. What I'm trying to do is have 5 lines for the users to enter in information of their sites, then if they need to add more sites, they can click a button to add another 5 lines keeping the 5 lines items they have already answered.  I keep getting this error:

Microsoft VBScript runtime (0x800A0009)
Subscript out of range: '[number: 0]'
On this line:
cCLcheck = arr_cCLcheck(x-1)

Here's my code....
 

<%
Dim z
z=5
%>

<%
Dim x, cCLcheck, cCPcheck, site_name, band_width
Dim arr_cCLcheck, arr_cCPcheck, arr_cSiteName
Dim arr_cBandWidth, z              

arr_cCLcheck = split(request.form("cCLcheck"), ", ")
arr_cCPcheck = split(request.form("cCPcheck"), ", ")
arr_cSiteName = split(request.form("site_name"), ", ")
arr_cBandWidth = split(request.form("band_width"), ", ")
                                             
For x = 1 to z
If (z > 6) And (x > 0 AND x <= (z-6)) Then
                                       
        cCLcheck = arr_cCLcheck(x-1)
        cCPcheck = arr_cCPcheck(x-1)
        site_name = arr_cSiteName(x-1)
        band_width = arr_cBandWidth(x-1)
                             
Else                                   cCLcheck = ""                         cCPcheck = ""
        site_name = ""
     band_width = ""
End If
%>
<tr>
<td><span style='font-size:10.0pt;font-family:Geneva, Arial, sans-serif'>
<INPUT type="checkbox" name="cCLcheck" value="ON"<%=checkedValue("cCLcheck")%>>CL
<br>
<INPUT type="checkbox" name="cCPcheck" value="ON"<%=checkedValue("cCPcheck")%>>CP
</span></td>
<td> <INPUT class=textfield type="text" name="site_name" value=<%=site_name%>></td>
<td> <INPUT class=textfield type="text" name="band_width" value=<%=band_width%>></td>
</td>
</tr>
<%
Next
%>
<tr>
<td colspan=5> <input class=textfield type="button" name="AddMore" value="Add More Sites" onClick="sendInfo(1);">
</td>
</tr>

Please help....
ssaville24Asked:
Who is Participating?
 
gladxmlConnect With a Mentor Commented:
ssaville24,

MAybe Maybe you cna try this approach... using a dropdown and not the button... Also it is much more effecient if you will save the the data in a database... Becasue in your process the only action that you can do is to resubmit the form...

The error that you have occurs when you get data that is not on the array collection or the number that you wnat to get exceed the maximum number of the array collection..


Here is the code... Basically the form has a dynamic input... It is somewhat similar to what you want to accomplish...

====save this as ssaville24-b.asp

 <% Response.Buffer = "True"%>
<%

if request.form("form_ok") = "True" then
for y = 1 to request.form("User")

if request.form("EmpNo" & y)  <> "" then
      
      if request.form("EmpName" & y)  <> "" then

'you can put your saving of record here

            tmpEmployeeNo = request.form("EmpNo" & y)  & "<br>"
            tmpEmployeeName = request.form("EmpName" & y)
            
response.write tmpEmployeeNo
response.write tmpEmployeeName
      end if

end if

next

'do a resposnse.redirect to other page....

end if
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<script language="Javascript">
<!--
function Passthis()
{
document.activeform.form_ok.value = "False";
activeform.submit();
}
function Savethis()
{
document.activeform.form_ok.value = "True";
activeform.submit();
}

// -->
</script>
<table width="784" height="665" border="0" cellspacing="0" cellpadding="30" align="center">
  <tr>
      <td valign="top" bgcolor="ffffff">
          <form name="activeform" method="POST" action="ssaville24-b.asp">
            <input type="hidden" name="form_ok" value="False">
        <table width="600" border="0" cellspacing="0" cellpadding="0" align="center">

          <tr>
               <td width="31%" valign="top" align="center">
              <div align="center"></div>
            </td>

            <td valign="middle" width="69%">
                          <div align="center"></div>
            <div align="center"></div>
</td>
          </tr>
          <tr>
            <td colspan="2" valign="top">
              <div align="left"></div>
              <table width="600" border="0" cellspacing="0" cellpadding="0" align="center">
                <tr>
                  <td height="1" bgcolor="#333333"> <img src="images/3.gif" width="1" height="1"></td>
                </tr>
              </table>
            </td>
          </tr>
          <tr>
              <td colspan="2" valign="top">
                <p align="center"><br>
                </p>
               
              <table width=600 align="center" valign="top" cellpadding="0" bgcolor="#FFFFFF" cellspacing="1" border="0">
                <tr bgcolor="#333333" > 
                  <td align="left" colspan="3" class="h2" height="30">@</td>
                </tr>
                <tr bgcolor="#cccccc" > 
                  <td align="left" class="h2" height="30">@</td>
                  <td align="left" class="h2" height="30" colspan="2">You can
                    input up to 20 PC User. </td>
                </tr>
                <tr bgcolor="#cccccc" > 
                  <td align="left" class="h2" height="30">
                    <div align="left">:</div>
                  </td>
                  <td align="left" class="h2" height="30">Please select number
                    of PC user</td>
                  <td align="left" class="h2" height="30">
                          <!--
                    <select class="content" onchange="doSel(this)" name="User">
                      <%for i = 1 to 20%>
                      <option value="location.href='PC5.asp?PCuser=<%=i%>'" <%if cint(request.querystring("PCuser"))=i then response.write "selected"%>><%=i%></option>
                      <%next%>
                    </select>
                    <input type="hidden" name="UserNum" value="<%=request.querystring("PCuser")%>">
                              -->
                    <select class="content" onchange="Passthis()" name="User">
                      <%for i = 1 to 20%>
                      <option value="<%=i%>" <%if cint(request.form("User"))=i then response.write "selected"%>><%=i%></option>
                      <%next%>
                    </select>
                  </td>
                </tr>
                <tr bgcolor="#cccccc" > 
                  <td align="left" width="10%" class="h2" height="30">@</td>
                  <td class="content" height="30">@</td>
                  <td width="56%" class="content" height="30">@</td>
                </tr>
                <tr bgcolor="#cccccc" > 
                  <td align="left" width="10%" class="h2" height="30">@</td>
                  <td class="content" height="30">Employee Number</td>
                  <td width="56%" class="content" height="30">Employee Name</td>
                </tr>
                        <%if request.form("User") <> "" then                        
                        for x = 1 to request.form("User")%>
                <tr bgcolor="#cccccc" > 
                  <td align="left" width="10%" class="h2" height="30">
                    <div align="center"><%=x%></div>
                  </td>
                  <td class="content" height="30">
                    <input type="text" name="<%="EmpNo" & x%>" value="<%=request.form("EmpNo" & x)%>">
                  </td>
                  <td width="56%" class="content" height="30">
                    <input type="text" name="<%="EmpName" & x%>" size="45" value="<%=request.form("EmpName" & x)%>">
                  </td>
                </tr>
                <%next
                        else%>
                <tr bgcolor="#cccccc" > 
                  <td align="left" width="10%" class="h2" height="30">
                    <div align="center"><%="1"%></div>
                  </td>
                  <td class="content" height="30">
                    <input type="text" name="<%="EmpNo1"%>" value="<%=request.form("EmpNo1")%>">
                  </td>
                  <td width="56%" class="content" height="30">
                    <input type="text" name="<%="EmpName1"%>" size="45" value="<%=request.form("EmpName1")%>">
                  </td>
                </tr>
                        <%end if%>
                <tr bgcolor="#333333" > 
                  <td align="left" colspan="3" class="h2" height="30">
                    <div align="center"></div>
                  </td>
                </tr>
              </table>
                        <table width="90%" border="0" cellspacing="0" cellpadding="4" align="center">
                <tr>
                  <td>@</td>
                </tr>
              </table>


              <table width="66%" border="0" cellspacing="0" cellpadding="5" align="center">
                  <tr>
                    <td >
                      <div align="center">
                        <input type="Submit" name="SubmitP5" value="Save & Next" Onclick="Savethis();">
                      </div>
                      <div align="left"> </div>
                    </td>
                  </tr>
                </table>

            </td>
          </tr>

        </table>
       </form>
      </td>
    </tr>
  </table>


Hope this help...

Happy programming...


0
 
ssaville24Author Commented:
Typo.  It's 6 lines instead of 5 lines...

<%
Dim z
z=6
%>
0
 
chisholmdCommented:
What happens if the user doesn't select the checkbox for cCLcheck ?  Empty array will fail on arr_cCLcheck(0)

By offering a check box I assume that it is OK for a user to leave it unchecked.  un-checked checkbox don't return a name-value pair.  Its a big drag.

so you might end up with returned fields like this:

cCLcheck "on,on"
cCPcheck "on,on,on"
cSiteName "redsite,bluesite,oldsite,newsite"
cBandWidth "bg,little,trickle,na"

You need to find another way of tracking them.  I think what I have done in the past is made hidden fields for each checkbox the hidden fields value is the name of the associated checkbox. Then for each hidden field I check for the existance of the checkbox. No checkbox means unchecked.

<input type=checkbox name=chk1 value="on">
<input type=hidden name=hidchk value="chk1">

then:
for counter = 1 to request.form("hidcheck").count
  if request.form(request.form("hidchk")(counter)) <> "" then
   'Its checked
  else
   'its unchecked
  end if
next


Also I would try is instead of setting z to 5 set z = ubound(arr..) then do your loop as 0 to z.

Make sense?







0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mgfranzCommented:
Try this;

cCLcheck = arr_cCLcheck(x) -1

But there are some fundemental flaws in your code, first remember that the Array is going to start at "0" unless you set your Option Base = 1.

arr_cCLcheck = split(request.form("cCLcheck"), ", ")

So the first arr_cCLcheck() is going to be "0"

So if you really wanted to start at "0"
                                           
For x = 0 to 5 ' or z

If (z > 6) And (x > 0 AND x <= (z-6)) Then '??? what the?  z is already set to 5


Try this;

arr_cCLcheck = split(request.form("cCLcheck"), ", ")
arr_cCPcheck = split(request.form("cCPcheck"), ", ")
arr_cSiteName = split(request.form("site_name"), ", ")
arr_cBandWidth = split(request.form("band_width"), ", ")
                                           
For x = 0 to z
' What you really should do is UBound() the array unless you are sure it is going to be more than 6
If x => 0 AND (x <= z) Then
                                     
       cCLcheck = arr_cCLcheck(x)
       cCPcheck = arr_cCPcheck(x)
       site_name = arr_cSiteName(x)
       band_width = arr_cBandWidth(x)
                             
Else                                   cCLcheck = ""                         cCPcheck = ""
       site_name = ""
    band_width = ""
End If


0
 
chisholmdCommented:
mqfranz: I think you need a day off dude. I have seen good answers from you in the past but today your a little off.

The fellow was using:
   cCLcheck = arr_cCLcheck(x-1)
because he is doing a for..next from 1 to 6
which as you say should be 0 to 5. So his notiation is correct.

This:
cCLcheck = arr_cCLcheck(x) -1

would be like saying "on-1" which obviously doesn't make any sense.

Perhaps you should slow down a bit today so that you don't send people on wild goose chases.

dave
0
 
ssaville24Author Commented:
chisholmd

I have this function in my code to check for the value of the checkbox...

<%
Function checkedValue(bTemp)
     If len(request.form(bTemp)) > 0 Then
          checkedValue = " Checked"
     Else
          checkedValue = ""
     End If
End Function
%>
0
 
mgfranzCommented:
Sorry, didn't see the input type part... :-/

INPUT type="checkbox" name="cCLcheck" value="ON"<%=checkedValue("cCLcheck")%

I get on the Array bandwagon to easily.
0
 
chisholmdCommented:
ssaville24: understood.  

But did you understand what I was saying about unchecked checkboxes not returning a name/value pair?  What it means is that unless ALL the checkboxes of the same name are checked your resulting arrays will be different lengths, but your treating them as all the same length in your for next.

dave
0
 
ssaville24Author Commented:
chisholmd

yes i understand it completely, but you confused me with the hidden input boxes idea.
0
 
CleanupPingCommented:
ssaville24:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
GaryCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Answer by gladxml

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
GaryC123
EE Cleanup Volunteer
0
All Courses

From novice to tech pro — start learning today.