Solved

check variable for multiple values

Posted on 2004-08-22
12
232 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

943 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now