test if item exists in collection

P

Pierre

Hello,

In a VBA collection I need to test if an item exists or not.
How to do this without having to loop accross all the collection ?

Thanks.

Pierre.
 
B

bob

Use this query to show the objects and set your desired object as a
parameter:


SELECT
IIf(Nz(Object.Type,0)=5,"Queries",IIf(Parent.Name="Scripts","Macros",Parent.Name))
AS ObjectType, Object.Name AS ObjectName,
Switch([ObjectType]="Tables",1,[ObjectType]="Queries",2,[ObjectType]="Forms",3,[ObjectType]="Reports",4,[ObjectType]="Macros",5,[ObjectType]="Modules",6,True,0)
AS SortOrder, CurrentDb.Name AS WhatDatabase
FROM MSysObjects AS Parent INNER JOIN MSysObjects AS [Object] ON
Parent.Id=Object.ParentId
WHERE (((Left$(Nz(Object.Name," "),1))<>"~") And ((Left$(Nz(Object.Name,"
"),4))<>"Msys") And ((Parent.Type)=3));


Bob Galway
(e-mail address removed)
 
M

Marshall Barton

Pierre said:
In a VBA collection I need to test if an item exists or not.
How to do this without having to loop accross all the collection ?


Searching the collection is the preferred method. If
that's taking too long, try retrieving the item's value and
using error traping when the item is not in the collection.
 
J

John Nurick

In a VBA collection I need to test if an item exists or not.
How to do this without having to loop accross all the collection ?

Sometimes the answer is to use a Dictionary (with its Exists method)
rather than a collection.
 
D

David C. Holley

For that matter, why not just do a DLookup() or DCount() on MSysObjects?
Use this query to show the objects and set your desired object as a
parameter:


SELECT
IIf(Nz(Object.Type,0)=5,"Queries",IIf(Parent.Name="Scripts","Macros",Parent.Name))
AS ObjectType, Object.Name AS ObjectName,
Switch([ObjectType]="Tables",1,[ObjectType]="Queries",2,[ObjectType]="Forms",3,[ObjectType]="Reports",4,[ObjectType]="Macros",5,[ObjectType]="Modules",6,True,0)
AS SortOrder, CurrentDb.Name AS WhatDatabase
FROM MSysObjects AS Parent INNER JOIN MSysObjects AS [Object] ON
Parent.Id=Object.ParentId
WHERE (((Left$(Nz(Object.Name," "),1))<>"~") And ((Left$(Nz(Object.Name,"
"),4))<>"Msys") And ((Parent.Type)=3));


Bob Galway
(e-mail address removed)


Hello,

In a VBA collection I need to test if an item exists or not.
How to do this without having to loop accross all the collection ?

Thanks.

Pierre.
 

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

Similar Threads

checking if an item exists in a custom collection 2
collection obj 4
Collection in vba 2
Excel VBA 0
Collection Object 12
item not found in this collection 7
Test Object Existence 2
Checking if table exists 1

Top