2016-05-10 10 views
1

Ich schreibe Code für String-Suche in klassischen asp, aber es zeigen Fehler. Zum Beispiel ist, wennbekomme Fehler "Index außerhalb des Bereichs Fehler" in ASP classic

my name is lucky from earth 

Ich erhalte diesen Fehler

Microsoft VBScript-Laufzeitfehler '800a0009'
Index außerhalb des zulässigen Bereichs auf der Suche schreiben: '6'
/bdn6/prod_search.asp 68, Zeile

wo Leitung 68 ist:

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

mein Code wie unten angegeben:

<% 
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) 
%> 

Bitte helfen Sie mir, dieses Problem zu lösen.

Antwort

1

Read etwas über Array Variables:

Dim A(10) 

Obwohl die Zahl in den Klammern gezeigt ist 10 alle Arrays in VBScript sind Null basierende, so Dieses Array enthält tatsächlich 11 Elemente. In einem nullbasierten Array ist die Anzahl der Array-Elemente immer die in Klammern angegebene Zahl plus eins. Diese Art von Array ist genannt eine feste Größe Array.

Split Function

Gibt ein Null basierendes, eindimensionales Array eine spezifizierte Anzahl von Teilketten enthält.

UBound Function

Gibt den größten verfügbaren Index für die angegebene Dimension ein Array.

Deshalb verwenden entweder

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

oder (Beharren auf 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 

oder (Beharren auf 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 
Verwandte Themen