Results 1 to 4 of 4

Thread: [RESOLVED] Delete Items from a LsitBox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Resolved [RESOLVED] Delete Items from a LsitBox

    I'm trying to delete items in a listbox that the user has selected. Currently my code deletes half of the items selected. (If 10 items were selected 5 would be deleted from the listbox, and the other 5 will remain). Any suggestions? Thanks

    VB Code:
    1. '*CMDVERIFY_CLICK()************************************************************
    2. 'NAME: cmdVerify_Click()
    3. 'DESC: Allows the user to verify that they have counted the selected item(s).
    4. Private Sub cmdVerify_Click()
    5.     Set connection = New ADODB.connection
    6.     connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    7.         "Data Source= G:\Finance\Inventory Control\CycleCount\DB\cycleCount.mdb"
    8.     connection.Open
    9.    
    10.     Dim counter As Integer
    11.     Do While counter < lstVerificationScreen.ListCount
    12.         If (lstVerificationScreen.Selected(counter) = True) Then
    13.  
    14.             Dim selectStatement, sSQL As String
    15.             Dim strArray() As String
    16.            
    17.             strArray = Split(lstVerificationScreen.List(counter), vbTab, -1, 1)
    18.                                
    19.             selectStatement = "DELETE FROM [MAIN] WHERE ID=" & strArray(0)
    20.            
    21.             sSQL = "UPDATE [ALTER] SET TYPEOFCHANGE='DELETED' WHERE [MAIN ID]='" & strArray(0) & "'"
    22.            
    23.             connection.Execute sSQL
    24.            
    25.             connection.Execute selectStatement
    26.  
    27.             lstVerificationScreen.RemoveItem (counter)
    28.            
    29.            
    30.         End If
    31.         DoEvents
    32.         counter = counter + 1
    33.     Loop
    34.    
    35.    
    36.     connection.Close
    37.     Set connection = Nothing
    38.    
    39. End Sub
    40. '*ENDOF*CMDVERIFY_CLICK()******************************************************

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Delete Items from a LsitBox

    You have to loop in reverse:

    VB Code:
    1. [COLOR=Red]counter = lstVerificationScreen.ListCount
    2. Do While counter > 0
    3. [/COLOR]        If (lstVerificationScreen.Selected(counter) = True) Then
    4.  
    5.             Dim selectStatement, sSQL As String
    6.             Dim strArray() As String
    7.                         strArray = Split(lstVerificationScreen.List(counter), vbTab, -1, 1)
    8.                                
    9.             selectStatement = "DELETE FROM [MAIN] WHERE ID=" & strArray(0)
    10.            
    11.             sSQL = "UPDATE [ALTER] SET TYPEOFCHANGE='DELETED' WHERE [MAIN ID]='" & strArray(0) & "'"
    12.                         connection.Execute sSQL
    13.            
    14.             connection.Execute selectStatement
    15.  
    16.             lstVerificationScreen.RemoveItem (counter)
    17.            
    18.            
    19.         End If
    20.         DoEvents
    21. [COLOR=Red]        counter = counter - 1
    22. [/COLOR]    Loop

  3. #3
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Delete Items from a LsitBox

    Probably need to roll back your counter after each delete. Your iterating through your listbox but your not giving yourself credit for the delete you just did.

    Doh! Nothing like repeating what the person above says....

    Also, you need to offset your "counter" variable because listboxes are 0 based.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Delete Items from a LsitBox

    Thanks guys

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