473,398 Members | 2,427 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,398 software developers and data experts.

How to make this 'simple' query?

field- field-
surname town
---- ----

john NY
john Vegas
john Boston
eddie Boston
eddie New Orleans
eddie NY
burt Miami
burt Boston
hank NY

Hi all,
In the example you see the content of a table of persons with their favorit town
(the table consists of two fields, name and town)
How can a make a query that returns the persons who like certain towns?

for example: what persons like Boston and NY?
the answer is John and Eddie, BUT how do I do this in a query?
In real the table is much bigger and do I need to query with different towns
another example: who likes NY, Boston and Vegas, answer: john
(Sorry, maybe a simple question)

Best regards and thanks in advance,

Eddie Smit.
Holland
Nov 12 '05 #1
6 3982
I think you should build the WHERE clause in code to allow an arbitrary
numbers of towns in the query if you want to be fancy. If not, try a
parameter query where you enter a list of towns:

SELECT Surname FROM MyTable WHERE Town IN ([Please enter towns])

Pavel

Eddie Smit wrote:

field- field-
surname town
---- ----

john NY
john Vegas
john Boston
eddie Boston
eddie New Orleans
eddie NY
burt Miami
burt Boston
hank NY

Hi all,
In the example you see the content of a table of persons with their favorit town
(the table consists of two fields, name and town)
How can a make a query that returns the persons who like certain towns?

for example: what persons like Boston and NY?
the answer is John and Eddie, BUT how do I do this in a query?
In real the table is much bigger and do I need to query with different towns
another example: who likes NY, Boston and Vegas, answer: john
(Sorry, maybe a simple question)

Best regards and thanks in advance,

Eddie Smit.
Holland

Nov 12 '05 #2
thanks Pavel,

But in the parameter one can only give ONE town.
How can I check for two or more towns in SQL?
Perhaps I need to write a VB loop?

Thanks, eddie.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #3
ed*******@freeler.nl (Eddie Smit) wrote in
news:9d**************************@posting.google.c om:
field- field-
surname town
---- ----

john NY
john Vegas
john Boston
eddie Boston
eddie New Orleans
eddie NY
burt Miami
burt Boston
hank NY

Hi all,
In the example you see the content of a table of persons with their
favorit town (the table consists of two fields, name and town)
How can a make a query that returns the persons who like certain
towns?

for example: what persons like Boston and NY?
the answer is John and Eddie, BUT how do I do this in a query?
In real the table is much bigger and do I need to query with different
towns another example: who likes NY, Boston and Vegas, answer: john
(Sorry, maybe a simple question)


SELECT surname FROM tblFavTowns WHERE town in ('NY','Boston') GROUP BY surname HAVING
count(town)=2

for the more general case, if the towns are listed in tblTownList:

SELECT surname FROM tblFavTowns WHERE town in (SELECT town FROM tblTownList) GROUP BY
surname HAVING count(town) = (select count(town) from tblTownList)

--
Ross Presser -- rpresser AT imtek DOT com
.... seeking a new quote ...
Nov 12 '05 #4
Is it feasible for you to turn the list of towns into a list box with
MultiSelect enabled and build your query from the items the user selects?

Pavel

eddie smit wrote:

thanks Pavel,

But in the parameter one can only give ONE town.
How can I check for two or more towns in SQL?
Perhaps I need to write a VB loop?

Thanks, eddie.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 12 '05 #5
One way to get at this in SQL is to use nested queries.

For example, to find out who likes Vegas, New York, and Boston, you could
use the following query:

SELECT Name
FROM tblFavCities
WHERE City = "Vegas" AND Name IN
(SELECT Name FROM tblFavCities WHERE City = "NY" and Name IN
(SELECT Name FROM tblFavCities WHERE City = "Boston"))

To break it down, the last query is evaluated first and it returns the
set of people with Boston as a favorite city. This would be John, Eddie
and Burt.

That list of people becomes a condition for the next query up, which then
returns the people who both match the list and have NY as a favorite
city. These are John and Eddie.

So John and Eddie are the ones passed on to the top query which returns
which of them is both on the list and has Vegas in their city list.
Finally, the query returns John.

To add another city to the list you would simply add an IN to the last
WHERE clause and slide in another subquery. Also, you can place
parameters in the slots for cities if you always wanted to search for a
set number of cities. Otherwise you could build this statement in code
fairly easily.

Hope that helps,

Carlos
ed*******@freeler.nl (Eddie Smit) wrote in
news:9d**************************@posting.google.c om:
field- field-
surname town
---- ----

john NY
john Vegas
john Boston
eddie Boston
eddie New Orleans
eddie NY
burt Miami
burt Boston
hank NY

Hi all,
In the example you see the content of a table of persons with their
favorit town (the table consists of two fields, name and town)
How can a make a query that returns the persons who like certain
towns?

for example: what persons like Boston and NY?
the answer is John and Eddie, BUT how do I do this in a query?
In real the table is much bigger and do I need to query with different
towns another example: who likes NY, Boston and Vegas, answer: john
(Sorry, maybe a simple question)

Best regards and thanks in advance,

Eddie Smit.
Holland


Nov 12 '05 #6
using a multi-select listbox definitely gets my vote... even better,
there's code that helps you do most of this stuff right here...

http://www.mvps.org/access/forms/frm0007.htm

Use Multi-Select List boxes as query parameters
Author(s)
Dev Ashish
(Q) I have a MultiSelect listbox control on my form. I want to pass
the selected items to a query as a parameter. How do I do this?

(A) Unlike simple listbox controls which can be referenced as a
parameter by a query, MultiSelect listboxes cannot be used directly as
a parameter. This is because calling the listbox
(Forms!frmMyForm!lbMultiSelListBox) from anywhere will not
automatically concatenate all the selected items. You need to build
the criteria yourself.

Note: You can still use a parameterized query provided you pass the
entire Where clause to it via code as a parameter. (eg. Have the query
reference a hidden control to which you manually assign the complete
WHERE clause using the following logic.)

For example,

'******************** Code Start ************************
Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String
Const cQUOTE As String = "'" '<--My addition (If you had dates,
it'd be #)

Set frm = Form!frmMyForm
Set ctl = frm!lbMultiSelectListbox
strSQL = "Select * from Employees where [EmpID]=" & cQUOTE
'Assuming long [EmpID] is the bound field in lb
'enumerate selected items and
'concatenate to strSQL
For Each varItem In ctl.ItemsSelected
strSQL = strSQL & ctl.ItemData(varItem) & cQUOTE & " OR
[EmpID]="
Next varItem

'Trim the end of strSQL
strSQL=left$(strSQL,len(strSQL)-12))
'******************** Code end ************************

NOTE: In your case, because the town name is a string, you would have
to include a delimiter... You might want to read the original article
in case I've bollixed this up, but I don't think I have...
Nov 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Nicolae Fieraru | last post by:
I have two tables, they contain: Table1: ID1, Name1, Address1, Purchase1 Table2: ID2, Name2, Address2, Purchase2 I need a query which creates Table3 with content from Table1 and Table2. The...
7
by: rednexgfx_k | last post by:
All, Problem Summary: I've running about 30 make table queries via VBA in Access 2000, and my database goes from 14,000k to over 2,000,000k. In addition, the longer the procedure runs, the...
0
by: Hi5 | last post by:
Hi, I am working for a client with loads of expectations from a simple database. Now, they want me to make them an access form, in which it will enable them to query the database using theee...
24
by: Bob Alston | last post by:
Anyone know a way to make all access to a linked table, in another Access MDB, read only? I really don't want all the hassle of implementing full access security. I can't do this at the server...
3
by: Don Sealer | last post by:
I'm guessing this is pretty simple however not simple enough for me. I'm developing a database to track expenses, income, banking transactions, etc. I have a very simple query with four fields,...
11
by: fyshfysh | last post by:
Hi all, A quick question about a function which surely should be possible in a Make-Table Query in Access. I wonder if it's possible to very simply have a field that numbers the rows in the...
3
by: Robertf987 | last post by:
Hi, I'm a bit stuck with an access database. I'm using access 2000 if that's any help. Right, it's 3:40am right now and I'm rather tired, but I *hope* this makes sense. I have a table which...
27
by: MLH | last post by:
How can I turn the following into a make-table query? SELECT & " " & AS Recipient FROM tblVehicleJobs INNER JOIN tblAddnlOwnrs ON tblVehicleJobs.VehicleJobID = tblAddnlOwnrs.VehicleJobID WHERE...
3
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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...
0
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...

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.