Link to home
Start Free TrialLog in
Avatar of XK8ER
XK8ERFlag for United States of America

asked on

error on code

hello there,
when I try to remove duplicates from a listview with more than 3k records in it.. I get an error

run time error 35600
index out of bound

the line bellow with >> is the one that gets yellow
Dim lRet As ListItem, strTemp As String, intCnt As Integer
    intCnt = 0
    
    Do While intCnt <= lvwMain.ListItems.Count - 1
        intCnt = intCnt + 1
        strTemp = lvwMain.ListItems.Item(intCnt).SubItem(1)
        Do
>>          lvwMain.ListItems.Item(intCnt).SubItems(1) = ""          
            Set lRet = lvwMain.FindItem(strTemp, lvwSubitem)
            
            If Not lRet Is Nothing Then
                lvwMain.ListItems.Remove (lRet.Index)
            End If
 
        Loop While Not lRet Is Nothing
        
        lvwMain.ListItems.Item(intCnt).SubItem(1) = strTemp
        DoEvents            
    Loop

Open in new window

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

try put intCnt = intCnt + 1 on bottom before the "loop":
Dim lRet As ListItem, strTemp As String, intCnt As Integer
    intCnt = 0
    
    Do While intCnt <= lvwMain.ListItems.Count - 1
        
        strTemp = lvwMain.ListItems.Item(intCnt).SubItem(1)
        Do
          lvwMain.ListItems.Item(intCnt).SubItems(1) = ""          
            Set lRet = lvwMain.FindItem(strTemp, lvwSubitem)
            
            If Not lRet Is Nothing Then
                lvwMain.ListItems.Remove (lRet.Index)
            End If
 
        Loop While Not lRet Is Nothing
        
        lvwMain.ListItems.Item(intCnt).SubItem(1) = strTemp
 
intCnt = intCnt + 1
 
        DoEvents            
    Loop

Open in new window

Avatar of XK8ER

ASKER

now with your code I get an error in the first strTemp = lvwMain.ListItems.Item(intCnt).SubItems
try change:

strTemp = lvwMain.ListItems.Item(intCnt).SubItem(1)


to:

strTemp = lvwMain.ListItems.Item(intCnt).SubItems(1)
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of XK8ER

ASKER

this is what I have so far.. and it works pefectly fine with about 500 records then when I import 4k I get an error in the >> marked line
Private Sub RemoveDups(strNum As Integer)
    Dim lRet As ListItem, strTemp As String, intCnt As Long
    intCnt = 1
    
    Do While intCnt <= lvwMain.ListItems.Count
        strTemp = lvwMain.ListItems.Item(intCnt).SubItems(strNum)
        Do
>>            lvwMain.ListItems.Item(intCnt).SubItems(strNum) = ""
            Set lRet = lvwMain.FindItem(strTemp, lvwSubItem)
    
            If Not lRet Is Nothing Then
                lvwMain.ListItems.Remove (lRet.Index)
            End If
        Loop While Not lRet Is Nothing
        
        lvwMain.ListItems.Item(intCnt).SubItems(strNum) = strTemp
        intCnt = intCnt + 1
        DoEvents
    Loop
End Sub

Open in new window

>>and it works pefectly fine with about 500 records then when I import 4k I get an error in the >> marked line

so what is the value of "strNum" here?
Avatar of XK8ER

ASKER

im using it like this

Call RemoveDups(1)
seems run fine for me, what error message you get here?
Avatar of XK8ER

ASKER

this error

run time error 35600
index out of bound
Check this out on possibly to resolve error "run time error 35600 index out of bound":
http://support.microsoft.com/kb/q167122/
good afternoon!
yes u will receive that kind of error because, ineteger holds the listcount of the listview and when
you remove the duplicate, the integer still holds the initial listcount..
that is why i add "On error resume next on the code"...

I''ve pasted this code earlier on your question, unfornatunately you accepted another comments..lol..
anyway try this code...


On Error Resume Next
Dim lRet As ListItem, strTemp As String, intCnt As Integer
    intCnt = 0
   
    Do While intCnt <= lsvWork.ListItems.Count - 1
        intCnt = intCnt + 1
        strTemp = lsvWork.ListItems.Item(intCnt).SubItems(1)
        Do
            Set lRet = lsvWork.FindItem(strTemp, lvwSubItem, intCnt + 1, 0)
           
            If Not lRet Is Nothing Then
                lsvWork.ListItems.Remove (lRet.Index)
            End If
 
        Loop While Not lRet Is Nothing
       
        DoEvents
        Loop


i tried it and it works fine with me..


game-master