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

How do I search on 2 fields in MS Access?

I copied the following from a database I have:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Combo41_AfterUpdate()
  2.     ' Find the record that matches the control.
  3.     Me.RecordsetClone.FindFirst "[First Name] = '" & Me![Combo41] & "'"
  4.     Me.Bookmark = Me.RecordsetClone.Bookmark
  5. End Sub
It finds the record. But 2 people with the same first name, it finds only the 1st one. I tried adding the [Last Name] in various ways but I get different error messages. Can you search on first and Last name? If so What is the syntax?
Help
Mar 31 '11 #1
2 1661
TheSmileyCoder
2,322 Expert Mod 2GB
If we assume you have a second combobox for the lastname, and add a button to start the search instead of using the afterupdate, it could look like this:

Expand|Select|Wrap|Line Numbers
  1. Private strCrit as String
  2. Private Sub btn_Search_Click()
  3. 'Error checking
  4. If Isnull(me.cmbFirstName) and isnull me.cmbLastName) then
  5.   Msgbox "No search criteria entered"
  6.   Exit Sub
  7. End If
  8.  
  9. dim strNewCrit as string
  10. If isNull(me.cmbFirstName) then
  11.   'Only search on lastName
  12.   strNewCrit="[LastName]='" & me.cmbLastName & "'"
  13. elseIf isNull(me.cmbLastName) Then
  14.   strNewCrit="[FirstName]='" & Me.cmbFirstName & "'"
  15. else
  16.   strNewCrit="[LastName]='" & me.cmbLastName & "'" & " AND " & "[FirstName]='" & Me.cmbFirstName & "'"
  17. end If
  18.  
  19.  
  20. If strNewCrit=strCrit Then
  21.   'Search allready done once before, move to next record matching
  22.   Me.RecordSet.FindNext strCrit
  23. Else
  24.   strCrit=strNewCrit
  25.   Me.RecordSet.FindFirst strCrit
  26. End If
  27.  
  28.  
  29. If me.RecordSet.NoMatch Then
  30.   Msgbox "No more records matching criteria
  31. end if
  32. End Sub

As a general note: The first time you use a control in code ALWAYS ALWAYS ALWAYS and did I mention ALWAYS give it a meaningful name. Combo41 does not count as a meaningful name.

Hope you can use this.
Mar 31 '11 #2
NeoPa
32,556 Expert Mod 16PB
An alternative approach might be to filter the data (See Cascading Combo/List Boxes) rather than moving directly to a record. This would then make all records available that matched the first name. It doesn't really answer your question I know, but it may be something to think about anyway.
Apr 3 '11 #3

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

Similar topics

0
by: _TeCk_ | last post by:
Do anyone know the name of the control : Search in Access. I want to know when the user leave the search form to refresh a subform. Thank's!
6
by: Robertf987 | last post by:
Hi, I need some help, I would be eternally greatful for any kind person out there to give me any advice. I've created a database for the youth centre. It stores applications for funding. I have...
2
by: chobo | last post by:
I'm not sure where to post this question, so sorry if it's in the wrong place. I have a design related question relating to xml files stored in a mysql database. I was wondering how some of you guys...
1
by: hottoku | last post by:
Hi All, I'm having quite a bit of trouble designing a search tool to work with my database. I have found lots of examples from Microsoft Templates to Allen Browne's sample search form. The...
0
by: Bob Alston | last post by:
I am doing volunteer work with a human services referral agency. The want me to build a database of referral data - other agencies, sources of information, etc. Ideally it would be a structured...
4
by: MissFatma | last post by:
hi i have question i've wrote program using visual stido 2005 C# i have to search a datatable on Access i can connect to it but i have to search using the e-mail address i don't know how? all...
1
by: David | last post by:
Hi, I have a table called 'jobserial' This contains amongst others, 2 fields called 'PSL_F_Serial' & 'PSL_L_Serial' Both of these are 'Text' fields (VarChar) which at present hold serial...
25
by: riaane | last post by:
Me again. Please tell me how I can do the following: I have a report listing the results from a number of fields from a query result. The 1 spesific fileld results must be displayed as a hyperlink on...
7
by: ghjk | last post by:
I have a php search page which is having more than one search felds. This is my query SELECT * FROM vehicles WHERE VehicleType='$type' AND VehicleMake='$make' AND VehicleModel='$model' AND Year=...
1
by: josil | last post by:
Hi I'm a newbie in this and need help. I mange to make a one field search form. But now I want to go further. How can I make a multiple field form? <h2>Search</h2>
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: 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: 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:
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.