Solved

check variable for multiple values

Posted on 2004-08-22
12
264 Views
Last Modified: 2010-05-02
I want to check a variable for multiple values in one easy statement.  For example, in SQL I can say if @X in ('dog','bird') to see if @X contains those 2 values.  In VB, the only way I know is "if x = "dog" or x = "bird" ".  Anything similar to this in VB other than the case statement, i.e.

select case x
 case "bird","dog"
    'code goes here
end select

0
Comment
Question by:TylerTheWimp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11865634
you can use instr() function:

if (instr(1,"bird dog cat",x) then ....
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11865641
to avoid any confusion, you can use a separator:

If InStr(1, ",bird,dog,cat,tiger,lion,elephant,", "," & x & ",") Then .....


This will avoid any confusion, notice that starting and ending commas are required.
0
 
LVL 2

Author Comment

by:TylerTheWimp
ID: 11865645
I need something that is not type specific.  So, it shouldn't matter if I 'm using ints, strings, objects, etc.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11865647
Sorry, maybe comma is not a good separator example, could be better:
f InStr(1, ":bird:dog:cat:tiger:lion:elephant:", ":" & x & ":") Then .....
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11865659

Dude, you answered your own question.  Try

Select Case X
  Case "bird", "dog"
    'code goes here
  Case Else
    'more code here
End Select
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11865662

For instance, try this:

  Dim a As String
 
  a = "cat"
 
  Select Case a
    Case "cat", "dog"
      MsgBox "Hello"
    Case Else
      MsgBox "Goodbye"
  End Select
 

0
 
LVL 7

Expert Comment

by:Enlade
ID: 11865672

Oh, my bad....I didn't see that you don't want to use case.  Sorry.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11865680
I think there is not such functionality, but you can declare an array:

Dim values() As Integer = {0, 1, 2, 3}

And create your own function like FindInArray(array, value) that returns true if a value is found in array.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11865690

Opps....gotta run...I can look at this later....but my suggestion for anyone that might want to solve this before I get back is to look at Collections.  Create a collection that contains each item ("dog", "cat", etc).  Gotta run.....
0
 
LVL 2

Author Comment

by:TylerTheWimp
ID: 11865694
The motive behind this question is laziness and obsessive complusive tendencies.  I hate writing out the variable name over and over again for one if statement.  If there is only one if statement and no "else" code to execute then I don't want to use a case statement since its ugly:

select case vSomeVariable
  case 1000.5, 1, "my name is wimp"
    'do code
  case else
    'do nothing
end select

i want a one liner soution:  if vSomeVariable in (1000.5, 1, "my name is wimp") then debug.print "stuff"
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 11865718
Maybe an "in" function could be very useful, still you can use my suggestion converting the searched value to string previous to calling instr, but, to maintain a collection of heterogeneous values (ints, strings, etc,) is definitively not a good practice.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 11865820
Well, I see you've already accepted an answer but I'd still like to offer the following for whatever it's worth.

Private Sub Command1_Click()
    If ContainedIn("Joe", 1, Text1.Text, "joe", "Joe") Then
        Debug.Print "Found It"
    End If
End Sub

Private Function ContainedIn(varValue1 As Variant, ParamArray varValue2() As Variant) As Boolean
    Dim intTests As Integer
    For intTests = 0 To UBound(varValue2)
        If varValue1 = varValue2(intTests) Then
            ContainedIn = True
            Exit For
        End If
    Next
End Function
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

691 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