Solved

ASP split array - Subscript out of range

Posted on 2004-10-11
5
1,486 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
  • 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 500 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now