lokesh purohit
lokesh purohit

Reputation: 179

getting error "Subscript out of range error" in asp classic

I am writing code for string search in classic asp but it show error. For example if is write in search

my name is lucky from earth

I get this error

Microsoft VBScript runtime error '800a0009'
Subscript out of range: '6'
/bdn6/prod_search.asp, line 68

where line 68 is :

SWord = SWord & " " &  Trim(arrKeyWords(j))

my code is as below given:

<%
Dim SearchWord, arrKeyWords, arrQry, MainQty, FinalQty, MergeQry, WhereCon, Cnt, tsearch, i, j
SearchWord = trim(request("searcha"))
arrKeyWords = Split(SearchWord ," ") 
Cnt = Ubound(arrKeyWords) + 1
%>

<%
dim Qry, SWord, NLWord, TableName, LastIndex
MainQty = "select a.rProd_name, r_id "
TableName = "from reseller_prod a, brand e, V_brand f, V_modal g Where a.rprod_vbrand=f.Vb_Id and f.vb_active=0 and a.rprod_vmodel=g.Vm_id and g.Vm_active=0 and a.rProd_brand=e.Brand_id and e.brand_active=0 and a.rProd_price <> 0 and a.rProd_price is not Null and a.rprod_nowallowd=0 and a.r_id in(select s_usrid from Reseller where S_approval=0) and a.r_id in(select usr_id from usr where Usr_Active=0)"
NLWord = ""
For i = 0 To Cnt
SWord = ""
For j = 0 To ((Cnt) - i)

SWord = SWord & " " &  Trim(arrKeyWords(j))  'getting error on this line: Subscript out of range

Next
WhereCon = WhereCon & " And (a.rProd_name like '%" & Trim(SWord) & "%' or f.vb_name like '%" & Trim(SWord) & "%' or g.Vm_modal like '%" & Trim(SWord) & "%')"
Qry = MainQty & ", " & (i + 1) & " as SortRecord " & TableName & " " & WhereCon
LastIndex = i + 1
Qry = Qry & NLWord
NLWord = NLWord & " And (a.rProd_name not like '%" & Trim(SWord) & "%' And  f.vb_name not like '%" & Trim(SWord) & "%' And g.Vm_modal not like '%" & Trim(SWord) & "%')"
FinalQty = FinalQty & Qry & " UNION "   

WhereCon = ""
Qry = ""

Next

FinalQty = left(FinalQty, (Len(FinalQty) - 6))

MergeQry = FinalQty
FinalQty = ""

 MainQty = "select a.Prod_name, '' as r_id "
 TableName = "from product a, brand e, V_brand f, V_modal g Where a.prod_vbrand=f.Vb_Id and f.vb_active=0 and a.prod_vmodel=g.Vm_id and g.Vm_active=0 and a.Prod_brand=e.Brand_id and e.brand_active=0 and a.prod_active=0 and a.Prod_price <> 0 and a.Prod_price is not Null"

 NLWord = ""
 For i = 0 To Cnt

 SWord = ""
 For j = 0 To ((Cnt) - i)

 SWord = SWord & " " &  Trim(arrKeyWords(j))
 Next
WhereCon = WhereCon & " And (a.Prod_name like '%" & Trim(SWord) & "%' or a.prod_keyword like '%" & Trim(SWord) & "%' or f.vb_name like '%" & Trim(SWord) & "%' or g.Vm_modal like '%" & Trim(SWord) & "%')"
Qry = MainQty & ", " & (LastIndex + i + 1) & " as SortRecord " & TableName & " " & WhereCon
Qry = Qry & NLWord
NLWord = NLWord & " And (a.Prod_name not like '%" & Trim(SWord) & "%' and a.prod_keyword not like '%" & Trim(SWord) & "%' And f.vb_name not like '%" & Trim(SWord) & "%' And g.Vm_modal not like '%" & Trim(SWord) & "%')"
FinalQty = FinalQty & Qry & " UNION "   

 WhereCon = ""
Qry = ""

Next
FinalQty = left(FinalQty, (Len(FinalQty) - 6))
FinalQty = FinalQty & "Order By SortRecord"

MergeQry = MergeQry & " UNION " & FinalQty

response.Write(MergeQry)
 %>

Please help me to resolve this issue.

Upvotes: 1

Views: 1636

Answers (1)

JosefZ
JosefZ

Reputation: 30123

Read something about Array Variables:

Dim A(10)

Although the number shown in the parentheses is 10, all arrays in VBScript are zero-based, so this array actually contains 11 elements. In a zero-based array, the number of array elements is always the number shown in parentheses plus one. This kind of array is called a fixed-size array.

Split Function

Returns a zero-based, one-dimensional array containing a specified number of substrings.

UBound Function

Returns the largest available subscript for the indicated dimension of an array.

Therefore, use either

Cnt = Ubound(arrKeyWords)  ''' instead of Cnt = Ubound(arrKeyWords) + 1

or (insisting upon Cnt = Ubound(arrKeyWords) + 1)

For i = 0 To Ubound(arrKeyWords)
    SWord = ""
    For j = 0 To (Ubound(arrKeyWords) - i)
        SWord = SWord & " " &  Trim(arrKeyWords(j))
    Next
    ''' … '''
Next

or (insisting upon Cnt = Ubound(arrKeyWords) + 1)

For i = 0 To cnt -1
    SWord = ""
    For j = 0 To (cnt - 1 - i)
        SWord = SWord & " " &  Trim(arrKeyWords(j))
    Next
    ''' … '''
Next

Upvotes: 1

Related Questions