Solved

Find something in a collection

Posted on 2001-08-03
11
171 Views
Last Modified: 2010-05-02
Forexample, I have a collection C = [1,2,3,45,32,21,7,9] and I have a variable A. I want to check if the value of A is a member of collection C. How do I do that in VB? Thanks..
0
Comment
Question by:veraps
11 Comments
 
LVL 4

Expert Comment

by:nutwiss
ID: 6348085
a collection of what?
0
 
LVL 4

Accepted Solution

by:
WolfgangKoenig earned 50 total points
ID: 6348088
Dim TestItem As Variant

For each TestItem in  C
  If TestItem = A Then MsgBox("Here it is !")
Next Test

Thats was it
Best regards ;O)
WoK
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6348095
Hi veraps,

Dim C() As Integer, A As Integer, i As Integer
Dim IsFound As Boolean

Redim Preserve C(7)

IsFound = False

c(0)=1
c(1)=2
c(2)=3
c(3)=45
c(4)=32
c(5)=21
c(6)=7
c(7)=9

For i = Lbound(C) to UBound(C)
    If A = C(i) Then IsFound = True:Exit For    
Next i

If IsFound Then Msgbox "Is Found" else Msgbox "Not Found"

'Hope will help.

0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6348099
Sorry:

Next TestItem

was right ...

_________________________________________________________
Here is a complete example:

Private Sub Form_Load()
Dim TestItem As Variant
Dim C As Collection
Dim A As Integer

A = 3
Set C = New Collection
C.Add 1
C.Add 2
C.Add 3
C.Add 45


For Each TestItem In C
 If TestItem = A Then MsgBox ("Item found !")
Next TestItem

End Sub
0
 
LVL 5

Expert Comment

by:jklmn
ID: 6348100
Hi,

Try this:

Private Sub Command1_Click()
Dim c As New Collection, i As Integer, a As Integer, aMember
For i = 1 To 8
c.Add Str(i)
Next i
a = 4
For Each aMember In c
If aMember = a Then
MsgBox "a is a member in collection c."
End If
Next
End Sub
0
 
LVL 13

Expert Comment

by:samic400
ID: 6348105
You would add an item to a collection.

 collection.add "a","a"

Then when you are doing a look-up for your variable, look up via the key.


  msgbox collection.item("a") - will return what the item is for that key.

If it cannot find it, then have some sort of error routine that it wasn't found.



 
0
 
LVL 1

Expert Comment

by:mcbeth
ID: 6348183
if you only use one collection in your app you can replace it with a dictionary ( make a reference to MS scripting runtime)

dim c as new dictionary

there is allready a function itemexits and you don't have to search by a for next loop
0
 
LVL 2

Expert Comment

by:TrueDrake
ID: 6348444
Hi,

If InStr(1, Text1.Text, "45", vbTextCompare) Or _
InStr(1, Text1.Text, "32", vbTextCompare) Then...

Enjoy
0
 
LVL 12

Expert Comment

by:Preece
ID: 6361656
Try this:

For lX = 1 to colC.Count
  if colC(lx) = varA then
    msgbox varA & " found at position " & lx & " of colX."
    exit For
  end if
next lX
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7198439
Hi veraps,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept WolfgangKoenig's comment(s) as an answer.

veraps, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7213221
Per recommendation, force-accepted.

Netminder
CS Moderator
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vb.net 2008 2 59
Run code from text file in vb 1 64
Using an encrypted  value to decrypt and display contents in vb6 9 52
How to read File Date Created using VB6 8 39
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question