1

I need to retrieve one specific value from a list of values stored in an array using an index position value.

This is the location reference coming from url parameter:

Dim currentorder     
Dim nextorderindex
Dim rowindex 
Dim iArray
Dim i

currentorder = Trim(Request.QueryString("order"))
rowindex = CINT(Trim(Request.QueryString("rowindex")))  'e.g. let's say this is a 4

SQLOrderList = "SELECT orderno" & _
                " FROM awd_order" & _
                " WHERE order_date >= '" & dtstart & " 00:00:00'" & _
                " AND order_date < '" & dtend & " 23:59:59'" & _                    
                " ORDER BY " & sortby

objRS.Open SQLOrderList, objConn

i = objRS.RecordCount  'e.g. this contains 7 records            

If Not rowindex >= (i - 1) Then     'e.g. 4 >= (7 - 1)
    nextorderindex = (rowindex + 1) 'e.g. then nextorderindex = 5
End If

'Get recordset to Array
iArray = objRS.GetRows()   

If nextorderindex > 0 Then    'e.g. nextorderindex is 5
    response.write iArray(nextorderindex)  'e.g. here is my issue, I get: subscript out of range error
Else
    response.write currentorder
End If
6
  • Your code is very confusing, you didnot define currentorder variable and using it in if condition and if next nextorderindex is > 0 then you are assigning 5 to it and if returned records are less then five it always produce index out of range Commented May 26, 2015 at 15:31
  • I added the declaration of current order, which is a parameter from query-string. I apologize for the confusing code, I am having difficulty understanding how to move in the array to obtain the next available order. If the rowindex value from url is 4, then my current order in the array has index 4, if i want to get the next order using index value, then I have to increment index 4 + 1 and get the string value from array that matches that index. Does that make sense? Commented May 26, 2015 at 15:38
  • i am redoing your code please wait Commented May 26, 2015 at 15:43
  • If it returns records that are less than 5, then there are no next available orders, and I should just stay/ print the current order Commented May 26, 2015 at 15:44
  • Just to improve this further, based on what saqibahmad has said - it would be better to include Option Explicit at the beginning of your code. This enforces the declaration of variables and prevents problems that you may get later on. Commented May 27, 2015 at 9:32

1 Answer 1

1
Dim currentorder     
Dim nextorderindex
Dim rowindex 
Dim iArray
Dim i

currentorder = Trim(Request.QueryString("order")) // e.g. 4
rowindex = CINT(Trim(Request.QueryString("rowindex")))  '' lets say this is a 4

SQLOrderList = "SELECT orderno" & _
            " FROM _order" & _
            " WHERE CAST(FLOOR(CAST(order_date AS FLOAT)) AS DATETIME)" & _
            " BETWEEN CAST('" & dtstart & "' AS DATETIME)" & _
            " AND CAST('" & dtend & "' AS DATETIME)" & _     
            " ORDER BY " & sortby    
objRS.Open SQLOrderList, objConn

i = objRS.RecordCount  ' this contains 7 records
if i > 0 then
If Not isNumeric(rowindex) Or rowindex = "" Then 
    rowindex = 0 
End If 
If rowindex < (i) Then  ' 4 >= (7 - 1)
    nextorderindex = (rowindex + 1) ' then nextorderindex = 5
else
    nextorderindex = 0
End If

'Get this to Array iArray = objRS.GetRows(i,0)
**** it return two dementinal array.

If nextorderindex > 0 Then 'nextorderindex is 5
    response.write iArray(0,nextorderindex) ' **here is my problem, I get     subscript out of range error**
Else
    response.write currentorder
End If
end if

GetRows return two dimensional array read this for details: http://www.w3schools.com/asp/met_rs_getrows.asp

i have not run this code but hope this will work if not do copy the error i will update the code. hope this will help

Sign up to request clarification or add additional context in comments.

4 Comments

Hi saqibahmad, thank you for your help. I ran it and it is working better than to what I have, but I noticed that the index value skips one in the array. If I have an array containing: A1G722(1),A1G723(2),A1G724(3),A1G725(4),A1G726(4),A1G727(4),A1G728(4), and then I pass" A1G724(3), the code will return A1G726(4). Is this due to the array being zero based? Many thanks.
I marked. To remedy this issue, would it be just to subtract 1 from the rowindex value passed in the URL(that value is not zero based)? e.g. rowindex = (CINT(Trim(Request.QueryString("rowindex"))) - 1)
you can do it in : If rowindex < (i) Then nextorderindex = (rowindex) donot increment it.
@saqibahmad: Option Explicit - very useful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.