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

Query that will search for mulitiple criteria in mulitiple combinations

I've got a query that returns 8 fields, 7 of which I need to search for in various combinations and the other 1 (Cost) just to view its contents. Below is the mess I've made of it so far:

SELECT tblNCRRpt.dtmNCRDate, tblNCRRpt.idsNCRNum, tblNCRRpt.chrVendr, tblNCRRpt.chrPartNam, tblNCRRpt.chrPartNum, tblNCRRpt.chrSerNum, tblNCRRpt.chrDisp, tblNCRRpt.curCost
FROM tblNCRRpt
WHERE (((tblNCRRpt.idsNCRNum) Like [Forms]![frmSrchNCR]![txtNCRNum])) OR (((tblNCRRpt.chrVendr) Like [Forms]![frmSrchNCR]![txtVendr])) OR (((tblNCRRpt.chrPartNum) Like [Forms]![frmSrchNCR]![txtPartNum]) AND ((tblNCRRpt.chrSerNum) Like [Forms]![frmSrchNCR]![txtSerNum])) OR (((tblNCRRpt.chrDisp) Like [Forms]![frmSrchNCR]![cboDisp])) OR (((tblNCRRpt.dtmNCRDate) Between [Forms]![frmSrchNCR]![txtDate1] And [Forms]![frmSrchNCR]![txtDate2])) OR (((tblNCRRpt.dtmNCRDate) Between [Forms]![frmSrchNCR]![txtDate1] And [Forms]![frmSrchNCR]![txtDate2]) AND ((tblNCRRpt.chrPartNum) Like [Forms]![frmSrchNCR]![txtPartNum])) OR (((tblNCRRpt.dtmNCRDate) Between [Forms]![frmSrchNCR]![txtDate1] And [Forms]![frmSrchNCR]![txtDate2]) AND ((tblNCRRpt.chrDisp) Like [Forms]![frmSrchNCR]![cboDisp])) OR (((tblNCRRpt.chrPartNum) Like [Forms]![frmSrchNCR]![txtPartNum]))
ORDER BY tblNCRRpt.dtmNCRDate, tblNCRRpt.idsNCRNum;

Obviously, this only works for some of the comibnations, which doesn't help me. I need ALL search combinations to work. Any ideas?
Jan 18 '07 #1
5 1685
cyberdwarf
218 Expert 100+
WOW! You mean that's not all the combinations???

If not all combinations are used in particular cases, you might simplify things by constructing your query on the fly, depending on which form fields have valid values at the time...

Expand|Select|Wrap|Line Numbers
  1. dim strSQL as String
  2. strSQL = "Select * From tblName Where "
  3. If Not IsNull(Form1!FldA) Then
  4.     strSQL = strSQL & "tblName.FldX = " & Form1!FldA
  5. End If
and so on

HTH

Steve
Jan 18 '07 #2
Steve, I am not good enough at this yet to be able to apply your answer. Maybe if I could give a little more info (Note: initial question should be corrected to 6 field to search for and 2 just for viewing)

The user will enter values in the following controls (located all on the same form) in different combinations (depending on what they are searching for) (and some of them being left null): (1) a date range (txtDate1) & (txtDate2), (2) an NCR# (idsNCRNum) (which is also the primary key of the main table all these search criteria are located in), (3) a serial# (txtSerNum), (4) a part# (txtPartNum), (5) a disposition type (txtDisp), (6) a vendor (txtVendr).

Example searches: (1) for all records during a given date range, (2) for a serial# (3) for a part# (4) for a serial# AND part# (where that part# and ser# occur together in the same record), (5) for a NCR#, (6) for disposition (7) for a Vendor, etc.....NOTING that all of these may or may not be searched for with a date range included and also in different combinations with each other.

Maybe your reply answered all of this, but I'm not advanced enough to know, sorry. I appreciate all help on this!
Jan 18 '07 #3
nico5038
3,080 Expert 2GB
Te easy way will be to have a mainform with a datasheet subform based on your query.
Now instruct the user to use the right-click popup menu. This will issue a filter to the rows and by adding new criteria the filter will "narrow" it down by using an AND relation. Even a Like can be specified this way.

The other option would be to have fields (or combo's) for your 7 fields and let the user enter the needed strings. The with code you need to test for filled fields and create a filter for the subform yourself. Guess you know now why I prefer the right-click :-)

Nic;o)
Jan 20 '07 #4
Te easy way will be to have a mainform with a datasheet subform based on your query.
Now instruct the user to use the right-click popup menu. This will issue a filter to the rows and by adding new criteria the filter will "narrow" it down by using an AND relation. Even a Like can be specified this way.

The other option would be to have fields (or combo's) for your 7 fields and let the user enter the needed strings. The with code you need to test for filled fields and create a filter for the subform yourself. Guess you know now why I prefer the right-click :-)

Nic;o)
Based on the users I will have, I will need something that is as user-friendly as possible, thus, having them fill in the fields will be my unfortunate answer. I already have a form set up where they will enter the fields they so choose. Right below these entry boxes is a tabular query subform that I want to show them the results of their "search".

Your idea to test for filled fields and then filter the subform myself makes perfect sense and is what I've been I've been stuck on for the last 3 days! I don't know how to write code well enough yet. Could you give me some example of how to start and then maybe I can figure enough of it out to fill in the rest of the blanks...? Thanks!
Jan 21 '07 #5
nico5038
3,080 Expert 2GB
OK, then place a [Filter] button behind the fields.
Next open the properties for the button (button with hand and white sheet) and under the event tab you'll find the OnClick event. Doubleclick on that so [Event Procedure] will appear.
Now press the [...] button at the end and the code form will open with the cursor in the procedure needed.
Now type:
Expand|Select|Wrap|Line Numbers
  1. dim strFilter as string
  2.  
  3. ' empty string
  4. strFilter = ""
  5. ' test text field filled
  6. IF len(nz(me.txtFieldText)) > 0 then
  7.     strFilter = strFilter & " and FieldText='" & me.txtFieldText & "'"
  8. endif
  9. ' test numeric field filled
  10. IF len(nz(me.txtFieldNum)) > 0 then
  11.     strFilter = strFilter & " and FieldNum=" & me.txtFieldNum 
  12. endif
  13. ' test date field filled
  14. IF len(nz(me.txtFieldDate)) > 0 then
  15.     strFilter = strFilter & " and FieldDate=#" & me.txtFieldDate & "#" 
  16. endif
  17. ' test or filter has been filled:
  18. IF len(strFilter) > 0 then
  19.     ' filter found, remove first " and " string
  20.     me.subformname.form.filter = mid(strFilter,5)
  21.     me.subformname.form.filteron = True
  22. else
  23.     ' deactivate filter
  24.     me.subformname.form.filteron = False
  25. endif
  26.  
Clear ?

Nic;o)
Jan 21 '07 #6

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

Similar topics

3
by: MVM | last post by:
Hello everyone, I need some help building a query using three tables and I am having difficulty writing the query. Here are the tables: GENERAL: GID - primary key PARCEL EDITDATE MAPCODE
6
by: Andy | last post by:
Hello, I am having many problems with setting up a parameter query that searches by the criteria entered or returns all records if nothing is entered. I have designed an unbound form with 3...
3
by: splashout1 | last post by:
I am trying to set up a search facility by giving the user a form for them to enter a variety of criteria. They will either know the exact criteria they are looking for or will not. Therefore i...
3
by: dskillingstad | last post by:
I'd appreciate any help I can get. I'm not sure what I'm doing wrong, but.... I've searched these groups for some solutions but no luck. I have an unbound form (frmSearch), with several unbound...
2
by: angie | last post by:
I need to figure out how to create a user interface to search a query, but here's the bad part...I need to account for criteria on at least 7 of the fields. Here's what I'm thinking I need to do:...
5
by: SeanCly10 | last post by:
Hi all. I don't want to sound like a complete idiot here, but I'm somewhat limited in my coding knowledge, and I need some advice and help. I'm working on a database that will eventually be used...
10
by: sesling | last post by:
I have created a query that will pull information from our database for the operators. This query will pull in on average 50,000 records. The operators need to refine the search results. I have...
15
by: Neekos | last post by:
I have a form set up as a search page. The user needs to be able to search by 3 criteria: Group Name, Group Number, or CruiseLine AND SailDate. My problem is with CruiseLine AND SailDate. It works if...
6
by: jmarcrum | last post by:
Hi! I have created a Union Query in ACCESS 2003, that combines 130 records from one query (Extra Foreman Radios) and 250 records from another query (Forman Main Radios). I have created a...
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: 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:
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?
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,...

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.