473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

107 Expert 100+
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:
Expand|Select|Wrap|Line Numbers
  1.    If myVar in("abc", "def", "hgi") then
  2.       'insert code here
  3.    End If 
is:

Expand|Select|Wrap|Line Numbers
  1. Select Case myVar
  2.     Case "abc", "def", "hgi"
  3.         'insert code here
  4. 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)...)
Jul 9 '10 #1
4 1210
gershwyn
122 100+
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:
Expand|Select|Wrap|Line Numbers
  1. Public Function fnIn(sCandidate As String, aList() As String)
  2.   fnIn = False
  3.   For Each Value In aList
  4.     If sCandidate = Value Then
  5.       fnIn = True
  6.       Exit Function
  7.     End If
  8.   Next
  9. End Function
Jul 9 '10 #2
NeoPa
32,556 Expert Mod 16PB
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!
Jul 10 '10 #3
Steven Kogan
107 Expert 100+
Thanks Gershwyn and NeoPa. That answers my question.
Jul 12 '10 #4
NeoPa
32,556 Expert Mod 16PB
As an alternative, that allows for multiple parameters rather than an array, this may help :
Expand|Select|Wrap|Line Numbers
  1. Public Function In(strField As String, _
  2.                    ParamArray avarArgs() As Variant) As Boolean
  3.     Dim strVal As String
  4.  
  5.     'In starts automatically as False
  6.     For Each strVal In avarArgs
  7.         If strField = strVal Then
  8.             In = True
  9.             Exit Function
  10.         End If
  11.     Next strVal
  12. End Function
This would be invoked in the following way :
Expand|Select|Wrap|Line Numbers
  1. If In(strLetter, "A", "B", "C", "D") Then _
  2.     Call MsgBox("Found in A - D.")
Jul 13 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: ivan divandelen | last post by:
Hi, How can i pass a parameter SQL stored Procedure from ASP pages
1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
3
by: Dave | last post by:
Hi guys, I have hit this bug more than once and was wondering if anyone else has ever seen it? SELECT A.nId ,B.nId FROM Server1.myDB_1.dbo.TableA A LEFT OUTER JOIN Server2.myDB_2.dbo.TableB...
4
by: Prince Kumar | last post by:
I joined a company recently and they have a java program which hangs (does nothing) after a while. This is no way consistent. It could succeed quite a few times and can fail a few other times....
4
by: banz | last post by:
Hello I have a problem to resolve: I wrote a Perlscript which caches data from a server (local on my machine) I would like to have a other connection to a remote server but I don't know how to...
5
by: Tom | last post by:
Hi I am trying to transfer to a different .ASPX page using Server.Transfer. However, I get the following error: "Error executing child request for .aspx." Anyone know why? Thanks for...
4
by: louise raisbeck | last post by:
I have this scenario (simplified) function addnewdata () { check for partial match already in db for information entered by user if (partialmatch succeeds) { open new window aspx page (using...
2
by: Charles Wilt | last post by:
I have a IBM iSeries (aka AS-400) running v5r3 of OS/400 that I access via a linked server from SQL Server 2000. The following select works fine: select * from...
9
by: =?Utf-8?B?TWlrZVM=?= | last post by:
Can anyone tell me how to insert variables for server info in a SQL connection statement. For example, in the following statement: ("Server=KIN-SSQL02;" & "Database=FTQ;" & "User ID=abc;" &...
17
by: Anil Gupte | last post by:
I am using the following to try to connect to the database, but it does not seem to be working. Dim sConnString sConnString = "Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.