Results 1 to 15 of 15

Thread: [RESOLVED] Still get values from checkbox

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    47

    Resolved [RESOLVED] Still get values from checkbox

    guys why is it that im having an error saying "INDEX OUT OF BOUNDS" here in this code? i just followed the same logic i learned here in this forums. here is the code..

    If ListView2.ListItems.Count = 0 Then
    MsgBox "Nothing to add.", vbCritical + vbApplicationModal, "Message"
    Exit Sub
    Else
    For nI = 1 To ListView2.ListItems.Count
    If ListView2.ListItems(nI).Checked = True Then

    Set rs = New ADODB.Recordset
    rs.Open "Select * from sendtbl where empid like '%" &ListView2.ListItems(nI).SubItems(4) & "%'", cn, 3, 2
    rs.Delete
    rs.Update
    rs.Close
    Set rs = Nothing

    Call load
    End If

    Next nI

    End If

    can someone help me please??

  2. #2
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Still get values from checkbox

    not totally sure, but try this as a first step.
    put the Set statements outside the For-Next loop
    remove the rs.delete statement because if you delete it, there is nothing to update

    EDIT, When you update, you also need to actually change a part of the record

  3. #3
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Still get values from checkbox

    I'm not sure but shouldn't it be:

    For nI = 1 To ListView2.ListItems.Count - 1

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Still get values from checkbox

    Yes it is here that is the problem
    Code:
    For nI = 1 To ListView2.ListItems.Count
    List are indexed starting at 0 so the last index is always 1 lower than the count. So if there are 5 items in the list Count returns 5 but the actual index numbers are 0,1,2,3,4 so when the loop gets to 5 you will get index out of bounds. You would also be skipping the first item in the list since your loop starts at 1
    Code:
    For nI = 0 To ListView2.ListItems.Count -1
    Is the correct way to do this

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Still get values from checkbox

    Close... it should be
    Code:
    For nI = 0 To ListView2.ListItems.Count - 1
    If there are 10 items in the list .Count will equal 10, but the index is 0-based so it runs from 0 to 9 ... which is why it needs to be 0 and .Count - 1 .... otherwise 1) you'll never reach that first item and 2) you exceed its bounds.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Still get values from checkbox

    I did not notice the loop was starting at 1 intitally but I did go back and edit before I saw your post

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Still get values from checkbox

    To be fair, I was actually replying to Tyson's post when I wrote that... seems the quick quote button doesn't click the "Include quote in reply" checkbox and I keep forgetting to check it.... :P

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    47

    Re: Still get values from checkbox

    sorry for the late reply. i tried your all your advices but still i get the same error.. i managed to change the code a bit to follow your advice.. currently i have this:

    Set rs = New ADODB.Recordset
    For nI = 0 To ListView2.ListItems.Count - 1
    If ListView2.ListItems(nI).Checked = True Then 'here is where i get the error guys
    rs.Open "Select * from sendtbl where empid like '%" & ListView2.ListItems(nI).SubItems(4) & "%'", cn, 3, 2
    rs.Delete
    rs.Update
    rs.Close
    Set rs = Nothing

    Call load
    End If

    Next nI

    i also tried changing the nI to 1 and it doesn't get the error but when i click the button it only "UNCHECKED" the records which i have checked.

  9. #9
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Still get values from checkbox

    Code:
    Set rs = Nothing
    you are setting rs to nothing and then going to next item . Remove that put it after Next NI
    And for your selected items if it unchecks them you might have to make a function that will remember all your selected items
    Last edited by Max187Boucher; Jun 13th, 2012 at 12:30 AM.

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    47

    Re: Still get values from checkbox

    thanks for the advice max but i dont think it is in that part, something's wrong in the indexing. i managed to overcome the error of unchecking the checked items but now although it's deleting a record, it only deletes one record per one click not multiple records in one click.

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Still get values from checkbox

    Sorry, the Listview actually uses a index base of 1 for the listitems in VB6 so this should have been fine
    Code:
    For nI = 1 To ListView2.ListItems.Count
    Using the code we gave will give you an error when it tries to process item 0 as it would not exist.

    I do see a few issues in that little block though.
    For example why are you calling Load and what does it do? Does it by chance reload the listview? Is so then that is the source of your problem because once you have deleted an item from the list then there are fewer items in the list but your code is trying to process the original number of items and thus throws an index out of bounds error.

    You should do a reverse loop if you are going to be removing items to avoid such errors
    Code:
    For nI = ListView2.ListItems.Count to 1 Step -1
    Also it is bad practice to Open a recordset, call delete, call update then close not to mention you are destroying the recordset object but may try to access it again if more than one item is checked which would result in yet another error not to mention the wasted time selecting, loading when all you are doing is deleting records.

    Would be better to move the Call Load out of the loop, Do away with all the RS stuff there and simply execute a delete query when needed. Would be faster, neater and more multiuser friendly.

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Still get values from checkbox

    In keeping with what you have you could try
    Code:
    Set rs = New ADODB.Recordset
    For nI = ListView2.ListItems.Count to 1 step -1
        If ListView2.ListItems(nI).Checked = True Then 'here is where i get the error guys
            rs.Open "Select * from sendtbl where empid like '%" & ListView2.ListItems(nI).SubItems(4) & "%'", cn, 3, 2
            rs.Delete  ' If more than one record was returned by the query this will delete only the first record
            'rs.Update ' This statement is useless
            rs.Close
            Call load ' Probably should be moved to after the Next NI, no point in reloading the data over and over
       End If
    
    Next nI
    Set rs = Nothing

    This would probably be better but be aware that if more than one record matches then more than one record will be deleted
    Code:
    For nI = 1 to ListView2.ListItems.Count 
        If ListView2.ListItems(nI).Checked = True Then 'here is where i get the error guys
            cn.Execute "Delete from sendtbl where empid like '%" & ListView2.ListItems(nI).SubItems(4) & "%'"        
        End If
    Next nI
    Call load
    Last edited by DataMiser; Jun 13th, 2012 at 12:43 AM.

  13. #13
    Addicted Member ryanframes's Avatar
    Join Date
    Apr 2012
    Posts
    210

    Re: Still get values from checkbox

    Quote Originally Posted by dexjel140503 View Post
    sorry for the late reply. i tried your all your advices but still i get the same error.. i managed to change the code a bit to follow your advice.. currently i have this:

    Set rs = New ADODB.Recordset
    For nI = 0 To ListView2.ListItems.Count - 1
    If ListView2.ListItems(nI).Checked = True Then 'here is where i get the error guys
    rs.Open "Select * from sendtbl where empid like '%" & ListView2.ListItems(nI).SubItems(4) & "%'", cn, 3, 2
    rs.Delete
    rs.Update
    rs.Close
    Set rs = Nothing

    Call load
    End If

    Next nI

    i also tried changing the nI to 1 and it doesn't get the error but when i click the button it only "UNCHECKED" the records which i have checked.
    i also tried changing the nI to 1 and it doesn't get the error but when i click the button it only "UNCHECKED" the records which i have checked
    it because ur script is tell to do it..

    If ListView2.ListItems(nI).Checked = True Then 'it's declare that u want to unchecked the records that u have checked

  14. #14
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Still get values from checkbox

    Quote Originally Posted by ryanframes View Post
    it because ur script is tell to do it..
    Code:
    If ListView2.ListItems(nI).Checked = True Then 'it's declare that u want to unchecked the records that u have checked
    Actually no that is not the case, that line is simply checking to see if it is checked. It is not making a change to it.

  15. #15

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    47

    Re: Still get values from checkbox

    woah! that's great men. really appreciate your help. i just made some revisions in the table fields and it all goes well.. thank you.. mwah mwah!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width