A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Query using multiple values from one text box



 
 
Thread Tools Display Modes
  #1  
Old May 14th, 2010, 02:14 PM posted to microsoft.public.access.queries
shm135
external usenet poster
 
Posts: 26
Default Query using multiple values from one text box

Hi,

I have a form called Test. On this form, I have a textbox where I can
type query criteria. I would like to be able to type multiple criteria
into this textbox and use this textbox to filter my query results.

For example, on my Test form textbox, I'd like to be able to type
("Alaska" OR "Alabama") and would like to then filter my query based
on what I type into this textbox.

Currently, when I type one thing into the textbox "Alaska," the query
properly filters to all results that equal "Alaska." However, when I
type another piece of critiera "AND Alabama" or "OR Alabama", the
query returns no results.

How can I accomplish this?

Thanks
  #2  
Old May 14th, 2010, 02:41 PM posted to microsoft.public.access.queries
ghetto_banjo
external usenet poster
 
Posts: 325
Default Query using multiple values from one text box


Well, I don't think it will work by directly passing that textbox
value to the query. The query ends up searching for the actual string
of "Alaska OR Alabama", which of course returns no results..


You could use some VB code to parse the string and manually generate
your WHERE clause of the SQL statement. Using the InStr() funciton,
you could search for strings such as " OR " and " AND ", and determine
from there how to build out the WHERE clause of the SQL. It's not the
simplest thing to code, but it's certainly possible. If you want to
try this route, we can assist with how the code will look.


Or you might be able to just use multiple text boxes on your form.
  #3  
Old May 14th, 2010, 03:11 PM posted to microsoft.public.access.queries
Tom van Stiphout[_2_]
external usenet poster
 
Posts: 1,653
Default Query using multiple values from one text box

On Fri, 14 May 2010 06:14:37 -0700 (PDT), shm135
wrote:

I'm guessing you have a query:
select * from myTable where StateName = Forms!myForm!myTextbox
You later use this query as the recordsource for some form or report.

The reason your current solution does not work is because there is no
StateName with a value of Alaska OR Alabama, regardless of how you put
the doublequotes.
The solution is to use an IN clause:
select * from myTable where StateName in ('Alaska', 'Alabama')
(note that I use single-quotes; will come in handy below)
I have noticed in the past that this does not work in a query:
select * from myTable where StateName in (Forms!myForm!myTextbox)

The workaround is to forget about the query and assign the
concatenated string to the recordsource property directly:
private sub form_open
if isnull(Forms!myForm!myTextbox) then
msgbox "Yo! Gimme some state(s)"
else
me.recordsource = "select * from myTable where StateName in (" &
Forms!myForm!myTextbox & ")"
end sub
Now if you put 'Alaska', 'Alabama' in the textbox it should work.

-Tom.
Microsoft Access MVP



Hi,

I have a form called Test. On this form, I have a textbox where I can
type query criteria. I would like to be able to type multiple criteria
into this textbox and use this textbox to filter my query results.

For example, on my Test form textbox, I'd like to be able to type
("Alaska" OR "Alabama") and would like to then filter my query based
on what I type into this textbox.

Currently, when I type one thing into the textbox "Alaska," the query
properly filters to all results that equal "Alaska." However, when I
type another piece of critiera "AND Alabama" or "OR Alabama", the
query returns no results.

How can I accomplish this?

Thanks

  #4  
Old May 14th, 2010, 03:14 PM posted to microsoft.public.access.queries
Jerry Whittle
external usenet poster
 
Posts: 4,732
Default Query using multiple values from one text box

If it's something pretty static, like all the US States and Territories, you
could save that data into a table then base a multi-select list box on it.
Then it's just a matter of clicking on all the state names. As a bonus, you
don't have to worry about misspelling things like Conneticut. ;-)
--
Jerry Whittle, Microsoft Access MVP
Light. Strong. Cheap. Pick two. Keith Bontrager - Bicycle Builder.


"shm135" wrote:

Hi,

I have a form called Test. On this form, I have a textbox where I can
type query criteria. I would like to be able to type multiple criteria
into this textbox and use this textbox to filter my query results.

For example, on my Test form textbox, I'd like to be able to type
("Alaska" OR "Alabama") and would like to then filter my query based
on what I type into this textbox.

Currently, when I type one thing into the textbox "Alaska," the query
properly filters to all results that equal "Alaska." However, when I
type another piece of critiera "AND Alabama" or "OR Alabama", the
query returns no results.

How can I accomplish this?

Thanks
.

  #5  
Old May 14th, 2010, 04:03 PM posted to microsoft.public.access.queries
KARL DEWEY
external usenet poster
 
Posts: 10,767
Default Query using multiple values from one text box

Another way, not the best, is to enter items separated by a space like this --
Alaska Alabama Texas
Use criteria --
Like "*" & [Enter item(s) for search] & "*"

It has drawbacks in that if you enter Bill it will pull Bill,
Billboard, and Billards.

--
Build a little, test a little.


"shm135" wrote:

Hi,

I have a form called Test. On this form, I have a textbox where I can
type query criteria. I would like to be able to type multiple criteria
into this textbox and use this textbox to filter my query results.

For example, on my Test form textbox, I'd like to be able to type
("Alaska" OR "Alabama") and would like to then filter my query based
on what I type into this textbox.

Currently, when I type one thing into the textbox "Alaska," the query
properly filters to all results that equal "Alaska." However, when I
type another piece of critiera "AND Alabama" or "OR Alabama", the
query returns no results.

How can I accomplish this?

Thanks
.

  #6  
Old May 14th, 2010, 05:00 PM posted to microsoft.public.access.queries
shm135
external usenet poster
 
Posts: 26
Default Query using multiple values from one text box

On May 14, 10:11*am, Tom van Stiphout wrote:
On Fri, 14 May 2010 06:14:37 -0700 (PDT), shm135
wrote:

I'm guessing you have a query:
select * from myTable where StateName = Forms!myForm!myTextbox
You later use this query as the recordsource for some form or report.

The reason your current solution does not work is because there is no
StateName with a value of Alaska OR Alabama, regardless of how you put
the doublequotes.
The solution is to use an IN clause:
select * from myTable where StateName in ('Alaska', 'Alabama')
(note that I use single-quotes; will come in handy below)
I have noticed in the past that this does not work in a query:
select * from myTable where StateName in (Forms!myForm!myTextbox)

The workaround is to forget about the query and assign the
concatenated string to the recordsource property directly:
private sub form_open
if isnull(Forms!myForm!myTextbox) then
* msgbox "Yo! Gimme some state(s)"
else
* me.recordsource = "select * from myTable where StateName in (" &
Forms!myForm!myTextbox & ")"
end sub
Now if you put 'Alaska', 'Alabama' in the textbox it should work.

-Tom.
Microsoft Access MVP



Hi,


I have a form called Test. On this form, I have a textbox where I can
type query criteria. I would like to be able to type multiple criteria
into this textbox and use this textbox to filter my query results.


For example, on my Test form textbox, I'd like to be able to type
("Alaska" OR "Alabama") and would like to then filter my query based
on what I type into this textbox.


Currently, when I type one thing into the textbox "Alaska," the query
properly filters to all results that equal "Alaska." However, when I
type another piece of critiera "AND Alabama" or "OR Alabama", the
query returns no results.


How can I accomplish this?


Thanks- Hide quoted text -


- Show quoted text -


Thank you all for your responses! I think I may have accidentally
marked one of your responses as SPAM- for that, I apologize!

You're right Tom. This is what I am doing now and it is not working:
select * from myTable where StateName in (Forms!myForm!myTextbox)

To further elaborate- I have a multiselect listbox, whose AFTERUPDATE
event transfers my selections to a textbox, with the word OR in
between. Then, I am setting the query criteria equal to the textbox.
If I make one selection, it works. More than one selection and it does
not work. In the end, I want to use my selections for an UPDATE query.
How can I go about this using the recordsource method you describe
above.

Essentially, I want to first pull out all of the records whose
statename is equal to my form textbox. Then, I want to be able to
update the Point of Contact field for multiple states at one time. How
do I do this?

Thanks!
  #7  
Old May 20th, 2010, 10:02 PM posted to microsoft.public.access.queries
shm135
external usenet poster
 
Posts: 26
Default Query using multiple values from one text box

On May 14, 12:00*pm, shm135 wrote:
On May 14, 10:11*am, Tom van Stiphout wrote:





On Fri, 14 May 2010 06:14:37 -0700 (PDT), shm135
wrote:


I'm guessing you have a query:
select * from myTable where StateName = Forms!myForm!myTextbox
You later use this query as the recordsource for some form or report.


The reason your current solution does not work is because there is no
StateName with a value of Alaska OR Alabama, regardless of how you put
the doublequotes.
The solution is to use an IN clause:
select * from myTable where StateName in ('Alaska', 'Alabama')
(note that I use single-quotes; will come in handy below)
I have noticed in the past that this does not work in a query:
select * from myTable where StateName in (Forms!myForm!myTextbox)


The workaround is to forget about the query and assign the
concatenated string to the recordsource property directly:
private sub form_open
if isnull(Forms!myForm!myTextbox) then
* msgbox "Yo! Gimme some state(s)"
else
* me.recordsource = "select * from myTable where StateName in (" &
Forms!myForm!myTextbox & ")"
end sub
Now if you put 'Alaska', 'Alabama' in the textbox it should work.


-Tom.
Microsoft Access MVP


Hi,


I have a form called Test. On this form, I have a textbox where I can
type query criteria. I would like to be able to type multiple criteria
into this textbox and use this textbox to filter my query results.


For example, on my Test form textbox, I'd like to be able to type
("Alaska" OR "Alabama") and would like to then filter my query based
on what I type into this textbox.


Currently, when I type one thing into the textbox "Alaska," the query
properly filters to all results that equal "Alaska." However, when I
type another piece of critiera "AND Alabama" or "OR Alabama", the
query returns no results.


How can I accomplish this?


Thanks- Hide quoted text -


- Show quoted text -


Thank you all for your responses! I think I may have accidentally
marked one of your responses as SPAM- for that, I apologize!

You're right Tom. This is what I am doing now and it is not working:
select * from myTable where StateName in (Forms!myForm!myTextbox)

To further elaborate- I have a multiselect listbox, whose AFTERUPDATE
event transfers my selections to a textbox, with the word OR in
between. Then, I am setting the query criteria equal to the textbox.
If I make one selection, it works. More than one selection and it does
not work. In the end, I want to use my selections for an UPDATE query.
How can I go about this using the recordsource method you describe
above.

Essentially, I want to first pull out all of the records whose
statename is equal to my form textbox. Then, I want to be able to
update the Point of Contact field for multiple states at one time. How
do I do this?

Thanks!- Hide quoted text -

- Show quoted text -


Does anyone have any input to help? Thanks for your time.
  #8  
Old May 20th, 2010, 10:39 PM posted to microsoft.public.access.queries
PieterLinden via AccessMonster.com
external usenet poster
 
Posts: 307
Default Query using multiple values from one text box

shm135 wrote:
I'm guessing you have a query:
select * from myTable where StateName = Forms!myForm!myTextbox

[quoted text clipped - 66 lines]

- Show quoted text -


Does anyone have any input to help? Thanks for your time.


What did you need help with?

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...eries/201005/1

  #9  
Old May 21st, 2010, 09:26 PM posted to microsoft.public.access.queries
shm135
external usenet poster
 
Posts: 26
Default Query using multiple values from one text box

On May 20, 5:39*pm, "PieterLinden via AccessMonster.com" u49887@uwe
wrote:
shm135 wrote:
I'm guessing you have a query:
select * from myTable where StateName = Forms!myForm!myTextbox

[quoted text clipped - 66 lines]


- Show quoted text -


Does anyone have any input to help? Thanks for your time.


What did you need help with?

--
Message posted via AccessMonster.comhttp://www.accessmonster.com/Uwe/Forums.aspx/access-queries/201005/1


have a multiselect listbox, whose AFTERUPDATE
event transfers my selections to a textbox, with the word OR in
between. Then, I am setting the query criteria equal to the textbox.
If I make one selection, it works. More than one selection and it
does
not work. In the end, I want to use my selections for an UPDATE
query.
How can I go about this using the recordsource method you describe
above.


Essentially, I want to first pull out all of the records whose
statename is equal to my form textbox. Then, I want to be able to
update the Point of Contact field for multiple states at one time.
How
do I do this?
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 09:33 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.