out of range?

1. Senior Member
Join Date
Dec 1969
Posts
538

out of range?

what does it mean when it says<BR>Subscript out of range: &#039;x&#039; <BR><BR>this is the code:<BR>ErrorMsg = ""<BR> x = 0<BR> For Each formField in Request.Form<BR> if Len(Request.Form(formField)) = 0 OR Request.Form(formField) = "title" then<BR> errorArray(x) = True<BR> ErrorMsg = ErrorMsg & Ucase(formField) & "<BR>"<BR> else<BR> errorArray(x) = False<BR> end if<BR> x = x + 1<BR><BR>-----<BR>i am trying to deal with blank fields in forms.<BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

It means DIM is wrong...

It means you are trying to store something into an element of an array (presumably errorArray? But since you didn&#039;t say what line the error is on, I have to guess, of course...that&#039;s a *HINT* for next time) where the array isn&#039;t big enough to hold that many elements.<BR><BR>So if you did:<BR><BR>Dim errorArray( 5 )<BR><BR>and then did:<BR><BR>x = 7<BR>errorArray(x) = anything<BR><BR>you&#039;d get an error because 7 is greater than 5.<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
538

RE: It means DIM is wrong...

here&#039;s the complete code:<BR><BR>&#060;%<BR>const numFields = 10<BR>dim errorArray()<BR>redim preserve errorArray(numFields)<BR><BR>if Request.Form("isSubmitted") ="yes" then<BR> ErrorMsg = ""<BR> x = 0<BR> For Each formField in Request.Form<BR> if Len(Request.Form(formField)) = 0 OR Request.Form(formField) = "title" then<BR> errorArray(x) = True<BR> ErrorMsg = ErrorMsg & Ucase(formField) & "<BR>"<BR> else<BR> &#039;Subscript out of range: &#039;x&#039; <BR> errorArray(x) = False<BR> end if<BR> x = x + 1<BR> Next<BR>End if <BR>%&#062;

4. Senior Member
Join Date
Dec 1969
Posts
96,118

So what did I say???

You dimensioned errorArray(10), effectively, didn&#039;t you?<BR><BR>So if you have more than 11 fields in that form, x will reach the value of 11 and Kablooey.<BR><BR>Why don&#039;t you add <BR>&nbsp; &nbsp; Response.Write "x is " & x & " when formField is " & formField & "&lt;BR&#062;"<BR>*just* after the FOR EACH statement in that code?<BR><BR>It will show you what is going on. That&#039;s called debugging. And it&#039;s your friend, honest.<BR><BR><BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
538

RE: So what did I say???

thank you.<BR><BR>i now realize where the bug is. It&#039;s counting the input type=hidden as part of the array because i have named it, and the formField is including it.<BR><BR>But I need to name it or the submit button to process the informations. The problem now is that when I accidentally don&#039;t fill a field say City. The field say Address above it is grabbed before City, and ASP would print out Address instead of City that is required to be filled in.<BR><BR>How can I resolve that.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

Use a field name prefix...

First of all, let me point out that it wouldn&#039;t hurt to simply do<BR><BR>Dim errorArray(100)<BR><BR>and make the array so big that you&#039;ll never get a subscript error.<BR><BR>But that would still mean that you&#039;d process fields you don&#039;t want to.<BR><BR>Okay, so put a prefix character in front of the name of every field that you want to process. Instead of &#060;INPUT Name="City"&#062;, use &#060;INPUT Name="#City"&#062;, let&#039;s say.<BR><BR>And now, in your loop, you do:<BR><BR>&#060;%<BR>For Each field In Request.Form<BR>&nbsp; &nbsp; prefix = Left(field,1) &#039; first character<BR>&nbsp; &nbsp; fldName = Mid(field,2) &#039; rest of field name<BR>&nbsp; &nbsp; If prefix = "#" Then<BR>&nbsp; &nbsp; &nbsp; &nbsp; ... process this field ...<BR>&nbsp; &nbsp; End If<BR>Next<BR>%&#062;<BR><BR>BUT... <BR><BR>But I hope you are aware that the fields you get using FOR EACH will *NOT* necessarily be in the order that they are on the form!!!!<BR><BR>See the ASPFAQs, category "Forms", for a way around this problem.<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
538

RE: Use a field name prefix...

thank you! i got that fixed.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•