Results 1 to 4 of 4
  1. #1
    merebag is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Sep 2016
    Posts
    19

    VBA equivalent to SQL NOT IN statement

    What is the best way in VBA to test for a number not in an array or set of numbers? Similar to a SQL NOT IN statement?



    Example:

    Test to see if the number 15 is in a set of numbers (1,4,15,29)

    Something like
    Dim mynumber as integer

    mynumber = 15

    If mynumber is NOT IN (1,4,15,29) etc....

    How would I code this?

  2. #2
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Depends. Is this (1,4,15,29) an array or a string? I know you used the word array, but also wrote "or set of numbers".
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    merebag is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Sep 2016
    Posts
    19
    I am assuming it would be a numeric array. Or would that be the best way?

    I just need to check a variable against a set of numbers to make sure it is not in that set.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Quote Originally Posted by merebag View Post
    I just need to check a variable against a set of numbers to make sure it is not in that set.
    I understand that. If it's a simple string, you could probably use the Find function. If it's an actual array, you have to split the elements then loop through them. Array has a different meaning in Access than it does for mathematics. The former is an actual object in memory. The latter can be considered a string for Access purposes.

    If you want to use a db type of array (one dimensional), this is about the simplest method I've seen, which you can find here
    http://stackoverflow.com/questions/1...ng-in-an-array
    Code:
    Sub Test()
      Dim arr As Variant
      arr = Split("abc,def,ghi,jkl", ",")
      Debug.Print IsInArray("ghi", arr)
    End Sub
    
    Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
      IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Vlookup equivalent in Access
    By Eddie in forum Access
    Replies: 2
    Last Post: 04-01-2014, 09:43 AM
  2. AndAlso equivalent in VBA?
    By Heatshiver in forum Programming
    Replies: 4
    Last Post: 05-02-2012, 12:05 PM
  3. Oracle LAG Equivalent
    By OzzyMiner in forum Queries
    Replies: 2
    Last Post: 03-10-2011, 11:41 AM
  4. Is there equivalent code in VBA?
    By techneophyte in forum Programming
    Replies: 4
    Last Post: 08-02-2010, 07:11 AM
  5. WeekdayName equivalent in 2000
    By sunnybrook in forum Queries
    Replies: 8
    Last Post: 07-14-2010, 10:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums