Solved

check variable for multiple values

Posted on 2004-08-22
12
250 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…

839 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