collection: how to know key in/not in list?

H

Hills

Hi,

I use orderCollection to keep these opened instances of the order form
with it's orderID as a key. So multiple instances of one single order
form can be opened simultaniously. When one order form is closed, it's
quite simple to use collection's remove method to remove a key, and
after an order opens, add a key is simple too.
'=============================================
Public orderCollection As New Collection
'=============================================
Private Sub test(orderID As Integer)
Dim order As New Form_Order
'''''here I want to add code to see if Cstr(orderID) is already
'''''a key in the collection. But I don't know how to do it.
orderCollection.Add order, Cstr(orderID)
order.visible = true
End Sub

But I don't know if there is way to ensure that a key has already been
added to the collection or a key is not added into. I use some like:
If IsNull(orderCollection(Cstr(orderID)).Name) ..
If Not IsObject(orderCollection(Cstr(orderID)) ...etc. no luck.

So far the only way I found is to add the key anyway, without any
additional code, and catch the "key already in collection" error and
set the order to nothing if adding a duplicated key.

Any better ideas?

TIA.
 
A

Allen Browne

AFAIK, you have to walk through the collection to see if the value is
already there (or trap the error as you say).

If you did want the ability to open multiple instances of a form to the same
record, you could use the hWnd of the instance's window as the key to the
collection. Details:
http://allenbrowne.com/ser-35.html
 

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