Results 1 to 5 of 5

Thread: [RESOLVED] Dictionary Object

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010

    Resolved [RESOLVED] Dictionary Object

    I created a test dictionary object see code
    VB 6
    Option Explicit
    Dim Phones As Dictionary

    Private Sub cmdDelete_Click()
    If lstNumbers.ListIndex > -1 Then
    Phones.Remove lstNumbers.List(lstNumbers.ListIndex)
    lstNumbers.RemoveItem lstNumbers.ListIndex
    End If
    End Sub

    Private Sub Form_Load()
    ' Create the dictionary object
    Set Phones = New Dictionary

    ' Add the phone numbers
    Phones.Add "123456", "Home"
    Phones.Add "225588", "Mobile"
    Phones.Add "798456", "Office"
    Phones.Add "54723", "Works"

    End Sub

    Private Sub UpdateControls()
    Dim i As Integer
    Dim phone As Variant

    'clear all the controls to start with

    For i = 0 To 3
    lblLocation(i).Caption = ""
    txtNumber(i).Text = ""
    Next i

    i = 0

    For Each phone In Phones
    ' add the phone numbers to the listbox
    lstNumbers.AddItem phone

    ' add the details to the label and textboxes
    lblLocation(i).Caption = Phones(phone)
    txtNumber(i).Text = phone

    i = i + 1
    End Sub
    When i try to insert the code in red into another project it fails with error 32811 missing key but if I debug.print the key its there can you help please
    Project VB6 code

    Public myPhones As Dictionary
    Private vPhone As Variant

    Set myPhones = New Dictionary

    If lstNums.ListIndex > -1 Then
    Debug.Print CStr(lstNums) 'has number
    Debug.Print lstNums.ListIndex 'has index
    If myPhones.Exists(lstNums) Then Debug.Print "Yes" 'nothing

    With frmEditorAdTe
    .lstNums.Visible = True
    myPhones.Remove CStr(.lstNums.List(lstNums.ListIndex))
    End With
    End If
    Last edited by RocketRon; Apr 16th, 2015 at 10:21 AM.

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    NC, USA

    Re: Dictionary Object

    Can you please next time enclose your code in code tags?
    Do you create your Dictionary in the other project, or just the 'code in red'?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010

    Re: Dictionary Object

    Sorry I should have known better about tags
    Its the code in red that I am having the problem with

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Beside Waldo

    Re: Dictionary Object

    I wouldn't expect this line to error: Phones.Remove lstNumbers.List(lstNumbers.ListIndex)
    Why? Because Phones had items added to it in Form_Load

    I would expect this line to error: myPhones.Remove CStr(.lstNums.List(lstNums.ListIndex))
    Why? Because from what code you posted, myPhones is a New Dictionary object and no entries were added to it
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online

    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010

    Re: Dictionary Object

    Hi LaVolpe
    Thanks for your post you were correct of course and it lead me to investigate further as I could not understand how the debug.print showed the number I wanted to delete and then I figured it out it was from the original dictionary object and not the current object and when I corrected that it all worked fine.
    So what you see may look like what you want but it may not be what you want

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