?
Solved

ASP split array - Subscript out of range

Posted on 2004-10-11
5
Medium Priority
?
1,514 Views
Last Modified: 2011-10-03
This code has been working for nearly a year and a half and all of a sudden, one of my users started getting the "Subscript out of range: '0'" message when he tried to close his open item. From this screen, users have the optin to close several items at once and to enter a comment at the same time. The only thing I can think may be crashing it now is the fact that there is only ONE item on this screen to close and we are trying to leave the "Comment" field empty. When I type anything into the "Comment" field and close my item, I get an Exception error, but the item does close. However, his employee number and last name are inserted into the "Closed By" field, where only his employee number should be.

As you can see from the code, I try to trap if the "Comment" field is empty, but I think that that field being empty may be what is throwing off the array:

-----------------------------------------------------------------------------
     Case "CloseItems"
            
     varTour = Request.Form("txtTourID")
     varClosedBy = Request.Form("txtClosedBy")

     Dim MyArrayCloseDate, MyArrayCloseID, MyArrayComment, MyArrayItemNo
     MyArrayCloseDate = split(Trim(Request.Form("txtCloseDate")),",")
     MyArrayCloseID = split(Request.Form("chkClose"),",")
     MyArrayComment = split(Trim(Request.Form("txtComment")),",")
     MyArrayItemNo = split(Trim(Request.Form("txtItemNo")),",")

     If Request.Form("chkClose") <> "" and IsDate(MyArrayCloseDate(i)) Then
          For i = 0 to ubound(MyArrayCloseID,1)
               If Trim(MyArrayComment(i)) = "" Then
                    strUpdate = "UPDATE tblInspItem Set DateCompleted = '" & MyArrayCloseDate(i) & "', Comment = NULL, Status = 'Closed', ClosedBy = '" & varClosedBy& "' WHERE ItemNo = '" & MyArrayCloseID(i) & "'"
               Else
                    strUpdate = "UPDATE tblInspItem Set DateCompleted = '" & MyArrayCloseDate(i) & "', Comment = '" & Trim(MyArrayComment(i)) & "', Status = 'Closed', ClosedBy = '" & varClosedBy& "' WHERE ItemNo = '" & MyArrayCloseID(i) & "'"
               End If
               objConn.Execute strUpdate
          Next
          Session("sysMsg") = "<span class='TitleText'>" & "SELF-INSPECTION TOURS - <span class='TitleGreen'>CHECKED ITEMS CLOSED</span>" & "</span>"
          Session("sysMsg2")= "<a href='sit_main.asp' target='mainFrame'><img src='../imgs/main/btn_back.gif' border='0' align='absmiddle'><span class='TitleText'>BACK</span></a>"
     Else
          If Not(IsDate(MyArrayCloseDate(i))) Then
               ShowItems = False
               Session("sysMsg") = "<span class='TitleText'>" & "SELF-INSPECTION TOURS - <span class='TitleRed'>INVALID DATE(S)!</span>" & "</span>"
               Session("sysMsg2")= "<a href='sit_main.asp' target='mainFrame'><img src='../imgs/main/btn_back.gif' border='0' align='absmiddle'><span class='TitleText'>BACK</span></a>"
          Else
               If Request.Form("txtItemNo") <> "" Then
                    For i = 0 to ubound(MyArrayItemNo, 1)
                         If Request.Form("txtComment") <> "" Then      
                              strUpdate = "UPDATE tblInspItem Set Comment = '" & MyArrayComment(i) & "' WHERE ItemNo = '" & MyArrayItemNo(i) & "'"
                              objConn.Execute strUpdate
                         Else
                              strUpdate = "UPDATE tblInspItem Set Comment = NULL WHERE ItemNo = '" & MyArrayItemNo(i) & "'"
                              objConn.Execute strUpdate
                         End If
                    Next
               End If      
                              
               Session("sysMsg") = "<span class='TitleText'>" & "SELF-INSPECTION TOURS - <span class='TitleGreen'>ALL COMMENTS UPDATED!</span>" & "</span>"
               Session("sysMsg2")= "<a href='sit_main.asp' target='mainFrame'><img src='../imgs/main/btn_back.gif' border='0' align='absmiddle'><span class='TitleText'>BACK</span></a>"
          End If
     End If
                  
     Set rsViewItems = objConn.Execute("SELECT tblInspection.TourNo, tblInspection.TourDate, tblInspItem.Comment, " &_
     "tblInspection.TourMonth, tblInspection.TourYear, tblInspItem.DateCompleted, tblInspItem.ItemNo, tblInspItem.ItemDesc, tblInspItem.Status, tblInspItem.InspID, " &_
     "tblInspItem.SubID, tblTour.Location, tblTour.RespEmp, tblEmployee.FirstName, tblEmployee.LastName, tblSubLocation.SubLocation " &_
     "FROM tblInspection " &_
     "LEFT JOIN tblTour ON tblInspection.TourNo = tblTour.TourID " &_
     "LEFT JOIN tblInspItem ON tblInspection.InspID = tblInspItem.InspID " &_
     "LEFT JOIN tblEmployee ON tblTour.RespEmp = tblEmployee.EmployeeID " &_
     "LEFT JOIN tblSubLocation ON tblInspItem.SubID = tblSubLocation.SubID " &_
     "WHERE tblInspection.TourNo = '" & varTour & "' AND Status = 'Open'" &_
     "ORDER BY tblInspItem.Status DESC, tblInspItem.ItemNo")
                              
     varRespEmp = rsViewItems("FirstName") & " " & rsViewItems("LastName")
      
     ShowItems = True
                        
     Title = "DISPLAYING ALL OPEN ITEMS FOR " & "<font color='#274f8e'>" & rsViewItems("Location") & "</font>"
-----------------------------------------------------------------------------

Any help would be most appreciative.

Thanks!
0
Comment
Question by:TammiCole
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12279131
As I am sure that you are aware, the subscript out of range error indicates that you are trying to access a non-existant array element. If there is a possibility that you don't have an array, perhaps you should use the isArray() command first before trying to access the array element.

FtB
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12279137
Also, if you look at this:

  If Request.Form("chkClose") <> "" and IsDate(MyArrayCloseDate(i)) Then
          For i = 0 to ubound(MyArrayCloseID,1)


it appears that you are trying to use i before it is put into the for loop?

FtB
0
 
LVL 15

Accepted Solution

by:
Thogek earned 2000 total points
ID: 12340851
> The only thing I can think may be crashing it now is the fact that there is only ONE
> item on this screen to close and we are trying to leave the "Comment" field empty

Assuming that your Comment field is txtComment...

    MyArrayComment = split(Trim(Request.Form("txtComment")),",")

then tries to split this value on comma-characters.  If the txtComment value is blank (empty string), then the split call will return an array of zero elements (see http://msdn.microsoft.com/library/en-us/script56/html/vsfctsplit.asp).  Which means that any attempt to call on

    MyArrayComment(i)

is likely to result in a "Subscript out of range" error.

If this is a possiblity, you may want to check for it in your code.  One (simplistic?) possibliity:

    If Request.Form("txtComment") = "" Then
        MyArrayComment = Null
    Else
        MyArrayComment = split(Trim(Request.Form("txtComment")),",")
    End If

and then, before any attempts to reference MyArrayComment(i), do a check on IsArray(MyArrayComment), as Fritz suggests above.
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12340862
Note that, in general, you appear to be splitting several different strings into arrays, and then assuming that all of those arrays will be of the same size.  Unless there is something going on in the population of those strings that will guarantee this, you'll want to code in some sort of verifications of this before attempting to use the resulting arrays, on the off-chance that they're not all the same size.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12341491
Uhm, you're welcome?

The isArray() and my comments about the placement were of no help whatsoever?

FtB
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question