Results 1 to 17 of 17

Thread: Deleting Item from ListView Field from MS-Access Table

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2004
    Location
    Bahrain
    Posts
    306

    Deleting Item from ListView Field from MS-Access Table

    Hi Guys,

    I'm wondering on how to delete an item in the ListBox that field from the Table MS-Access.

    VB Code:
    1. Private Sub CmdDelete_Click()
    2. Call OpenConn
    3.  If Me.List1.ListIndex = -1 Then
    4.  MsgBox "You should select a Level to delete", vbExclamation, "Delete Level"
    5.  Else
    6.  Me.List1.RemoveItem Me.List1.ListIndex
    7.  
    8.  
    9. RSLevels.Open "Levels", Conn, adOpenDynamic, adLockOptimistic, adCmdTable
    10.  
    11.     With RSLevels
    12.           If Not (.BOF = True Or .EOF = True) Then
    13.          .Delete adAffectCurrent
    14.           If Not (.BOF = True Or .EOF = True) Then
    15.          .MoveNext
    16.           If .EOF Then RS.MoveLast
    17.          .Requery
    18.           Me.List1.Refresh
    19.          .Close
    20.           End If
    21.           End If
    22.    End With
    23.    End If
    24.    
    25. End Sub

    I've used the above code, it's deleted the select item from the ListBox perfectly, but once you cameback to the Form you will notice that the Item deleted is the First Item in the Table.

    Can anyone help to delete the Selected Item from the ListBox as well as from the Table ??

    Many Thanks in advance,

    habibalby
    Last edited by Habibi; Nov 4th, 2004 at 07:10 AM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2004
    Location
    Bahrain
    Posts
    306
    Hi Once again,

    I've made it with the ListView and also same.
    VB Code:
    1. Private Sub CmdDelete_Click()
    2. Call OpenConn
    3.  If Me.ListView1.SelectedItem.Selected = 0 Then
    4.  MsgBox "You should select a Level to delete", vbExclamation, "Delete Level"
    5.  Else
    6.  
    7. RSLevels.Open "Levels", Conn, adOpenDynamic, adLockOptimistic, adCmdTable
    8.  
    9.     With RSLevels
    10.  
    11.          .Delete adAffectCurrent
    12.          .Update
    13.          .Requery
    14.           Me.ListView1.Refresh
    15.          .Close
    16.    End With
    17.  
    18.     If ListView1.SelectedItem.Index <> 1 Then
    19.     ListView1.ListItems.Remove ListView1.SelectedItem.Index
    20.     End If
    21.     End If
    22. End Sub

    Regards,
    habibalby

  3. #3
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    You are deleting an unknown row? How did you know, in your code, what row to delete? You should have something like

    VB Code:
    1. RSLevels.Open "select [id] from levels where [id] = " & val(listView1.SelectedItem.key), Conn, adOpenDynamic, adLockoptimistic, adCmdTable
    2.  
    3. RSLevels.Delete
    Me.ListView1.Refresh
    This will not refresh your listview with the data you have in your rs (in VB6 at least).

    Take note that ID was assumed a numeric data type (INT, LONG,etc)
    Last edited by dRAMmer; Nov 3rd, 2004 at 03:56 AM.
    live, code and die...

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2004
    Location
    Bahrain
    Posts
    306
    dRAMmer

    Well, I've used your suggestion to get rid of this, but I end up with a Syntex Error.
    VB Code:
    1. RSLevels.Open "Select Levels.[ID] From Levels Where Levels.[ID] = " & Val(ListView1.SelectedItem.Key), Conn, adOpenDynamic, adLockOptimistic, adCmdTable
    2.  
    3. RSLevels.Delete

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2004
    Location
    Bahrain
    Posts
    306
    Hi,

    Well, now the item is deleted from the ListView, but it won't be deleted from the Table.

    VB Code:
    1. RSLevels.Open "Select ID From Levels Where ID=" & Val(ListView1.SelectedItem.Key), Conn, adOpenDynamic, adLockOptimistic, adCmdText
    2.  
    3. With RSLevels
    4. If Not (.BOF = True Or .EOF = True) Then
    5.         .Delete
    6.          If Not (.BOF = True Or .EOF = True) Then
    7.         .MoveNext
    8.          If .EOF Then RS.MoveLast
    9.         .Update
    10.         .Requery
    11.         .Close
    12.         End If
    13.         End If
    14. End With
    15.  
    16.     If ListView1.SelectedItem.Index <> 1 Then
    17.     ListView1.ListItems.Remove ListView1.SelectedItem.Index
    18.     MsgBox " Item Deleted", vbExclamation, "Delete Item"
    19.     End If

    Any Help ????????

    Thanks,
    Habibalby

  6. #6
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    Hi!
    You might have forgotten to set the key of each listitem to the id of the table.
    live, code and die...

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2004
    Location
    Bahrain
    Posts
    306
    Hi mate,

    Which Key you mean ?? in the Table I have two field one is the ID which is AutoNumber and the Other is Level Which is Text.

    Is there any other key I have to set for the ListItem ??

    Thanks,
    Habibalby

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2004
    Location
    Bahrain
    Posts
    306
    Hi once again,

    I've made the Key as per the Column Heard Caption which is level. and I've tried to delete also, but I can't always is getting error Syntax error.

    Thanks,
    Habibalby

  9. #9
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    VB Code:
    1. ListView1.ListItems.Add ,"ID" & .fields("id"),.fields("level")
    Deleting the the record
    VB Code:
    1. RSLevels.Open "Select ID From Levels Where ID=" & Val(mid(ListView1.SelectedItem.Key,3,len(ListView1.SelectedItem.Key)-2)), Conn, adOpenDynamic, adLockOptimistic, adCmdText
    2. rslevels.delete
    3. 'or
    4. '
    5. Dim sKey as string
    6. sKey = ListView1.SelectedItem.Key
    7. Mid(sKey, 1, 2) = Space(2)
    8.  
    9. RSLevels.Open "Select ID From Levels Where ID=" & Val(skey), Conn, adOpenDynamic, adLockOptimistic, adCmdText
    10. RSLevels.Delete
    live, code and die...

  10. #10
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    VB Code:
    1. ListView1.ListItems.Add ,"ID" & .fields("id"),.fields("level")
    Deleting the the record
    VB Code:
    1. RSLevels.Open "select ID from Levels where ID=" & val(mid(listview1.selecteditem.key,3,len(listview1.selecteditem.key)-2)), conn, adopendynamic, adlockoptimistic, adcmdtext
    2. rslevels.delete
    3. '
    4. 'or
    5. Dim sKey as strin
    6. sKey = listview1.SelectedItem.Key
    7. Mid(Skey, 1, 2) = space(2)
    8. rslevel.open "Select ID From Levels Where ID=" & val(skey),conn, adopendynamic, adlockoptimistic, adcmdtext
    9. rslevels.delete
    live, code and die...

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2004
    Location
    Bahrain
    Posts
    306
    HI mate,

    I've tried both techniques, but it didn't works. Can you have a look at the simple project that I posted here, download it and check if y ou can modify it??


    Thanks,
    Habibalby

  12. #12
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    Where's the project? You forgot to post it?
    live, code and die...

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2004
    Location
    Bahrain
    Posts
    306
    Originally posted by dRAMmer
    Where's the project? You forgot to post it?
    Attached Files Attached Files

  14. #14
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    Here it is, take notice of the comments.
    Attached Files Attached Files
    live, code and die...

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2004
    Location
    Bahrain
    Posts
    306

    Waw

    Many thanks mate. But one more think I'd like to ask you, is it wasn't deleted becuase the ID's not entered along with the name. ??
    VB Code:
    1. ListView1.ListItems.Add , "emp" & .Fields("ID"), .Fields("Name")

    Thanks,
    habibalby

  16. #16
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    Yes it was, you need to have a reference to the record you want to manipulate, so you have to set the key of the listitem to the reference (ID, for that matter) of your row.
    live, code and die...

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2004
    Location
    Bahrain
    Posts
    306
    Hi Mate. Yes, I got it now, but the code also it can be run without the Dim RA as Long Variable Like:
    VB Code:
    1. Skey = ListView1.SelectedItem.Key
    2. Mid(Skey,1,3) = Space(3)
    3.  
    4. Sql = "Delete From TableName where [ID]=" & Val(Skey)
    5. Conn.Excute Sql, SKey
    6.  
    7. If Skey = 0 then
    8. 'Do Nothing
    9. Else
    10. Msgbox "Item Deleted"
    11. End if

    Regards,
    Habibalby

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