Question : Access Like "*" in IIf Statement of Criteria Returns No Records

Hi Experts,

I am developing an Access db with two tables for Regions and Provinces. The tables are called tbl_Regions and tbl_Province respectively. They are joined one-to-many where each region has many provinces. I have a Main form with two combo boxes cmb_Region and cmb_Province. I want to use each box in the criteria of a query. The bound value for each is an integer (AutoNumber) value. When a user selects a region, the query should return the records for that region and all provinces. If the user also selects a province, only that provinces records should be returned.

I have the following criteria for the Region ID (rID) and it works fine:

[Forms]![frm_Main]![cmb_Region]

I have the following in the criteria for the Province ID (pID) and it does not return any records:

IIf([Forms]![frm_Main]![cmb_Province]="", Like "*",[Forms]![frm_Main]![cmb_Province])

If I just put Like “*” in the criteria I get all Provinces. If I enter a literal (like the number 4) instead of Like “*” for the true side of the IIf, I get the Province 4 records. The IIf statement seems to working correctly. It is just when I try to use Like”*” within it that I have a problem.
Attachments:
 
Screenshot of access query builder
 

Answer : Access Like "*" in IIf Statement of Criteria Returns No Records

You have a syntax error,
Also, to use  IIF correctly you should specify what to do when the result is true and when the result is False,

Try:

IIf([Forms]![frm_Main]![cmb_Province]="", Like "*" & [Forms]![frm_Main]![cmb_Province] ,"cmb_Province has a specific value")

I am sure you can adapt this to work in your database.

If not, then can you take a step back and explain to us what your ultimate goal is here, ...there may be a more efficient way to do this...

For eaxmple: If your goal is to show all records if no value is selected, the this is typically done simply like this in the query criteria for "ProvinceID:
    Like "*" & [Forms]![frm_Main]![cmb_Province] & "*"

(No real need for IIF...)



;-)

JeffCoachman
Random Solutions  
 
programming4us programming4us