PDA

View Full Version : Solved: deleting form listbox and datagrid



maryam
02-14-2007, 09:44 PM
we populate listbox2 with selected items from listbox1 clicking on the add bottom, here are the codes for click event:
Private Sub cmdAdd_Click()
Dim mI As Long
Dim CheckDic As Dictionary
Set CheckDic = New Dictionary

' First populate the Dictionary object (CheckDic)
' with all items in listbox2...if any
If ListBox2.ListCount > 0 Then
For mI = 0 To ListBox2.ListCount - 1
If Not CheckDic.Exists(ListBox2.List(mI)) Then _
CheckDic.Add ListBox2.List(mI), Nothing
Next
End If

' Now check for items from listbox1 that are to be added
' into listbox2 if they already exist.
For mI = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(mI) Then
If Not CheckDic.Exists(ListBox1.List(mI)) Then
ListBox2.AddItem ListBox1.List(mI)
r.AddNew
r.Fields(0).Value = ListBox1.List(mI)
Else
Beep
End If
End If
Next
End Sub

then in the other form in which we have datagird1, in the form initialize event we have :
Set DataGrid1.DataSource = r

and we defined Public r As ADOR.Recordset in a module

Now there is a problem in deleting items from listbox2. here are the codes:
Private Sub CmdDelete_Click()
Dim mI As Long

For mI = ListBox2.ListCount - 1 To 0 Step -1
If ListBox2.Selected(mI) Then
r.MoveFirst
r.Find "Selected = '" & ListBox2.List(mI) & "'"
r.Delete adAffectCurrent
r.MoveNext
ListBox2.RemoveItem mI
End If
Next
End Sub

it gives this debug:
"Item cannot be found in the collection corresponding to the requested name or ordinal" and
r.Find "Selected = '" & ListBox2.List(mI) & "'" is yellow.http://us.i1.yimg.com/us.yimg.com/i/mesg/tsmileys2/02.gif

recordset was not filled with the same information as the listbox. How can we solve this?

Bob Phillips
02-15-2007, 03:20 AM
Can you post the workbook to save us the effort of re-inventing it?

maryam
02-15-2007, 05:34 AM
here is the file

maryam
02-16-2007, 02:51 AM
I made a carelessness. toomy understood. In my DataGrid I wrote Component and then under delete click event is:
r.find" Selected= ...", it should be changed to r.find"Component= ..."
attached is the file which works.

mfn562
09-02-2007, 03:52 AM
I made a carelessness. toomy understood. In my DataGrid I wrote Component and then under delete click event is:
r.find" Selected= ...", it should be changed to r.find"Component= ..."
attached is the file which works.

thanks