473,729 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select Query with Comma Delimited Paramater

3 New Member
I am working on a website with ASP.NET 2.0

I started with a SQL database .mdf. After further discussions with my client, we decided to switch to an Access database.

I created a Select query that has a parameter that is a comma delimited string brought in through a paramater in to the <selectcommand= ...>

The query worked perfectly before, but I ran into a problem in the switch over because:

WHERE charindex(',' + CustomerID + ',', ',' + @customers + ',') > 0

Doesn't translate to the Access server.

I know the InStr() function is intended to do the same purpose, but I can't get it to do the same thing that the charindex did in SQL.

Essentially, I need a quick and easy method to translate a comma delimited paramater in a select query. The simpler the better, its a short string derived from some user input. I'm not worried about database injection because the selections are derived from predetermined values, not typed strings.

I've been searching for hours to find a solution, but have had no luck, so I'm going to the Access PRO's on this one.

Thanks,
FB
May 12 '07 #1
5 7277
ADezii
8,834 Recognized Expert Expert
I am working on a website with ASP.NET 2.0

I started with a SQL database .mdf. After further discussions with my client, we decided to switch to an Access database.

I created a Select query that has a parameter that is a comma delimited string brought in through a paramater in to the <selectcommand= ...>

The query worked perfectly before, but I ran into a problem in the switch over because:

WHERE charindex(',' + CustomerID + ',', ',' + @customers + ',') > 0

Doesn't translate to the Access server.

I know the InStr() function is intended to do the same purpose, but I can't get it to do the same thing that the charindex did in SQL.

Essentially, I need a quick and easy method to translate a comma delimited paramater in a select query. The simpler the better, its a short string derived from some user input. I'm not worried about database injection because the selections are derived from predetermined values, not typed strings.

I've been searching for hours to find a solution, but have had no luck, so I'm going to the Access PRO's on this one.

Thanks,
FB
  1. The same functionality that CHARINDEX provides in TransactSQL can be found in the Instr() Function in Access, namely returning the Starting Position of a specific expression in a character string. The following SQL Statement will produce the Last and First Names of all Employees who have the string 'ch' contained somewhere within the [LastName] Field.
    Expand|Select|Wrap|Line Numbers
    1. SELECT [LastName], [FirstName]
    2. FROM tblEmployee Where InStr([LastName],'ch') > 0;
  2. I'm not exactly sure what you mean when you request how to
    translate a comma delimited paramater in a select query
    Please explain in more detail.
May 12 '07 #2
Frank Jovi
3 New Member
Thank you for responding, first of all :) I really appreciate the time you're taking!

So, first... I am passing a series of paramaters to a SQLdataSource.

srcMarket.Selec tParameters("Re gions").Default Value = RegionStr
srcMarket.Selec tParameters("Da teFrom").Defaul tValue = DFV
srcMarket.Selec tParameters("Da teUntil").Defau ltValue = DUV
srcMarket.Selec tParameters("It em").DefaultVal ue = ItemV

RegionStr will look something like this, passed as a string.

10021,10027,100 34,10019,10054, 10055,10007,

I want the WHERE command to select each RegionID from each location in the table that matches that region. The problem is that it can be a variable number of items, so passing it as a string and then parsing it is the easiest method i know how. The charindex function did it in SQL, so I'm sure it can be done in Access. When I try different variations in Access, either I can't seem to get the formatting correct, or the InStr can't handle it for some reason? Any viable solution is an option right now.

I've tried surrounding the market.regionid with ',' (commas) so that it can parse the column data with the commas included.

(InStr(',' & [Market.RegionID] & ',', ',' & @Region & ',') > 0)

What should happen is it checks each regionID column against the @regions string and returns the ones that match the string.

However, I'm getting no difference when I add in the InStr function to the WHERE. It just returns nothing whatsoever.



  1. The same functionality that CHARINDEX provides in TransactSQL can be found in the Instr() Function in Access, namely returning the Starting Position of a specific expression in a character string. The following SQL Statement will produce the Last and First Names of all Employees who have the string 'ch' contained somewhere within the [LastName] Field.
    Expand|Select|Wrap|Line Numbers
    1. SELECT [LastName], [FirstName]
    2. FROM tblEmployee Where InStr([LastName],'ch') > 0;
  2. I'm not exactly sure what you mean when you request how to

    Please explain in more detail.
May 12 '07 #3
ADezii
8,834 Recognized Expert Expert
Thank you for responding, first of all :) I really appreciate the time you're taking!

So, first... I am passing a series of paramaters to a SQLdataSource.

srcMarket.Selec tParameters("Re gions").Default Value = RegionStr
srcMarket.Selec tParameters("Da teFrom").Defaul tValue = DFV
srcMarket.Selec tParameters("Da teUntil").Defau ltValue = DUV
srcMarket.Selec tParameters("It em").DefaultVal ue = ItemV

RegionStr will look something like this, passed as a string.

10021,10027,100 34,10019,10054, 10055,10007,

I want the WHERE command to select each RegionID from each location in the table that matches that region. The problem is that it can be a variable number of items, so passing it as a string and then parsing it is the easiest method i know how. The charindex function did it in SQL, so I'm sure it can be done in Access. When I try different variations in Access, either I can't seem to get the formatting correct, or the InStr can't handle it for some reason? Any viable solution is an option right now.

I've tried surrounding the market.regionid with ',' (commas) so that it can parse the column data with the commas included.

(InStr(',' & [Market.RegionID] & ',', ',' & @Region & ',') > 0)

What should happen is it checks each regionID column against the @regions string and returns the ones that match the string.

However, I'm getting no difference when I add in the InStr function to the WHERE. It just returns nothing whatsoever.
The best I can do right now is hopefully point you in the right direction since I am really pressed for time and heading out for a wedding. I've listed the Syntax below for parsing a String based on a specific Delimiter and putting its values into an Array. Let me know how you make out.
Expand|Select|Wrap|Line Numbers
  1. Dim varRet As Variant, intCounter As Integer
  2. Dim strToParse As String
  3.  
  4. strToParse = "10021, 10027, 10034, 10019, 10054, 10055, 10007"
  5.  
  6. '2nd Argument to the Splpit() Function is the Delimiter
  7. varRet = Split(strToParse, ",")
  8.  
  9. 'Total # of Array Elements = UBound(varRet) - 1
  10. For intCounter = 0 To UBound(varRet) - 1
  11.   Debug.Print Trim(varRet(intCounter))
  12. Next
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. 10021
  2. 10027
  3. 10034
  4. 10019
  5. 10054
  6. 10055
May 12 '07 #4
Frank Jovi
3 New Member
It took me a at least 8 hours, but I solved it... and the answer isn't anything very enlightening.

I had problems when I was working with the InStr feature as we discussed. I don't understand why at all, but it wasn't returning anything when I used the @Regions variable. I took it into Access Query Designer and worked with it from there. It kept giving me a Compile Error when I switched to the Datasheet view. Repeatedly. I saved it to take a break and went to the ASP.NET code. I worked with the ASP to no avail. When I came back, I don't know why, but it stopped giving me the Compile error and would pull up the data properly. I directly copied this to my ASP.NET page and it worked. Now, at first I thought this was magical, and amazing and absolutely puzzling. But, it gets even more confusing. I copied the string to a backup datasource in an old file to see what had changed. It wouldn't work again. I remembered I had adjusted the Paramaters default value, since it seemed like the paramaters weren't being passed correctly... I set the Default Value for Regions to one of the RegionIDs... This, for some reason, which I don't understand, fixed the problem. It accepted not only the default value, but also a string of values and worked properly. I've never had to set a default value before, and the other paramaters I have aren't set to any default value.

I don't know why when I saved the query and then reopened it the compile error disappeared. On top of that, I don't know why a default value had to be set to make the query work, so I I don't see the relevance. I had a confirmation label.text to ensure that the variable was being set correctly, which it was. However, setting the default value seems to have fixed the problem, so perhaps its some kind of rare bug.

Thanks for your help very much! I really appreciate your time, and I'll definitely spread the word this is the place to get some fast and good answers. :)

F
May 12 '07 #5
ADezii
8,834 Recognized Expert Expert
It took me a at least 8 hours, but I solved it... and the answer isn't anything very enlightening.

I had problems when I was working with the InStr feature as we discussed. I don't understand why at all, but it wasn't returning anything when I used the @Regions variable. I took it into Access Query Designer and worked with it from there. It kept giving me a Compile Error when I switched to the Datasheet view. Repeatedly. I saved it to take a break and went to the ASP.NET code. I worked with the ASP to no avail. When I came back, I don't know why, but it stopped giving me the Compile error and would pull up the data properly. I directly copied this to my ASP.NET page and it worked. Now, at first I thought this was magical, and amazing and absolutely puzzling. But, it gets even more confusing. I copied the string to a backup datasource in an old file to see what had changed. It wouldn't work again. I remembered I had adjusted the Paramaters default value, since it seemed like the paramaters weren't being passed correctly... I set the Default Value for Regions to one of the RegionIDs... This, for some reason, which I don't understand, fixed the problem. It accepted not only the default value, but also a string of values and worked properly. I've never had to set a default value before, and the other paramaters I have aren't set to any default value.

I don't know why when I saved the query and then reopened it the compile error disappeared. On top of that, I don't know why a default value had to be set to make the query work, so I I don't see the relevance. I had a confirmation label.text to ensure that the variable was being set correctly, which it was. However, setting the default value seems to have fixed the problem, so perhaps its some kind of rare bug.

Thanks for your help very much! I really appreciate your time, and I'll definitely spread the word this is the place to get some fast and good answers. :)

F
I am just as befuddled as you are - I see absolutley no reason why setting a Default Value would have corrected the problem and eliminated the Compilation Error. Could it possibly have been the Default Value implying a different Data Type that the Variable was coerced into? Who knows - soemtimes in this business we never find the answer. Congratulations on solving the problem and thanks for spreading the word!
May 13 '07 #6

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

Similar topics

4
2820
by: Arne | last post by:
From: "Arne de Booij" <a_de_booij@hotmail.com> Subject: Comma delimited array into DB problems Date: 9. februar 2004 10:39 Hi, I have an asp page that takes input from a form on the previous page, puts that into an array and inserts the array into SQL server. Now here is the problem:
1
595
by: Craig Stadler | last post by:
Can someone help with query syntax regarding IN/EXISTS.. I'm trying to do this: insert into table2 (field1) select field1 from table1 where field1 not in (select field1 from table2) delete from table1 where field1 in (select field1 from table2) 1. Insert field1 from table1 into table2 if it doesn't already exist there...
5
5390
by: Pat L | last post by:
I have a function that is designed to return a variable that contains concatenated values from a partinular field in the returned rows: DECLARE @output varchar(8000) SELECT @output = CASE WHEN @output IS NULL THEN CAST(TSD.ScheduledTime AS varchar(4)) ELSE @output+ ', '+ ISNULL(CAST(TSD.ScheduledTime AS
2
4234
by: JS | last post by:
Let's say I have two tables: t1 myname apple banana cherry t2 myname | value
1
2287
by: Mark | last post by:
How can I use the selections in a multi-select listbox as criteria in a query? Thanks! Mark
6
4845
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID SalesManName AT Alan Time
1
18143
by: Razia | last post by:
Hello everyone I want to create a select query according to the values in a single dimension array Select * from <table Name> where <Array items> Can anybody give me some suggestions
3
2163
by: Phil Stanton | last post by:
I have a number of queries which use code for the output of 1 or more fields. For example Address:GetAddress(AddressID, True, 60) Address ID Points to an Address in a table - Address Line1, Line 2, Line 3, Town, County, Post Code, Country. The True is whether to include the country and the 60 is the maximum line length (after which a line feed is inserted). All this is to output to a formatted RTF file for a handbook. In addition, I...
0
960
by: stevehogg | last post by:
I have a script that I used in SQL 2000 Query Analyzer. The output in the reult pane saved as a comma delimited file just fine. When I use the query anylzer equivilent in SQL2005 it puts some FF FE characters in the output at the beginng of the the first record and some otheers through out the file. I just want a plain ascii comma delimited file. Is there some sort of setting in the sql program that needs to be changed?
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.