473,387 Members | 3,684 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,387 software developers and data experts.

Search using 1 textbox through multiple fields

convexcube
Hi everyone,

I am developing a database which amongst other things records contacts and related information. This database is structured so that a person can be an 'Employee', 'User', 'Customer' etc or any combination of those. So I have a Persons table, a Users table, an Employees table and a Customers table all related by Person_ID. Persons can also have multiple tags (such as Electrician, Plumber etc), so this is in a separate table also related by Person_ID.

What I would like is to be able to search in a single textbox, the name (or part of name of a person), or the tag of a person and have the results shown in a subform below (actually multiple subforms - in order to separate it out by category - Customer, User, Employee etc)

I have been able to do this but have run into a problem, when searching by name, I will get multiple results in any one category if they have more than one tag. I think I need to restructure the query significantly. I realise that this might not be specific enough for a definitive answer but I'm just after a pointer in the right direction, if necessary I can provide more details.

Any help is much appreciated,

Thanks, Ken.
Oct 27 '08 #1
4 3303
mshmyob
904 Expert 512MB
Hello Ken,

Not sure if you have your table structure proper. Sounds to me like you have data redundancy. Could you show your exact table structures with fields and primary and foreign keys.

cheers,

Hi everyone,

I am developing a database which amongst other things records contacts and related information. This database is structured so that a person can be an 'Employee', 'User', 'Customer' etc or any combination of those. So I have a Persons table, a Users table, an Employees table and a Customers table all related by Person_ID. Persons can also have multiple tags (such as Electrician, Plumber etc), so this is in a separate table also related by Person_ID.

What I would like is to be able to search in a single textbox, the name (or part of name of a person), or the tag of a person and have the results shown in a subform below (actually multiple subforms - in order to separate it out by category - Customer, User, Employee etc)

I have been able to do this but have run into a problem, when searching by name, I will get multiple results in any one category if they have more than one tag. I think I need to restructure the query significantly. I realise that this might not be specific enough for a definitive answer but I'm just after a pointer in the right direction, if necessary I can provide more details.

Any help is much appreciated,

Thanks, Ken.
Oct 28 '08 #2
Hello Ken,

Not sure if you have your table structure proper. Sounds to me like you have data redundancy. Could you show your exact table structures with fields and primary and foreign keys.

cheers,
Thanks for your response mshmyob,

My Table structure is as follows

Persons
Person_ID (PK)
Person_NameFirst
Person_NameLast

Users
User_ID (PK)
User_Password
User_Level
Person_ID (FK)

Customers
Customer_ID (PK)
Person_ID (FK)

Employees
Employee_ID (PK)
Employee_DOB
Person_ID (FK)

Tags
Tag_ID (PK)
Tag_Detail
Person_ID (FK)

Form_Central Query
SELECT Persons.Person_NameFirst, Persons.Person_NameLast, Tags.Tags_Detail
FROM Persons INNER JOIN Tags ON Persons.Person_ID = Tags.Person_ID;

Form_Central Filter
Person_NameFirst Like Forms!Central.txtSearch & "*" Or Person_NameLast Like Forms!Central.txtSearch & "*" Or Tags_Detail Like Forms!Central.txtSearch & "*"

There are other tables that are related to these but I didn't include them because they don't affect this query (which is why the 'Customers' table may look pointless, and in fact only the Persons & Tags table are relevant to the query). As far as I can see my data has been normalised and there is no redundancy. I do understand why I am getting multiple results when searching by a persons name if they have more than 1 tag (because multiple records match the criteria), but what I would like to know is how I can display only 1 of those matching records (since I only see the name, I only need to see it once).

Please let me know if you need more info and thanks for your time, it is much appreciated.

Regards,
Ken.
Oct 29 '08 #3
mshmyob
904 Expert 512MB
Try the following

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT tblPerson.PersonFName, tblPerson.PersonLName
  2. FROM tblPerson INNER JOIN tblTag ON tblPerson.PersonID = tblTag.PersonID
  3. WHERE tblPerson.PersonID= tblTag.PersonID;
  4.  
Again looking at it I still think you have the design a little messed up. For instance Your two tables PERSONS and TAGS. Sounds to me like this should be a many to many. For instance

Each PERSON can have many TAGS
and
Each TAG can be had by many PERSONS

Therefore a Briodge table is needed between them.

The way you have it you are creating a new record in the TAGS table for every person that is an Electrician, Plumber, etc.. You are therefore duplicating TAGS. If this is correct then putting the bridge table in will solve redundancy problems and make your queries work without problems.

I haven't looked at the other tables.

cheers,


Thanks for your response mshmyob,

My Table structure is as follows

Persons
Person_ID (PK)
Person_NameFirst
Person_NameLast

Users
User_ID (PK)
User_Password
User_Level
Person_ID (FK)

Customers
Customer_ID (PK)
Person_ID (FK)

Employees
Employee_ID (PK)
Employee_DOB
Person_ID (FK)

Tags
Tag_ID (PK)
Tag_Detail
Person_ID (FK)

Form_Central Query
SELECT Persons.Person_NameFirst, Persons.Person_NameLast, Tags.Tags_Detail
FROM Persons INNER JOIN Tags ON Persons.Person_ID = Tags.Person_ID;

Form_Central Filter
Person_NameFirst Like Forms!Central.txtSearch & "*" Or Person_NameLast Like Forms!Central.txtSearch & "*" Or Tags_Detail Like Forms!Central.txtSearch & "*"

There are other tables that are related to these but I didn't include them because they don't affect this query (which is why the 'Customers' table may look pointless, and in fact only the Persons & Tags table are relevant to the query). As far as I can see my data has been normalised and there is no redundancy. I do understand why I am getting multiple results when searching by a persons name if they have more than 1 tag (because multiple records match the criteria), but what I would like to know is how I can display only 1 of those matching records (since I only see the name, I only need to see it once).

Please let me know if you need more info and thanks for your time, it is much appreciated.

Regards,
Ken.
Oct 29 '08 #4
Try the following

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT tblPerson.PersonFName, tblPerson.PersonLName
  2. FROM tblPerson INNER JOIN tblTag ON tblPerson.PersonID = tblTag.PersonID
  3. WHERE tblPerson.PersonID= tblTag.PersonID;
  4.  
Again looking at it I still think you have the design a little messed up. For instance Your two tables PERSONS and TAGS. Sounds to me like this should be a many to many. For instance

Each PERSON can have many TAGS
and
Each TAG can be had by many PERSONS

Therefore a Briodge table is needed between them.

The way you have it you are creating a new record in the TAGS table for every person that is an Electrician, Plumber, etc.. You are therefore duplicating TAGS. If this is correct then putting the bridge table in will solve redundancy problems and make your queries work without problems.

I haven't looked at the other tables.

cheers,
Thanks for that, I can see how a many to many relationship can benefit my situation and I will try that. I'll post on this thread again if I still have problems.

Thanks very much for your help.

Regards,
Ken.
Oct 29 '08 #5

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

Similar topics

9
by: Christopher Koh | last post by:
I will make a form which will search the database (just like google interface) that will look/match for the exact name in the records of a given fieldname. Any suggestions on how to make the code?
8
by: Steph | last post by:
Hi. I'm very new to MS Access and have been presented with an Access database of contacts by my employer. I am trying to redesign the main form of the database so that a button entitled...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
5
by: JP SIngh | last post by:
Hi All This is a complicated one, not for the faint hearted :) :) :) Please help if you can how to achieve this search. We have a freetext search entry box to allow users to search the...
2
by: Homey! | last post by:
Hello all I am new to Access. I have imported data from an old FoxPro 2.x database. This is probably the most basic function but I cant get a search box to work. I need to search for company name...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
3
by: Redbeard | last post by:
Hi All this is my first time post, be gentle. I am looking at creating a keyword search that searches multiple fields in a Form and then filters records that match the keyword. The Form...
10
by: Eugenio | last post by:
Hi there, I'm returning to this forum for the second time and I would like to say thanks for the great help provided. I've encountered a new problem now and hope that you will be able to help me...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.