ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Using the Dictionary object (https://www.excelbanter.com/excel-programming/387144-using-dictionary-object.html)

vqthomf

Using the Dictionary object
 
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

Tom Ogilvy

Using the Dictionary object
 
show the code that doesn't work.

--
Regards,
Tom Ogilvy


"vqthomf" wrote:

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


vqthomf

Using the Dictionary object
 
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.

"Tom Ogilvy" wrote:

show the code that doesn't work.

--
Regards,
Tom Ogilvy


"vqthomf" wrote:

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


Tom Ogilvy

Using the Dictionary object
 
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


"vqthomf" wrote:

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.

"Tom Ogilvy" wrote:

show the code that doesn't work.

--
Regards,
Tom Ogilvy


"vqthomf" wrote:

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


vqthomf

Using the Dictionary object
 
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

"Tom Ogilvy" wrote:

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


"vqthomf" wrote:

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.

"Tom Ogilvy" wrote:

show the code that doesn't work.

--
Regards,
Tom Ogilvy


"vqthomf" wrote:

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


Dana DeLouis

Using the Dictionary object
 
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

--
Dana DeLouis
Windows XP & Office 2007


"vqthomf" wrote in message
...
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.

"Tom Ogilvy" wrote:

show the code that doesn't work.

--
Regards,
Tom Ogilvy


"vqthomf" wrote:

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





All times are GMT +1. The time now is 09:13 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com