Link to home
Start Free TrialLog in
Avatar of jackjohnson44
jackjohnson44

asked on

vb search

I have a performance question.

I want to see if a given item exists in a list.

search for Item #2

Item #1
Item #2
Item #3
Item #4

I am currently creating a semi colon delimited list

myList="Item #1;Item #2;Item #3;Item #4"

and then doing an instr so see if it exists

If InStr(myList, searchItem& ";") Then
  'I found it
end if


Is this a good way of accomplishing this?
Would it be better to use an array or something?

Thanks

Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

As long as the list is fairly short then you should be ok. But if you list is going to grow larger then you should probably consider switching to an array, or even using a dictionary.
Avatar of jackjohnson44
jackjohnson44

ASKER

how would I accomplish this using an array or dictionary?
Using an array would be a simple loop:

    Dim arrList(3)
    arrList(0) = "Item #1"
    arrList(1) = "Item #2"
    arrList(2) = "Item #3"
    arrList(3) = "Item #4"  

    Dim sSearchFor As String
    sSearchFor = "Item #2"

    Dim bFound As Boolean
    Dim i As Integer

    bFound = False    '// Default
    For i = 0 To UBound(arrList)
        If arrList(i) = sSearchFor Then
            '// Found the item, so exit loop
            bFound = True
            Exit For
        End If
    Next i


An example of using the Dictionary can be found here:

    http://www.kamath.com/tutorials/tut009_dictionary.asp
Here is another Dictionary example...

Option Explicit

' Click on Project --> References and Check the "Microsoft Scripting Runtime" Entry.

Private dct As New Dictionary

Private Sub Form_Load()
    dct.Add "Item #1", "a"
    dct.Add "Item #2", "b"
    dct.Add "Item #3", "c"
    dct.Add "Item #4", "d"
End Sub

Private Sub Command1_Click()
    Dim key As String
    Dim value As Variant
   
    key = "Item #3"
    If dct.Exists(key) Then
        value = dct(key)
        MsgBox value, vbInformation, key
    Else
        MsgBox key, vbExclamation, "Key Not Found"
    End If
   
    key = "Item #5"
    If dct.Exists(key) Then
        value = dct(key)
        MsgBox value, vbInformation, key
    Else
        MsgBox key, vbExclamation, "Key Not Found"
    End If
End Sub
Here is the code you'll need.

'//Code\\
'This code requires nothing
Private Function IsPresent(ByVal List As String, ByVal Item As String) As Boolean
Dim aHold() As String, iCount As Integer
aHold = Split(List, ";")
For iCount = LBound(aHold) To UBound(aHold)
    If (aHold(iCount) = Item) Then IsPresent = True: Exit Function
Next iCount
End Function
'\\Code//

Arguments:
List :: The list, each item should be seperated by a SemiColon (;)
Item :: The item that you are checking for
SOLUTION
Avatar of BrianGEFF719
BrianGEFF719
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For LARGE lists, the Collection or Dictionary approach will be the fastest...especially if you have to add/remove items in the list.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also forgot you can manually load recordset but if you need to do this use the dictionary object. Here is example:

https://www.experts-exchange.com/questions/20990817/using-Filters-with-ADO-recordsets.html?query=disconnected+recordset+ford&clearTAFilter=true