By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,483 Members | 1,447 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,483 IT Pros & Developers. It's quick & easy.

Help with query need

P: n/a
Any suggestions for the following would be gratefully accepted.

I have a table which includes name and interest fields e.g.,

contact interest
smith carpentry
jones gardening
smith plumbing
smith geology
jones geology

I want users to be able to produce a report which lists the names of those
who meet certain criteria (i.e., the user selects up to 3 interests using
combo boxes and then produces a report which lists the names of those who
have those interests). For example, choosing geology and carpentry should
return only the name 'smith'.

Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
news.uow.edu.au wrote:
Any suggestions for the following would be gratefully accepted.

I have a table which includes name and interest fields e.g.,

contact interest
smith carpentry
jones gardening
smith plumbing
smith geology
jones geology

I want users to be able to produce a report which lists the names of those
who meet certain criteria (i.e., the user selects up to 3 interests using
combo boxes and then produces a report which lists the names of those who
have those interests). For example, choosing geology and carpentry should
return only the name 'smith'.

You would be much better off to have the names listed once in a single
table the interests in a second table listed once and a third table
which has a pointer number from the first and second.

For example:

TBL_CONTACTS

CON_PK CON_NAME_2 CON_NAME_1 CON_ADDRESS etc, etc
7568 Smith John
123 Jones Brigitte
34569 Keen I.M.
etc

TBL_INTERESTS

INT_PK INT_DESCRIP INT_DETAILS etc, etc
671234 Carpentry Hitting things with hammers...
1235 Gardening Swearing at and hitting weeds with hammers...
1112 Plumbing Hitting pipes with hammers...
98356 Geology Hitting rocks with hammers....

TBL_INTEREST_LOOKUP (configured as for your example)

LU_CON_FK LU_INT_FK
7568 671234
123 1235
7568 1112
7568 98356
123 98356

For the PK numbers, you never let people see or bother with the numbers
- use an autonumber datatype (that's why I stuffed in random looking
numbers).

This is a harder way to do things for a newbie, especially with respect
to data entry - you'll need to make up 3 forms: one for listing and
adding/editing contacts (form associated with TBL_CONTACTS), another for
listing interests (form associated with TBL_INTERESTS) and a third,
probably using combo boxes, to list contacts and interests.

Hard, yes, but you'll thank yourself in the future for doing things the
database way.

The query to get the information you'd want would be (air code):

select
CON_NAME_1 & ", " & CON_NAME_2 as Contact,
INT_DESCRIP as Interest
from
(TBL_INTERESTS INNER JOIN TBL_INTEREST_LOOKUP ON TBL_INTEREST.INT_PK =
TBL_INTEREST_LOOKUP.LU_INT_FK) INNER JOIN TBL_CONTACTS ON
TBL_INTEREST_LOOKUP.LU_CON_FK = TBL_CONTACTS.CON_PK
where
INT_DESCRIP like Forms!MyForm.cboInterest1 & "*" or
INT_DESCRIP like Forms!MyForm.cboInterest2 & "*" or
INT_DESCRIP like Forms!MyForm.cboInterest3 & "*"

Open a query, close the show table dialog and click on the SQL view of
the table and paste the above into the query.

The key part here (which can be used also on your single table approach
in your original post) is the where clause, above. By using the Like
operator, you allow a combo box which has nothing in it to not mess up
the works.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Nov 13 '05 #2

P: n/a

"Tim Marshall" <TI****@PurplePandaChasers.Moertherium> wrote in message
news:d5**********@coranto.ucs.mun.ca...
news.uow.edu.au wrote:
Any suggestions for the following would be gratefully accepted.

I have a table which includes name and interest fields e.g.,

contact interest
smith carpentry
jones gardening
smith plumbing
smith geology
jones geology

I want users to be able to produce a report which lists the names of
those who meet certain criteria (i.e., the user selects up to 3 interests
using combo boxes and then produces a report which lists the names of
those who have those interests). For example, choosing geology and
carpentry should return only the name 'smith'.

You would be much better off to have the names listed once in a single
table the interests in a second table listed once and a third table which
has a pointer number from the first and second.

For example:

TBL_CONTACTS

CON_PK CON_NAME_2 CON_NAME_1 CON_ADDRESS etc, etc
7568 Smith John
123 Jones Brigitte
34569 Keen I.M.
etc

TBL_INTERESTS

INT_PK INT_DESCRIP INT_DETAILS etc, etc
671234 Carpentry Hitting things with hammers...
1235 Gardening Swearing at and hitting weeds with hammers...
1112 Plumbing Hitting pipes with hammers...
98356 Geology Hitting rocks with hammers....

TBL_INTEREST_LOOKUP (configured as for your example)

LU_CON_FK LU_INT_FK
7568 671234
123 1235
7568 1112
7568 98356
123 98356

For the PK numbers, you never let people see or bother with the numbers -
use an autonumber datatype (that's why I stuffed in random looking
numbers).

This is a harder way to do things for a newbie, especially with respect to
data entry - you'll need to make up 3 forms: one for listing and
adding/editing contacts (form associated with TBL_CONTACTS), another for
listing interests (form associated with TBL_INTERESTS) and a third,
probably using combo boxes, to list contacts and interests.

Hard, yes, but you'll thank yourself in the future for doing things the
database way.

The query to get the information you'd want would be (air code):

select
CON_NAME_1 & ", " & CON_NAME_2 as Contact,
INT_DESCRIP as Interest
from
(TBL_INTERESTS INNER JOIN TBL_INTEREST_LOOKUP ON TBL_INTEREST.INT_PK =
TBL_INTEREST_LOOKUP.LU_INT_FK) INNER JOIN TBL_CONTACTS ON
TBL_INTEREST_LOOKUP.LU_CON_FK = TBL_CONTACTS.CON_PK
where
INT_DESCRIP like Forms!MyForm.cboInterest1 & "*" or
INT_DESCRIP like Forms!MyForm.cboInterest2 & "*" or
INT_DESCRIP like Forms!MyForm.cboInterest3 & "*"

Open a query, close the show table dialog and click on the SQL view of the
table and paste the above into the query.

The key part here (which can be used also on your single table approach in
your original post) is the where clause, above. By using the Like
operator, you allow a combo box which has nothing in it to not mess up the
works.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me


Thanks Tim,

much appreciate the response, it gets me half of what I'd like, but i'm
still stuck on the rest e.g., if i search for people who have 3 interests,
I get them, but I also get the names of those who have only 1 or 2 of the
three. I'd like to be able to return ONLY the names of those who have ALL
the interests (i.e., to exclude those who have only 1 or 2 of the interests.

PS: I thought i might be able to use the COUNT option to limit the result to
those who have all 3 interests only, but it all seems a bit beyond my skills
:-(
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.