find in a collection by key

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

find in a collection by key

Post by sal21 »

I fill a collection with:

....
TROVA = GIORNO & "-" & NOMINATIVO & "-" & NUMCELLULARE
SCHIAVE = RIGA & "-" & COLONNA
COLLETT.Add Key:=SCHIAVE, Item:=TROVA
....

TROVA = GIORNO & "-" & NOMINATIVO & "-" & NUMCELLULARE
debug.print TROVA
01/08/2022-ARTURO-(+39)-78678611
debug.print SCHIAVE
1-4

now i have a varc=1 and varr=4, how to return the element of colletion, based this two var?

in my case i need:
01/08/2022-ARTURO-(+39)-78678611

peraphs i can use the key of collection and match with varc&"-"&varr to find?

or i need to use a dictionary...

note:
COLLETT is a Public var
..

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: find in a collection by key

Post by HansV »

Code: Select all

Debug.Print COLLETT(varr & "-" & varc)
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: find in a collection by key

Post by sal21 »

HansV wrote:
14 Feb 2022, 11:50

Code: Select all

Debug.Print COLLETT(varr & "-" & varc)
tks!!!!!!!!!
WORK! :clapping:

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: find in a collection by key

Post by sal21 »

sal21 wrote:
14 Feb 2022, 12:50
HansV wrote:
14 Feb 2022, 11:50

Code: Select all

Debug.Print COLLETT(varr & "-" & varc)
tks!!!!!!!!!
WORK! :clapping:
Only One...
To check if Key not existst i can use
A nothing stetement or?
And
With a collet.remove, are deleted Key and item from collection or?

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: find in a collection by key

Post by HansV »

You could use code like this:

Code: Select all

    On Error Resume Next
    Itm = COLLETT(varr & "-" & varc)
    On Error GoTo 0
    If Itm = "" Then
        ' Item does not exist
    Else
        ' Item exists
    End If
The Remove method deletes both the key and the item.

By the way, the Dictionary object has a method Exists that returns True if the specified key exists, and False otherwise.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: find in a collection by key

Post by sal21 »

HansV wrote:
14 Feb 2022, 13:22
You could use code like this:

Code: Select all

    On Error Resume Next
    Itm = COLLETT(varr & "-" & varc)
    On Error GoTo 0
    If Itm = "" Then
        ' Item does not exist
    Else
        ' Item exists
    End If
The Remove method deletes both the key and the item.

By the way, the Dictionary object has a method Exists that returns True if the specified key exists, and False otherwise.

COLLET.REMOVE (varr & "-" & varc)
Is correct?

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: find in a collection by key

Post by HansV »

Yes, but you don't need the ( )

Code: Select all

COLLET.Remove varr & "-" & varc
Best wishes,
Hans