Using the Dictionary object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi we have a form with a next and prev buttons we are using the dictionary
object to hold the answers selected by the user, we use the check box
selection for each question selected if the check is true we add it to the
dictionary and if unticked we remove it from the dictionary then the use
would move to the next set of question that are loaded from a database. If
the use uses the prev button and the data is loaded from the database and the
check boxes are ticked from the dictionary, if the user unticks a check box
the code tries to remove this refference from then dictionary when we use the
exist it finds it but wont remove it? can any body explain reason for this.
TIA
Charles
 
The code for selecting the check box is
Private Sub LV_ItemCheck(ByVal Item As MSComctlLib.ListItem)
'

RcdNumber = Item.Index

Select Case Item.Checked
Case True
dct.Add LV.ListItems(RcdNumber), " "
Debug.Print "Added " & LV.ListItems(RcdNumber)
TextBox1.Text = ""
TextBox1.SetFocus
Case False
If dct.Exists(LV.ListItems(RcdNumber)) Then dct.Remove
(LV.ListItems(RcdNumber))
LV.ListItems(RcdNumber).SubItems(3) = ""
TextBox1.Text = ""
End Select

End Sub

This works as long as you are working on the listbox move away it wont
remove an item.

Private Sub cmdPrev_Click()
'
Dim itm, litm, strKey As String, i

If cmdNext.Enabled = False Then cmdNext.Enabled = True
MyNext = MyNext - 1
MyLabelNum = MyNext
MyLabelNum = LabelConfig(MyLabelNum)
If MyNext < 2 Then cmdPrev.Enabled = False
LV.ListItems.Clear
Call GetQuest

For i = 0 To dct.Count - 1
itm = dct.Keys(i)
For Each litm In LV.ListItems
If litm = itm Then
LV.ListItems(litm.Index).Checked = True
LV.ListItems(litm.Index).SubItems(3) =
dct.Item(dct.Keys(i))
End If
Next litm
Next i

'Call GetSavedData
End Sub
this will bring back everything including items that we thought had been
removed.
 
I get an error if I use the construct of

itm = dct.Keys(i)

So maybe you have an error handler clouding the issue.

I would use


Private Sub cmdPrev_Click()
'
Dim itm, litm, strKey As String, i, a

If cmdNext.Enabled = False Then cmdNext.Enabled = True
MyNext = MyNext - 1
MyLabelNum = MyNext
MyLabelNum = LabelConfig(MyLabelNum)
If MyNext < 2 Then cmdPrev.Enabled = False
LV.ListItems.Clear
Call GetQuest

a = dct.Keys
For i = 0 To dct.Count - 1
itm = a(i)
For Each litm In LV.ListItems
If litm = itm Then
LV.ListItems(litm.Index).Checked = True
LV.ListItems(litm.Index).SubItems(3) = _
dct.Item(dct.Keys(i))
End If
Next litm
Next i

'Call GetSavedData
End Sub
 
our problem is that a key is found in the exist method of the dictionary but
wont remove it from the dictionary, if we stay working selecting and
unslecting the checkboxes everything works if we click the next button then
click the prev button the key is found but we get an error when the code
tries to remove the key?.
TIA
Charles
 
Hi. I can't tell, but is the Key you are adding a string, or some kind of
array?
I'm guessing from these two lines.

dct.Add LV.ListItems(RcdNumber), " "
'...
LV.ListItems(RcdNumber).SubItems(3) = ""

I would debug by making "LV.ListItems(RcdNumber)" a variable.

Private Sub LV_ItemCheck(ByVal Item As MSComctlLib.ListItem)
Dim Str As String

RcdNumber = Item.Index
Str = LV.ListItems(RcdNumber)

Select Case Item.Checked
Case True
dct.Add Str, Space(1)
Debug.Print "Added " & Str
TextBox1.Text = vbNullString
TextBox1.SetFocus
Case False
If dct.Exists(Str) Then dct.Remove (Str)
LV.ListItems(RcdNumber).SubItems(3) = vbNullString
TextBox1.Text = vbNullString
End Select

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top