Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find a value in an array 1

Status
Not open for further replies.

GKChesterton

Programmer
Aug 17, 2006
278
US
I am adding records to an array called arrayNeedles. For each value strNeedle, I need to know if it is already in the array or not.

The only way I know to do this:
Code:
    Dim lngUbound As Long 
    lngUbound = UBound(arrayNeedles, 2)
    
    Dim i As Long
    For i = 0 To lngUbound  
        If arrayNeedles(0, i) Like strNeedle Then
            'Gotcha!  Now I edit arrayNeedles(1, i)
            arrayNeedles(1, i) = arrayNeedles(1, i) + 1
            Exit Property
        End If
    Next

    ' Hmm, I never made it to the Exit 
    ' ... must have to add this as a new record
    i = lngUbound + 1

    ReDim Preserve arrayPN(1, i)

    arrayNeedles(0, i) = strNeedle
    arrayNeedles(1, i) = 1

Maybe I'm just spoiled on the array functions for PHP, but seems to me there ought to be a simpler way. Any suggestions?

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 




maybe this...
Code:
If arrayNeedles(0, i) Like "'*" & strNeedle & "*'" Then

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
No; it's a discrete text string not needing wildcards. Thanks though. I've done some more googling and I think my routine above is as good as it gets using VBA. But would love to hear otherwise.

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
Had it been a one dimensional array, you could have used the Join and InStr functions, but I don't think there are better ways for arrays, except if it's sorted and you can use different finding strategies...

I find myself using arrays seldom know. If it's fetched from a recordset, I use the recordset. If the information has other sources, I'm more inclined to use a disconnected ADO recordset in stead, where I can issue multicolumn sorts, filter and more stuff that takes pages of code to do with arrays.

Roy-Vidar
 
Thanks RV. I read another entry of yours about disconnected ADO recordsets.

But I am staying in DAO. I got burned on ADO, -- my own fault I am sure. I built a heavily and inexpertly automated database, and the attempt to convert to ADO went badly. (You can find traces of my agony in this very forum.)

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
I'd use a Scripting.Dictionary object.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I would go with dictionary as well. It has an Exists method. For more on it have a look here.

Dictionary
 
I'm giving kudos and a star as I think this response will help someone (it went to jadams, thanks to the delightful reference to emerods in his tutorial example).

It's easy to see how this could be applied to my app, but my actual need doesn't rise to the level where I want to learn a new method. (It's this darn unrelated Linux hobby -- I'm out of brain slots for new keywords and such.) But MUCH THANKS.

[purple]If we knew what it was we were doing, it would not be called
research [blue]database development[/blue], would it? [tab]-- Albert Einstein[/purple]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top