Solved

Microsoft Access Query Wildcard dilemma

Posted on 2013-06-15
5
450 Views
Last Modified: 2013-06-17
OK I'm going crazy... I've tried the * and the % to no avail.

Here is the SQL code of the query:

SELECT tempDocList.FName, tempDocList.FPath, [forms]![frmEquipment]![PartNo] AS PN, [forms]![frmEquipment]![SerialNo] AS SN, [forms]![frmEquipment]![ModelNo] AS MN, [forms]![frmEquipment]![EquipmentDesc] AS [Desc], [forms]![frmEquipment]![FirmWare] AS FW
FROM tempDocList
WHERE (((tempDocList.FName) Like "%" & [forms]![frmEquipment]![PartNo] & "%*") AND (([forms]![frmEquipment]![PartNo])<>"" And ([forms]![frmEquipment]![PartNo]) Is Not Null)) OR (((tempDocList.FPath) Like "%" & [forms]![frmEquipment]![PartNo] & "%") AND (([forms]![frmEquipment]![PartNo])<>"" And ([forms]![frmEquipment]![PartNo]) Is Not Null)) OR (((tempDocList.FName) Like "%" & [forms]![frmEquipment]![SerialNo] & "%") AND (([forms]![frmEquipment]![SerialNo])<>"" And ([forms]![frmEquipment]![SerialNo]) Is Not Null)) OR (((tempDocList.FPath) Like "%" & [forms]![frmEquipment]![SerialNo] & "%") AND (([forms]![frmEquipment]![SerialNo])<>"" And ([forms]![frmEquipment]![SerialNo]) Is Not Null)) OR (((tempDocList.FName) Like "%" & [forms]![frmEquipment]![ModelNo] & "%") AND (([forms]![frmEquipment]![ModelNo])<>"" And ([forms]![frmEquipment]![ModelNo]) Is Not Null)) OR (((tempDocList.FPath) Like "%" & [forms]![frmEquipment]![ModelNo] & "%") AND (([forms]![frmEquipment]![ModelNo])<>"" And ([forms]![frmEquipment]![ModelNo]) Is Not Null)) OR (((tempDocList.FName) Like "%" & [forms]![frmEquipment]![FirmWare] & "%") AND (([forms]![frmEquipment]![FirmWare])<>"" And ([forms]![frmEquipment]![FirmWare]) Is Not Null)) OR (((tempDocList.FPath) Like "%" & [forms]![frmEquipment]![FirmWare] & "%") AND (([forms]![frmEquipment]![FirmWare])<>"" And ([forms]![frmEquipment]![FirmWare]) Is Not Null)) OR (((tempDocList.FName) Like "%" & [forms]![frmEquipment]![EquipmentDesc] & "%") AND (([forms]![frmEquipment]![EquipmentDesc])<>"" And ([forms]![frmEquipment]![EquipmentDesc]) Is Not Null)) OR (((tempDocList.FPath) Like "%" & [forms]![frmEquipment]![EquipmentDesc] & "%") AND (([forms]![frmEquipment]![EquipmentDesc])<>"" And ([forms]![frmEquipment]![EquipmentDesc]) Is Not Null));

I'm attaching a spreadsheet with the data for the table "tempDocList" -- if you can create a query with the above string and create a table named as the spreadsheet and tell me what I'm doing wrong??
tempDocList.xlsx
0
Comment
Question by:Ei0914
5 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
Comment Utility
Hi,
what is your data source? Tables in Access or SQL server?
Where is your query executed?
Can you post the code where you build the query string?
Thanks.
Rainer
0
 
LVL 33

Assisted Solution

by:Norie
Norie earned 167 total points
Comment Utility
What is your query meant to return?

In Access % isn't a wildcard, the usual wildcard is *.

So something like this,

tempDocList.FName Like "%" & [forms]![frmEquipment]![PartNo] & "%*")

Should look like this.

tempDocList.FName Like "*" & [forms]![frmEquipment]![PartNo] & "*"
 
Does the query work without the criteria?

If it does try adding the criteria back in a section at a time and test as you go.
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 333 total points
Comment Utility
A couple of issues.

1.  As mentioned above, the wildcard you are seeking in Access is the *, not the %.  If you are using SQL Server as you back-end, and the query is being executed on the SQL Server via a Pass-Thru query, you would use the %.

2.  Since 'FName' and [FPath] are strings, you have to wrap the search strings 'PartNo', 'SerialNo' in quotes.  I've uses single quotes in my example below.  Note that there is a '* before each of the form fields and a *' after each.  Assuming that your [PartNo] field on your form contains "abcd", they syntax would return something like the following:

[FName] LIKE '*abcd*'

3.  There is an easier way to test for an empty string or a NULL value.  There are actually several, you could use:

 [forms]![frmEquipment]![PartNo] & "" <> ""

or you could use

NZ( [forms]![frmEquipment]![PartNo], "") <> ""

Either of these will significantly increase the readability of your query.

So, you might try:
WHERE (tempDocList.FName Like "'*" & [forms]![frmEquipment]![PartNo] & "*'" AND
       NZ([forms]![frmEquipment]![PartNo], "") <> "")
OR (tempDocList.FPath Like "'*" & [forms]![frmEquipment]![PartNo] & "*'" AND 
       NZ([forms]![frmEquipment]![PartNo], "") <> "")
OR (tempDocList.FName Like "'*" & [forms]![frmEquipment]![SerialNo] & "*'" AND
        NZ([forms]![frmEquipment]![SerialNo], "") <> "")
OR (tempDocList.FPath Like "'*" & [forms]![frmEquipment]![SerialNo] & "*'" AND
        NZ([forms]![frmEquipment]![SerialNo], "") <> "")
OR (tempDocList.FName Like "'*" & [forms]![frmEquipment]![ModelNo] & "*'" AND
       NZ([forms]![frmEquipment]![ModelNo], "") <> "") 
OR (tempDocList.FPath Like "'*" & [forms]![frmEquipment]![ModelNo] & "*'" AND
       NZ([forms]![frmEquipment]![ModelNo], "") <> "") 
OR (tempDocList.FName Like "'*" & [forms]![frmEquipment]![FirmWare] & "*'" AND
       NZ([forms]![frmEquipment]![FirmWare], "") <> "") 
OR (tempDocList.FPath Like "'*" & [forms]![frmEquipment]![FirmWare] & "*'" AND
       NZ([forms]![frmEquipment]![FirmWare], "") <> "") 
OR (tempDocList.FName Like "'*" & [forms]![frmEquipment]![EquipmentDesc] & "*'" AND
       NZ([forms]![frmEquipment]![EquipmentDesc], "") <> "") 
OR (tempDocList.FPath Like "'*" & [forms]![frmEquipment]![EquipmentDesc] & "*'"  AND
       NZ([forms]![frmEquipment]![EquipmentDesc], "") <> "");

Open in new window

0
 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 333 total points
Comment Utility
Since you are comparing two text fields to several other values, you could also concatenate those fields together, something like:
WHERE (tempDocList.FName & "!" & tempDocList.FPath Like "'*" & [forms]![frmEquipment]![PartNo] & "*'" AND
       NZ([forms]![frmEquipment]![PartNo], "") <> "")
OR (tempDocList.FName & "!" & tempDocList.FPath Like "'*" & [forms]![frmEquipment]![SerialNo] & "*'" AND
       NZ([forms]![frmEquipment]![SerialNo], "") <> "")
OR (tempDocList.FName & "!" & tempDocList.FPath Like "'*" & [forms]![frmEquipment]![ModelNo] & "*'" AND
       NZ([forms]![frmEquipment]![ModelNo], "") <> "")
OR (tempDocList.FName & "!" & tempDocList.FPath Like "'*" & [forms]![frmEquipment]![FirmWare] & "*'" AND
       NZ([forms]![frmEquipment]![FirmWare], "") <> "")
OR (tempDocList.FName & "!" & tempDocList.FPath Like "'*" & [forms]![frmEquipment]![EquipmentDesc] & "*'" AND
       NZ([forms]![frmEquipment]![EquipmentDesc], "") <> "")

Open in new window

In my example above, I have concatenated the two text field together into a single string, using an exclamation point as a field seperator.  Depending on what your various search fields (PartNo, SerialNo, ModelNo, ...) look like, you might want to make the !! or ZZ or some other combination.
0
 

Author Closing Comment

by:Ei0914
Comment Utility
I broke the query into multiple queries which seems to be less complicated and is returning the records I wanted. Thanks for all your help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now