473,378 Members | 1,415 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,378 software developers and data experts.

How do I create an advanced search form?

Seth Schrock
2,965 Expert 2GB
I'm wanting to create a search form so that I can search for employees using wildcards such as * and then be able to select one of the results and go to that employee's record. It will be searching a whole name field that is in the form of Doe, John A. I want to be able to search for *, John and find everyone whose first name is John or search for Doe, * and find everyone whose last name is Doe.

I'm using Access 2010
Jun 13 '11 #1

✓ answered by NeoPa

I just looked at your SQL again more closely, and I realised it's mostly there using a slightly different approach. See if this helps :

Expand|Select|Wrap|Line Numbers
  1. SELECT [ID]
  2.      , [First Name]
  3.      , [Middle Initial]
  4.      , [Last Name]
  5.      , [Last Name] & 
  6.        (', ' + [First Name]) & 
  7.        (' ' + [Middle Initial]) AS [Full Name]
  8. FROM   [People]
  9. WHERE (([First Name] Like [Forms]![Form2]![SearchFirstTxt] & '*')
  10.   AND  ([Last Name] Like [Forms]![Form2]![SearchLastTxt] & '*'))

20 6154
ADezii
8,834 Expert 8TB
First and foremost, a Whole Name should never be stored in a single Field - this defies the Rules of Database Normalization. The above being strongly stated, you can have a Text Box (txtPartial) into which the User can enter a Partial Whole name either First or Last. A List Box will then contain all possible Matches in the [Whole Name] Field for the Value entered in txtPartial. You can then select an entry in the List Box and because of the Unique [ID] Field; a Form can be opened displaying only that Record. The Logic, in general, is posted below:
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.  
  3. If IsNull(Me![txtPartial]) Then Exit Sub
  4.  
  5. strSQL = "SELECT tblEmployees.ID, tblEmployees.[Whole Name], tblEmployees.City, tblEmployees.State, " & _
  6.          "tblEmployees.Zip FROM tblEmployees WHERE tblEmployees.[Whole Name] Like '*" & Me![txtPartial] & "*'"
  7.  
  8. lstMatches.RowSource = strSQL       
P.S. - Before you do anything else, I would follow, and read, the Insight Posted at the Link below:
http://bytes.com/topic/access/insigh...ble-structures
Jun 13 '11 #2
NeoPa
32,556 Expert Mod 16PB
Hi Seth.

One of the main points about using a modern database is that you never attempt what you are asking for. If you want data within a field to be treated differently from any other data in the same field, then it should be in a separate field. You can always use the data joined together, but separating the data when stored together is an approach that will only lead you into trouble.

All that said, you can filter data very easily within a database. Example Filtering on a Form and Cascaded Form Filtering can help you with the basic concepts and should give you all you need.
Jun 13 '11 #3
Seth Schrock
2,965 Expert 2GB
Well, our main database for work (which is huge and is certainly a modern database) has this feature and my boss wants this feature in the database that I'm creating. The reason that I have a whole name field is that I use it to populate combo boxes. I know that I can combine first and last name fields in the combo box, but then they are way separated from each other to make room for big names. If you know of a way around this, I would use it, but it is the only way that I know of. I do have first and last name fields as well as the whole name field.

I will look into the links that you provided and see what I can do with my individual fields.
Jun 14 '11 #4
NeoPa
32,556 Expert Mod 16PB
The solution is relatively simple Seth. The SQL to populate your ComboBox would go from something like the first example to something like the second :
Expand|Select|Wrap|Line Numbers
  1. SELECT [FullName]
  2. FROM   [Table]
Expand|Select|Wrap|Line Numbers
  1. SELECT [Surname] & (', ' + [GivenName]) AS [FullName]
  2. FROM   [Table]
It would still be a single column (assuming that's what you require of course as a multiple column version is also easily supported).

I recommend you actually read the article ADezii linked to in his post #2. Bosses have a habit of asking for things which are technically not very sensible. It's unfortunate certainly (and you have my sympathy), but it comes with the territory I'm afraid. A good, solid understanding of Normalisation is a must for any database technician hoping to survive any length of time in the discipline. It may even provide you with technical ammunition with which to educate your bosses (It sometimes helps when you can refer them to something written down that supports what you are trying to get across to them).
Jun 14 '11 #5
Seth Schrock
2,965 Expert 2GB
I tried your second SQL example and the combo box is empty. In the AS [FullName] part, is FullName the name of a real field or is this a reference for MS Access? For example, my fields are FirstName, LastName, and WholeName. Would my SQL be AS [WholeName] or would I leave it as [FullName]?

I did read Adezii's article. I do try to make my databases as close to normalized as I can, but there the times like this when I can't get features to work without adding the extra field. Once I figure out how to do the combo boxes I can get rid of the WholeName field.
Jun 14 '11 #6
NeoPa
32,556 Expert Mod 16PB
Sorry Seth. I shouldn't have implied you hadn't. I must have misread your response a little. No worries.

The [FullName] part of the SQL was actually an ALIAS, rather than a field. As such, you can call it pretty much anything you like, but try to ensure it clashes with nothing else for best results.

I'm rushing now, but if you post exactly what you tried when it came up with nothing I'll think about it for you when I can.
Jun 14 '11 #7
Seth Schrock
2,965 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. SELECT [People].[Last Name] & (', ' + [People].[First Name]) AS FullName FROM People;
People is the name of the table in my test database. At first I had put the [] around the FullName, but then Access took them away. I tried taking away the [People] from both the Last Name and First Name fields, but that didnt' fix anything. It was there from before so I just left it for this.

If I sound frustrated, it isn't with you. It is with my lack of knowledge. I really do appreciate your help.
Jun 14 '11 #8
NeoPa
32,556 Expert Mod 16PB
I see nothing wrong with your SQL Seth. When you say there is nothing there do you mean nothing visible? Are there 0 entries in the ComboBox?
Jun 14 '11 #9
Seth Schrock
2,965 Expert 2GB
There should be about 150 people listed, but none are shown. There is space for them and I can scroll down, but there are no names.
Jun 14 '11 #10
NeoPa
32,556 Expert Mod 16PB
Ah, so it clearly recognises the records, it just struggles with getting the data. Interesting.

Can you run the following for me in a query and describe the results for me please :

Expand|Select|Wrap|Line Numbers
  1. SELECT [Last Name] & (', ' + [First Name]) AS FullName
  2.      , [Last Name]
  3.      , [First Name]
  4. FROM   People;
If you can, can you also post the fieldname list from the [People] table.
Jun 15 '11 #11
Seth Schrock
2,965 Expert 2GB
It worked. The first field is FullName, the second is Last Name, and the third is First Name. The FullName field is in the form of Doe, John.

The fields for the People table are:
ID AutoNumber PK
First Name Text
Last Name Text
Middle Initial Text
Whole Name Text

Okay, I'm an idiot. I just fixed the previous version. The Combo box properties had 3 columns with the first set to 0". I just changed it to 1 column and made it 3" and everything appeared. Now I just have to get the filtering portion done so that I can search.
Jun 15 '11 #12
Seth Schrock
2,965 Expert 2GB
I have come up with a solution for my search problem. I have setup two queries that pull the ID, First Name, Middle Initial, and Last Name fields from the People table. The first query has a criteria in the First Name field that is tied to a textbox on my search form. Here is the code:
Expand|Select|Wrap|Line Numbers
  1. SELECT People.ID, People.[First Name], People.[Middle Initial], People.[Last Name]
  2. FROM People
  3. WHERE (((People.[First Name]) Like [Forms]![Form2]![SearchFirstTxt] & "*"));
For the textbox, I have run this query in the After Update Event. The second query does the same thing, except that it is for the Last Name search. This allows me to type "Ta" and get all the Taylors, Talberts, etc.

Is the a valid way to do this? Does this break any rules? I do want to do this correctly and follow all the normalization rules and rules of proper database design.
Jun 15 '11 #13
NeoPa
32,556 Expert Mod 16PB
That's pretty much the way to go Seth. I was working up to a more complicated query that included the full name, as well as the individual elements. This query could be filtered on any of the elements you choose (as all are available as fields in the query).

Ultimately, how you apply your filtering depends on criteria I'm not fully aware of at this time (the scenario you're in), but I would consider applying the filtering separately from the building of the query. That is just to make life as straightforward as possible for you though. Your solution is fundamentally correct.

Forms a good objects for working with (creating and applying) filters. I'm not sure what you're applying it to or for but Example Filtering on a Form should fill in many of the blanks.
Jun 15 '11 #14
NeoPa
32,556 Expert Mod 16PB
I just looked at your SQL again more closely, and I realised it's mostly there using a slightly different approach. See if this helps :

Expand|Select|Wrap|Line Numbers
  1. SELECT [ID]
  2.      , [First Name]
  3.      , [Middle Initial]
  4.      , [Last Name]
  5.      , [Last Name] & 
  6.        (', ' + [First Name]) & 
  7.        (' ' + [Middle Initial]) AS [Full Name]
  8. FROM   [People]
  9. WHERE (([First Name] Like [Forms]![Form2]![SearchFirstTxt] & '*')
  10.   AND  ([Last Name] Like [Forms]![Form2]![SearchLastTxt] & '*'))
Jun 15 '11 #15
Seth Schrock
2,965 Expert 2GB
I think that I see what you are doing. If I understand it correctly, I still have two text boxes on my search form. I believe that I would be able to enter a partial name into either or both text boxes and then run this query from, say a button's On Click event. So I should be able to enter A in the First Name text box and Ta in the Last Name text box and get everyone whose name starts with A and whose last name start with Ta, for example, Andrew Taylor and Amber Talbert. There would also be only one query instead of the two that I had since this one contains the criteria for both the First Name and the Last Name. Am I correct?
Jun 16 '11 #16
NeoPa
32,556 Expert Mod 16PB
Absolutely. Right on the button :-)

Furthermore, this query could form the basis of a report or a form and then show just the records that you're interested in.
Jun 16 '11 #17
Seth Schrock
2,965 Expert 2GB
That is exactly what I had in mind. I plan on making a subform in datasheet view with the ID field as a hyperlink to the corresponding record in the Employee form. This part I already know how to do.

Would I be wasting my time in trying to make it so that there would be only one textbox to search from? I would make the criteria be
Expand|Select|Wrap|Line Numbers
  1. WHERE txtSearch LIKE [Last Name] & (', ' + [First Name] & (' ' + [Middle Initial])
so that if I put in Ta, it would search for the last name or if I put in , A it would search for everyone whose first name starts with an A? I have a test database already setup at work, so unless you think that this is a crazy idea, I will play around with it tomorrow at work.
Jun 16 '11 #18
NeoPa
32,556 Expert Mod 16PB
It wouldn't be wasting time so much, as using a less appropriate mechanism. I wouldn't advise that approach. Databases are designed to work with fields. They are generally clever enough to work with calculated values too, but you lose most of the power of the RDBMS doing it that way.

In simple terms it's a bit like saying for filtering fields the RDBMS filters them at source. You plug in the value and it's handled cleverly within the process. Before the data is even returned to you. When filtering on calculated values it can't do that, so it has to get all the data first and only then can it go through, retrospectively almost, and junk the stuff that doesn't match. Like pretend filtering.
Jun 16 '11 #19
Seth Schrock
2,965 Expert 2GB
That makes sense. Thank-you so much for your help. You have fixed two problems: the combo box and the search.
Jun 16 '11 #20
NeoPa
32,556 Expert Mod 16PB
You're welcome Seth. It's been a pleasure :-)
Jun 16 '11 #21

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

Similar topics

1
by: Wayneyh | last post by:
I am trying to create a search form. I have a form that has a combo box with 8 columns in it. How do i create vb code in my search form to search the columns in the combo box in the other form. I...
2
by: MNNovice | last post by:
I am working on a database on my CD collection using Access 2003. Some of the table structures are given below. Table 1 tblMusicCategory Field 1: MusicCategoryID - Auto Number (PK) Field 2:...
2
by: Bobby Edward | last post by:
I am creating an Advanced Search form. The user can select whether their phrase will search the "Title", "Description" or "All" fields. Obviously the WHERE clause will change, depending on the...
2
by: zandiT | last post by:
Hello everyone i have a mainform called frmMain and it has 2 subforms. the frmMain consists of 2 combo boxes. i want to create a search form whereby the user can choose an option in combo1 and in...
1
by: NLR2008 | last post by:
Hi there, Can anybody help me and provide me with a SIMPLE solution to create a search form in Access 2003. I have created a database for Finance Payments and want to enable the user to...
11
by: woodey2002 | last post by:
This problem is driving me crazy. Hello there, i am trying to create a search form for records in my access database. The search form will contain text boxes and a multi select list box. The user...
6
by: woodey2002 | last post by:
Hi Everyone. Thanks for your time. I am trying to create a search form that will allow users to select criteria from multiple multi select boxes. So far i have managed to achieve a search option...
6
by: mercout | last post by:
Hey, I've been trying to create a search form in access for a while now, searching through books and emails. I have the search form set up with 11 combo box's, 3 text box's, a view button, and a...
8
by: munkee | last post by:
Hi everyone, I am using the following code adapted from Allen Browne: 'Purpose: This module illustrates how to create a search form, _ where the user can enter as many or few...
11
by: MadSma | last post by:
hi iam quite new to access and i am now at a stage where i have created my database tables and various forms to input data on them but i need a way of displaying data based on some search criteria ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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?
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...

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.