Earlier in the day I figured out the following query.
SELECT DISTINCT Visits.KHA_ID, Visits.totalCharges
FROM (Visits INNER JOIN (ICDTable INNER JOIN ICDVisitsJxn ON ICDTable.ICD9ID = ICDVisitsJxn.ICD_IDFK) ON Visits.ID = ICDVisitsJxn.VisitsIDFK)
INNER JOIN (ICDTable AS ICDTable_1
INNER JOIN ICDVisitsJxn AS ICDVisitsJxn_1 ON ICDTable_1.ICD9ID = ICDVisitsJxn_1.ICD_IDFK) ON Visits.ID = ICDVisitsJxn_1.VisitsIDFK
WHERE (((ICDTable.Description)
Like "*[enter term]*") AND ((ICDTable_1.Description) Like "*[enter another term]*"));
This works the way I want it to. I would prefer, however, if instead of having to type the exact text in the corresponding field that the user could search for a string of text. This would be akin to the Like *word here* construct Access uses. I've always hardcoded the substrings. Anyone know how to do this programatically.
If you are running this in Access, just add * :
WHERE (((ICDTable.Description) Like "*" & [enter term] & "*")
AND ((ICDTable_1.Description) Like "*" & [enter another term] & "*"));
You can refer to a control on a form:
WHERE (((ICDTable.Description) Like "*" & Forms!MyForm!FirstTerm & "*")
AND ((ICDTable_1.Description) Like "*" & Forms!MyForm!SecondTerm & "*"));
in addition to Remou's answer, this link may also be useful to you:
http://www.techrepublic.com/article/10-tips-for-using-wildcard-characters-in-microsoft-access-criteria-expressions/6154704
Related
I've written a search query with multiple parameters, and parameters are entered via an unbound form. It works except that for one parameter (CostumeInventory.PeriodName) it will return the PK not the value. All parameters & their values come from join tables, and all are set up the same way--datatypes all match, all the same kind of join--all but this one are showing the values instead of the PK like I want them to. Any ideas? Thank you!
Here is my SQL from the query (apologies, I know it's messy, I am just learning so for the most part I'm letting Access write it):
SELECT CostumeInventory.SetID, CostumeInventory.InventoryID,
CostumeInventory.ItemName, CostumeInventory.Description, CostumeInventory.Color,
CostumeInventory.Size, CostumeInventory.ChestBust, CostumeInventory.Waist,
CostumeInventory.Hip, CostumeInventory.Inseam, CostumeInventory.Photo1,
CostumeInventory.Condition, CostumeInventory.RentalRate,
CostumeInventory.ReplacementValue
FROM CostumeInventory
WHERE (((CostumeInventory.Color) Like "*" & [Forms]![frmItemSearch]![cboColor] & "*")
AND ((CostumeInventory.GarmentType) Like "*" & [Forms]![frmItemSearch]![cboSelectType] & "*")
AND ((CostumeInventory.GarmentSubType) Like "*" & [Forms]![frmItemSearch]![cboSubType] & "*")
AND ((CostumeInventory.PeriodName) Like "*" & [Forms]![frmItemSearch]![cboPeriod] & "*"));
I created a query in MS Access that references text put into a form to create an expression for criteria to look up records in a table, by either road name or district.
Others will eventually use the form so I used the like wild card to allow for the flexibility of entering an incomplete road name in the form.
Unfortunately this has meant that if one leaves the road name text box empty, the like function displays all records in the database, and does not limit them based on the second criteria (name of a district.)
Should I be using a different function or perhaps writing more complex criteria?
(I have tried removing the wild card, putting the Or functions in each field on different lines or the same line and also thought about adding to an existing macro to limit the query results by district name, if the street name text box was left blank.)
I have been Googling for quite some time and can't figure this one out. Thank you for any help!
Essentially:
The form (called MJidea) has two text entry boxes -
Street (PriStReport)
District (District)
The query set up:
You could change the SQL where clause of your query to:
where
(
[Forms]![MJidea]![PriStReport] is not null and
[Primary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*"
) or
(
[Forms]![MJidea]![PriStReport] is not null and
[Secondary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*"
) or
(
[Forms]![MJidea]![District] is not null and
[Magisterial District] like "*" & [Forms]![MJidea]![PriStReport] & "*"
)
Alternatively, consolidating the first two tests:
where
(
[Forms]![MJidea]![PriStReport] is not null and
(
[Primary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*" or
[Secondary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*"
)
)
or
(
[Forms]![MJidea]![District] is not null and
[Magisterial District] like "*" & [Forms]![MJidea]![PriStReport] & "*"
)
Thank you, ahead of time, to everyone, for your time and attention.
I am doing a very simple database for my job, that was too large to fit into an excel spreadsheet, which is what we usually use here. I apologize ahead of time, as I have very limited knowledge of access, but have to figure this out.
I have about 1,150,000 records and need to be able to search by the following criteria: Part Number (txtPK), Step (txtStep), Skipped Percentage (txtPer), and Bottleneck? (chkARD); ARD is in the database as "Y" or "N".
This code worked to show either only Y or only N:
IIf([Forms]![Skips_Form]![chkARD],"Y", "N")
However, when I tried to adapt it to show "*" instead of "N" it returns no records, instead of all records.
IIf([Forms]![Skips_Form]![chkARD],"Y", "*")
My desire is to show only the records with "Y" when the checkbox is checked,
and to show all records when it is not checked.
Presumably you are using these iif statements within the where clause of your query, in conjunction with the = operator - something like:
select *
from YourTable
where ARD = IIf([Forms]![Skips_Form]![chkARD],"Y", "*")
If this is indeed the case, then, when the else argument of the iif statement is returned, the SQL statement becomes:
select *
from YourTable
where ARD = "*"
Hence, this will return records for which the ARD field has a literal value of "*".
Instead, you should either use the like operator, which will allow wildcards such as the asterisk to match any value, e.g.:
select *
from YourTable
where ARD like IIf([Forms]![Skips_Form]![chkARD],"Y", "*")
Or, use some simple boolean logic, such as:
select *
from YourTable
where (not [Forms]![Skips_Form]![chkARD]) or ARD="Y"
Complete Working SQL Code
Thank you Lee Mac
SELECT Data.POS, Data.PK, Data.[Step], Data.[ARD?], Skips.Skips, Skips.Total,
Skips.[Skips %], Data.[OPERATION DESCRIPTION], Data.CHARGE_NUMBER, Data.[MDM_PN],
Data.[PlanVer], Data.[PlanRev], Data.[Task_Desc], Data.[Prod Version]
FROM Data INNER JOIN Skips ON Data.POS = Skips.POS
WHERE
(((Data.POS) Like "*" & [Forms]![Skips_Form]![txtPK] & "*")
AND ((Data.[Step]) Like "*" & [Forms]![Skips_Form]![txtStep] & "*")
AND ((Data.[ARD?]) Like IIf([Forms]![Skips_Form]![chkARD],"Y","*"))
AND ((Skips.[Skips %])>=[Forms]![Skips_Form]![txtPer]))
OR
(((Data.POS) Like "*" & [Forms]![Skips_Form]![txtPK] & "*")
AND ((Data.[Step]) Like "*" & [Forms]![Skips_Form]![txtStep] & "*")
AND ((Data.[ARD?]) Like IIf([Forms]![Skips_Form]![chkARD],"Y","*")))
ORDER BY Skips.[Skips %];
I have a field called "Expertise" which houses data about Doctors/Nurses/Surgeons such that Doctor1 may have "Pediatric surgeon; Neurology" in this field. Basically just a bunch of information about the Doctor will fill this field. I have four search boxes on a form where users can search for specific health care professionals. Someone may want to search for "pediatric", "Surgeon", and "neurology". I have this set up now using SQL and my code looks like this:
AND (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise1] & "*"
AND (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise2] & "*"
AND (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise3] & "*"
AND (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise4] & "*"
Notice how I use Like because the user may want to look up the word "Surg" which will return records that contain surgery AND surgeon. This works great if a record has the words pediatric, surgeon, and neurology in their Expertise field. However I need this query to return Doctor1 who has only surgeon in his Expertise field AND Doctor2 who has neurology in his Expertise field.
If I use OR instead of AND it will return ALL RECORDS in the database. I believe I have to use the Nz() function but I'm not exactly sure how.
You can use Nz to transform a Null text box value to a string which will never match any values stored in dbo_Contact.Expertise. For example, say "BOGUS" will never match. Then Like '*BOGUS*' will also never match.
So you can OR those Nz conditions together. Any non-Null text boxes will select matching rows. Null text boxes will not select any. (And the query result set is the combination of rows selected by each of those conditions.)
SELECT c.*
FROM dbo_Contact AS c
WHERE
c.Expertise Like '*' & Nz(Forms!Data_Qry_Test_Page!Expertise1, 'BOGUS') & '*'
OR c.Expertise Like '*' & Nz(Forms!Data_Qry_Test_Page!Expertise2, 'BOGUS') & '*'
OR c.Expertise Like '*' & Nz(Forms!Data_Qry_Test_Page!Expertise3, 'BOGUS') & '*'
OR c.Expertise Like '*' & Nz(Forms!Data_Qry_Test_Page!Expertise4, 'BOGUS') & '*'
I think your problem is just in the organization of your boolean logic.
The way your query works now, you would only get a result if dbo_Contact.Expertise exists in all 4 fields.
What you want however is if Expertise exists in field1 or field2 or field3 or field4.
The reason why you are getting the whole database currently is because you have a few criteria above this group that you've not included here. Its saying "All this stuff above" OR "check this in expertise"
Instead you need to include it as one group anding it to the others above.
AND ((dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise1] & "*"
OR (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise2] & "*"
OR (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise3] & "*"
OR (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise4] & "*")
Notice how I have put all the constraints in parens and "anded" them together with the previous constraints.
I am trying to filter a query via a textbox on a form. I have this in the criteria of the query:
Like "*" & [Forms]![Form_Name]![Textbox] & "*"
However if the textbox is blank it only shows data in the query that have got text in the field. It doesn't show all data, i.e. the fields that have no data. Is this possible to do?
The only way I have found to do it is to have 2 queries 1 with the filter and 1 without and run code that if the textbox is empty swap the query.
Just append a zero-length string to the field for the compare, then none of the fields will be null, so they will match *.
WHERE MyField & ""
Like "*" & [Forms]![Form_Name]![Textbox] & "*"
I don't have a copy of Access available to me at the moment but a quick google and some tinkering tells me that this is likely to do what you need:
Like "*" & [Forms]![Form_Name]![Textbox] & "*"
Or (Len(Nz([Forms]![Form_Name]![Textbox],"")) = 0 And Len(Nz([Column],"")) = 0)
Sources:
http://bytes.com/topic/access/answers/607087-validation-if-null-empty-string
http://office.microsoft.com/en-001/access-help/table-of-operators-HA010235862.aspx
http://www.techonthenet.com/access/functions/advanced/nz.php