Check to see if object key is in collection

  • Thread starter PacFreshAnthony via AccessMonster.com
  • Start date
P

PacFreshAnthony via AccessMonster.com

New to classes and collections. Is there a way to test if a unique key is in
a collection? Right now I'm using this and it works - but I think there has
to be a better way.

Public Function BadgeExists(col As Collection, key As String) As Boolean
Dim check As Integer
Dim e As Employee

For Each e In Employees
If e.Badge = key Then
check = check + 1
End If
Next

BadgeExists = check
Debug.Print BadgeExists
End Function

Employee is a class and Employees is a collection of Employee. I Assign the
badge as the object key name in the collection.

I've tried using this code I found but it keeps returning false even if the
Key Exists

public Function KeyExists(col as Collection, key as String) as Boolean
Dim ret as Variant
On Error Resume Next
ret = col.Item(key) <- checked the debugger and ret is always empty
if err.Number<>0 Then
KeyExists = False
Else
KeyExists = True
End if
End function

Any Help would be appreciated
Thank you,
Anthony
 
P

PacFreshAnthony via AccessMonster.com

PacFreshAnthony said:
New to classes and collections. Is there a way to test if a unique key is in
a collection? Right now I'm using this and it works - but I think there has
to be a better way.

Public Function BadgeExists(col As Collection, key As String) As Boolean
Dim check As Integer
Dim e As Employee

For Each e In Employees
If e.Badge = key Then
check = check + 1
End If
Next

BadgeExists = check
Debug.Print BadgeExists
End Function

Employee is a class and Employees is a collection of Employee. I Assign the
badge as the object key name in the collection.

I've tried using this code I found but it keeps returning false even if the
Key Exists

public Function KeyExists(col as Collection, key as String) as Boolean
Dim ret as Variant
On Error Resume Next
ret = col.Item(key) <- checked the debugger and ret is always empty
if err.Number<>0 Then
KeyExists = False
Else
KeyExists = True
End if
End function

Any Help would be appreciated
Thank you,
Anthony

I made some changes to the found code and it works:

Public Function KeyExists(col As Collection, key As String) As Boolean
Dim ret As Variant
On Error Resume Next
ret = col.Item(key)
KeyExists = (Err.Number = 438)
End Function

Is there a better way?

Thank you,
Anthony
 
D

Dirk Goldgar

PacFreshAnthony via AccessMonster.com said:
I made some changes to the found code and it works:

Public Function KeyExists(col As Collection, key As String) As Boolean
Dim ret As Variant
On Error Resume Next
ret = col.Item(key)
KeyExists = (Err.Number = 438)
End Function

Is there a better way?


That's the best way I have found. Some people hold that your first approach
(looping through all the items) is better, because they prefer never to
raise an error, even an anticipated one. My view is that using the
collection's hidden index is better, but there are arguments on both sides.
Performance-wise, it probably depends on how many items you expect the
collection to hold, on average, but I've never done any benchmark tests.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top