Results 1 to 4 of 4

Thread: [RESOLVED] Searching Collection - alternative solution !

  1. #1

    Thread Starter
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Resolved [RESOLVED] Searching Collection - alternative solution !

    Hi Everyone,

    i have 2 collections, one holding a subset of the others data, and i am trying to find out if an item in the smaller collection exists in the bigger collection.

    What i have is a full list of tables & a list of enabled tables. From these i have to extrapolate the disabled tables.

    I do have a solution for this but it is a bit messy and uses On Error Resumes Next which i would like to avoid, so i was wondering if anyone has an alternative solution.

    This is the full sub which basically fills two listbox's. Enabled tables in one & Disabled tables in the other. a user can the move the tables between the list to enable or disable them.

    VB Code:
    1. Dim arrEnabledTables() As String
    2. Dim colEnabledTables As Collection
    3. Dim colAvailableTables As Collection
    4. Dim vitem As Variant
    5. Dim vitem2 As Variant
    6.  
    7. On Error GoTo errorhandler
    8.  
    9. Set mParent = FrmParent
    10.  
    11. Set colAvailableTables = New Collection
    12. Set colEnabledTables = New Collection
    13.  
    14. colAvailableTables.Add "item1", "item1"  'List of All Tables
    15. colAvailableTables.Add "item2", "item2"
    16. colAvailableTables.Add "item3", "item3"
    17. colAvailableTables.Add "item4", "item4"
    18. colAvailableTables.Add "item5", "item5"
    19. colAvailableTables.Add "item6", "item6"
    20. colAvailableTables.Add "item7", "item7"
    21. colAvailableTables.Add "item8", "item8"
    22. colAvailableTables.Add "item9", "item9"
    23. colAvailableTables.Add "item10", "item10"
    24.  
    25. arrEnabledTables = mParent.Application.EnabledTables  'external method that returns an array of enabled tables
    26.  
    27. For n = 0 ToUBound(arrEnabledTables)  'add Enabled Tables to listbox & a collection
    28.      LstEnableTables.AddItem Right(arrEnabledTables(n), Len(arrEnabledTables(n)) - 3)
    29.      colEnabledTables.Add Right(arrEnabledTables(n), Len(arrEnabledTables(n)) - 3), Right(arrEnabledTables(n), Len(arrEnabledTables(n)) - 3)
    30. Next
    31.  
    32. On Error Resume Next
    33. For Each vitem In colAvailableTables  'get Disabled Tables and add them to the other listbox
    34.      vitem2 = colEnabledTables(vitem)
    35.      If IsEmpty(vitem2) Then
    36.           LstDisabledTables.AddItem CStr(vitem)
    37.      End If
    38.      vitem2 = Nothing
    39. Next
    40. On Error GoTo 0

    The reason i have to use OERN is that when you search a collection for an item that is not in it, it throws an error (unlike .Net which if you use an ArrayList it returns a boolean when using the contains method)

    Any ideas for alternatives ?
    Last edited by NeedSomeAnswers; Sep 3rd, 2010 at 04:27 AM.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Searching Collection - alternative solution !

    Any ideas for alternatives ?
    how about a recordset? you can search result using filter
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: Searching Collection - alternative solution !

    Using OERN is pretty standard fare when it comes to determining if a collection contains a given item, however life is a little easier if you also employ the Key field when using the Add method.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    
        Dim col As New Collection
        Dim Search4 As String
        
        col.Add "Henry", "Henry"
        col.Add "John", "John"
        
        Search4 = "Henry"
        
        On Error Resume Next
        col.Add Search4, Search4
        If Err = 457 Then
            MsgBox Search4 & " is already in the collection"
        ElseIf Err Then
            MsgBox "Unanticipated error " & Err & " " & Err.Description
        Else
            'if we do not want Search4 to be added
            col.Remove (Search4)
            
            MsgBox Search4 & " is NOT already in the collection"
        End If
        On Error GoTo 0
    
    End Sub

  4. #4

    Thread Starter
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Searching Collection - alternative solution !

    Hi Guys thanks for the responses,

    i have been so busy fixing some post implementation bugs i almost forgot i had even posted this. I got the answer i expected anyway, i didn't think there would be a way.

    Westconn - that is an interesting idea to use a disconnected recordset, and one i will try in the future i just dont have any time to really rewrite it now so i will leave it as is.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



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