Solved

check variable for multiple values

Posted on 2004-08-22
12
224 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 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…

707 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

15 Experts available now in Live!

Get 1:1 Help Now