Is there an "IN" statement in VBA like the one in SQL Server?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Kogan
    Recognized Expert New Member
    • Jul 2010
    • 107

    #1

    Is there an "IN" statement in VBA like the one in SQL Server?

    I was trying Google an answer for this question and couldn't find a good substitute for SQL's IN feature in VBA from the postings that came up.

    In VBA the closest I could get to a construct of:
    Code:
       If myVar in("abc", "def", "hgi") then
          'insert code here
       End If
    is:

    Code:
    Select Case myVar
        Case "abc", "def", "hgi"
            'insert code here
    End Select
    That's okay, but does someone have a function to simulate SQL's 'in' statement in VBA? The idea is to replace the expression:
    myVar = "abc" or myVar = "def" or myVar = "hgi" etc... with something like:

    fnIn(myVar, "abc", "def", "hgi"... (string array)...)
  • gershwyn
    New Member
    • Feb 2010
    • 122

    #2
    I know of no function native to VBA that will duplicate SQL's IN statement. I assume what you dislike about the Select statement in this case is that it uses hardcoded values, and you'd like to work with something a little more dynamic.

    If you have your heart set on the behavior you described, one option is to roll your own. This function accepts a string value and an array, and returns true if the former is found within the latter:
    Code:
    Public Function fnIn(sCandidate As String, aList() As String)
      fnIn = False
      For Each Value In aList
        If sCandidate = Value Then
          fnIn = True
          Exit Function
        End If
      Next
    End Function

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      Good thinking Steve, but as Gershwyn says, there's nothing built-in.

      You already have my best idea included (Select Case) so the option is to roll your own, or not.

      Welcome to Bytes!

      Comment

      • Steven Kogan
        Recognized Expert New Member
        • Jul 2010
        • 107

        #4
        Thanks Gershwyn and NeoPa. That answers my question.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32669

          #5
          As an alternative, that allows for multiple parameters rather than an array, this may help :
          Code:
          Public Function In(strField As String, _
                             ParamArray avarArgs() As Variant) As Boolean
              Dim strVal As String
          
              'In starts automatically as False
              For Each strVal In avarArgs
                  If strField = strVal Then
                      In = True
                      Exit Function
                  End If
              Next strVal
          End Function
          This would be invoked in the following way :
          Code:
          If In(strLetter, "A", "B", "C", "D") Then _
              Call MsgBox("Found in A - D.")
          Last edited by NeoPa; Jul 13 '10, 12:18 PM.

          Comment

          Working...