Solved

check variable for multiple values

Posted on 2004-08-22
12
255 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
Technology Partners: 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

730 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