How do I pass multiple criteria into a single parameter in vba?

Posted on 2010-09-08
Medium Priority
Last Modified: 2012-05-10
I'd like to be able to pass multiple criteria into a single parameter in a vba code which calls a sql procedure.

In creating the command to call the procedure I've used is:
      cmd1.Parameters("@clientcode") = "AB3,BM1"
which returns no result.

The section of the sql code which relates to the parameter is:
      where T01.ClientCode in (@clientcode)

Any thoughts?  
Question by:ajnorton
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 58

Expert Comment

ID: 33625902
Create the function below, then change your code from

      where T01.ClientCode in (@clientcode)


      where T01.ClientCode in (select value from values2table(@clientcode, ','))
CREATE function [dbo].[values2table]
@values varchar(max),
@separator varchar(2)
) returns @res table ([value] varchar(100))
declare @value int
declare @commapos int, @lastpos int
set @commapos = 0
select @lastpos = @commapos, @commapos = charindex(@separator, @values, @lastpos+1)
while @commapos > @lastpos
	select @value = substring(@values, @lastpos+1, @commapos-@lastpos-1)
	insert into @res select @value where @value <> ''
	select @lastpos = @commapos, @commapos = charindex(@separator, @values, @lastpos+1)
select @value = substring(@values, @lastpos+1, len(@values))
insert into @res select @value where @value <> ''

Open in new window


Accepted Solution

WiB earned 1000 total points
ID: 33626432
you may not assign parameter @clientcode to your cmd1, but may just format  the CommandText,
inserting "AB3,BM1" or whatever you need into the string (formatting the string) that contains the command text

Author Comment

ID: 33626938
Thanks for the reply.
I've just got to duck out of office and will look at it in more detail tomorrow.
Thanks again!

Author Comment

ID: 33767498
Sorry its taken so long in getting back.

Thanks for the help!

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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