Results 1 to 4 of 4

Thread: Find the value of an array item which is in a collection

  1. #1

    Thread Starter
    Hyperactive Member csKanna's Avatar
    Join Date
    Dec 2005
    Location
    Tech-Tips-Now.com
    Posts
    339

    Find the value of an array item which is in a collection

    Hi,

    I have a collection and each and every item is an array of values.

    I would like to find the value of items in that array in that collection. I can extract the values if I use For Each ... However, I would like to access directly and find the value.

    The current code I have is:

    Code:
    Sub TestCollection()
        Dim oTests As New Collection
        Dim var As Variant
    
        oTests.Add Array("student 1", "Maths", "90")
        oTests.Add Array("student 2", "history", "50")
        oTests.Add Array("student 3", "science", "80")
    
        For Each var In oTests
            'var(0) 'Student
            'var(1) 'subject
            'var(2) 'marks
            Debug.Print var(0)
            Debug.Print var(1)
            Debug.Print var(2)
        Next var
    End Sub
    I would like to access and get the value "student 2" directly, not using for each.

    Thank you
    Kanna

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Find the value of an array item which is in a collection

    MsgBox oTests.Item(1)(0)
    MsgBox oTests.Item(2)(0)
    MsgBox oTests.Item(3)(0)



  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Find the value of an array item which is in a collection

    I designed a form with a command button and a label. Then I applied this code, preserving most of your notation:
    Code:
    Dim oTests As New Collection
    
    Private Sub Command1_Click()
    'Retrieve all the values for Student 2
    For I = 0 To 2
        Label1.Caption = Label1.Caption & oTests.Item(2)(I) & " "
    Next
    End Sub
    
    Private Sub Form_Load()
    oTests.Add Array("student 1", "Maths", "90")
    oTests.Add Array("student 2", "history", "50")
    oTests.Add Array("student 3", "science", "80")
    Label1.Caption = vbNullString
    End Sub
    I believe a loop is still the best way to pull in all the values within an item in the collection.
    Last edited by Code Doc; Jul 27th, 2008 at 03:47 PM.
    Doctor Ed

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Find the value of an array item which is in a collection

    Since I assume that a single student can be taking more than one subject you might like this. It's a bit complicated but it's a flexible technique. BTW it's a modification of some code that I got from Devx a few years ago.
    Attached Files Attached Files

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