Nov 3rd, 2004, 12:44 AM
#1
Thread Starter
Hyperactive Member
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:
Private Sub CmdDelete_Click()
Call OpenConn
If Me.List1.ListIndex = -1 Then
MsgBox "You should select a Level to delete", vbExclamation, "Delete Level"
Else
Me.List1.RemoveItem Me.List1.ListIndex
RSLevels.Open "Levels", Conn, adOpenDynamic, adLockOptimistic, adCmdTable
With RSLevels
If Not (.BOF = True Or .EOF = True) Then
.Delete adAffectCurrent
If Not (.BOF = True Or .EOF = True) Then
.MoveNext
If .EOF Then RS.MoveLast
.Requery
Me.List1.Refresh
.Close
End If
End If
End With
End If
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 .
Nov 3rd, 2004, 01:17 AM
#2
Thread Starter
Hyperactive Member
Hi Once again,
I've made it with the ListView and also same.
VB Code:
Private Sub CmdDelete_Click()
Call OpenConn
If Me.ListView1.SelectedItem.Selected = 0 Then
MsgBox "You should select a Level to delete", vbExclamation, "Delete Level"
Else
RSLevels.Open "Levels", Conn, adOpenDynamic, adLockOptimistic, adCmdTable
With RSLevels
.Delete adAffectCurrent
.Update
.Requery
Me.ListView1.Refresh
.Close
End With
If ListView1.SelectedItem.Index <> 1 Then
ListView1.ListItems.Remove ListView1.SelectedItem.Index
End If
End If
End Sub
Regards,
habibalby
Nov 3rd, 2004, 03:45 AM
#3
Hyperactive Member
You are deleting an unknown row? How did you know, in your code, what row to delete? You should have something like
VB Code:
RSLevels.Open "select [id] from levels where [id] = " & val(listView1.SelectedItem.key), Conn, adOpenDynamic, adLockoptimistic, adCmdTable
RSLevels.Delete
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...
Nov 3rd, 2004, 05:14 AM
#4
Thread Starter
Hyperactive Member
dRAMmer
Well, I've used your suggestion to get rid of this, but I end up with a Syntex Error.
VB Code:
RSLevels.Open "Select Levels.[ID] From Levels Where Levels.[ID] = " & Val(ListView1.SelectedItem.Key), Conn, adOpenDynamic, adLockOptimistic, adCmdTable
RSLevels.Delete
Nov 4th, 2004, 07:08 AM
#5
Thread Starter
Hyperactive Member
Hi,
Well, now the item is deleted from the ListView, but it won't be deleted from the Table.
VB Code:
RSLevels.Open "Select ID From Levels Where ID=" & Val(ListView1.SelectedItem.Key), Conn, adOpenDynamic, adLockOptimistic, adCmdText
With RSLevels
If Not (.BOF = True Or .EOF = True) Then
.Delete
If Not (.BOF = True Or .EOF = True) Then
.MoveNext
If .EOF Then RS.MoveLast
.Update
.Requery
.Close
End If
End If
End With
If ListView1.SelectedItem.Index <> 1 Then
ListView1.ListItems.Remove ListView1.SelectedItem.Index
MsgBox " Item Deleted", vbExclamation, "Delete Item"
End If
Any Help ????????
Thanks,
Habibalby
Nov 7th, 2004, 07:47 PM
#6
Hyperactive Member
Hi!
You might have forgotten to set the key of each listitem to the id of the table.
Nov 8th, 2004, 06:43 AM
#7
Thread Starter
Hyperactive Member
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
Nov 9th, 2004, 07:34 AM
#8
Thread Starter
Hyperactive Member
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
Nov 9th, 2004, 07:17 PM
#9
Hyperactive Member
VB Code:
ListView1.ListItems.Add ,"ID" & .fields("id"),.fields("level")
Deleting the the record
VB Code:
RSLevels.Open "Select ID From Levels Where ID=" & Val(mid(ListView1.SelectedItem.Key,3,len(ListView1.SelectedItem.Key)-2)), Conn, adOpenDynamic, adLockOptimistic, adCmdText
rslevels.delete
'or
'
Dim sKey as string
sKey = ListView1.SelectedItem.Key
Mid(sKey, 1, 2) = Space(2)
RSLevels.Open "Select ID From Levels Where ID=" & Val(skey), Conn, adOpenDynamic, adLockOptimistic, adCmdText
RSLevels.Delete
Nov 9th, 2004, 07:21 PM
#10
Hyperactive Member
VB Code:
ListView1.ListItems.Add ,"ID" & .fields("id"),.fields("level")
Deleting the the record
VB Code:
RSLevels.Open "select ID from Levels where ID=" & val(mid(listview1.selecteditem.key,3,len(listview1.selecteditem.key)-2)), conn, adopendynamic, adlockoptimistic, adcmdtext
rslevels.delete
'
'or
Dim sKey as strin
sKey = listview1.SelectedItem.Key
Mid(Skey, 1, 2) = space(2)
rslevel.open "Select ID From Levels Where ID=" & val(skey),conn, adopendynamic, adlockoptimistic, adcmdtext
rslevels.delete
Nov 11th, 2004, 09:11 PM
#11
Thread Starter
Hyperactive Member
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
Nov 12th, 2004, 12:17 AM
#12
Hyperactive Member
Where's the project? You forgot to post it?
Nov 12th, 2004, 02:11 AM
#13
Thread Starter
Hyperactive Member
Originally posted by dRAMmer
Where's the project? You forgot to post it?
Attached Files
Nov 15th, 2004, 07:43 PM
#14
Hyperactive Member
Here it is, take notice of the comments.
Attached Files
Nov 16th, 2004, 08:30 AM
#15
Thread Starter
Hyperactive Member
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:
ListView1.ListItems.Add , "emp" & .Fields("ID"), .Fields("Name")
Thanks,
habibalby
Nov 16th, 2004, 07:23 PM
#16
Hyperactive Member
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.
Nov 18th, 2004, 08:50 AM
#17
Thread Starter
Hyperactive Member
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:
Skey = ListView1.SelectedItem.Key
Mid(Skey,1,3) = Space(3)
Sql = "Delete From TableName where [ID]=" & Val(Skey)
Conn.Excute Sql, SKey
If Skey = 0 then
'Do Nothing
Else
Msgbox "Item Deleted"
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
Forum Rules
Click Here to Expand Forum to Full Width