Solved

Microsoft Access Query Wildcard dilemma

Posted on 2013-06-15
5
458 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
ID: 39250890
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
ID: 39250916
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
ID: 39251197
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
ID: 39251202
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
ID: 39254990
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

828 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