MS Access Query nested IIF statement & derived field

In a report, I have a query that supports a flag that should populate a derived field [Specs] when the [Box] includes specific characters.

My nested IIf statement is giving me some trouble...

1. If the first 2 char of [Box] = "TW"
2. If the first 2 char of [Box] = "CR"
3. If the first 2 char of [Box] = "DW"
4. If [Box] = "0WT"
5. If [Box] = "WWT"

Then the derived field should be populated with "TW", otherwise it should be Null.

Here is what I thought the query should read:

Specs: IIf((Left([Box],2)=“TW”) Or (Left([Box],2)=“CR”) Or (Left([Box],2)=“DW”) Or ([Box]=“0WT”) Or ([Box]=“WWT”),”TW”,Null)

When I put it into the query, Access changes it to this:

Specs: IIf((Left([Box],2)=[“TW”]) Or (Left([Box],2)=[“CR”]) Or (Left([Box],2)=[“DW”]) Or ([Box]=[“0WT”]) Or ([Box]=[“WWT”]),[”TW”],Null)

I am sure that the issue is with a poorly written IIf statement.  Any and all help would be appreciated!

Thank You!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Do you have to do this in the query?  If this is a stand-alone application it's MUCH easier to do in VBA and pass parameters as a function from the query.
CPKGDevTeamAuthor Commented:
The front end of the application is Access and the backend is SQL.  Since the report is built on several different tables, it would seem difficult to write this as code.  Or perhaps I am not understanding the method that you are recommending.  This master report is a critical part of our business process and as such, it contains about 70 fields from 8 different tables. Visually, it is easier for me to keep in a design view.

But I am open to whatever I would need to do to accomplish this.

The bigger problem seems I would not know where to start in your recommendation.

Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
The query designer will often re-format things and sometimes do it incorrectly.  Try it like this:

 IIf(Left$([Box],2)=“TW” Or Left$([Box],2)=“CR” Or Left$([Box],2)=“DW” Or [Box]=“0WT” Or [Box]=“WWT”,”TW”,Null)

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

CPKGDevTeamAuthor Commented:
Thanks JimD.

I did as you typed, and unfortunately, access is still putting [ ] around the quotes "".  Therefore access asks me for values for all [ ] when I run the query.

"TW" becomes ["TW"]
"CR" becomes ["CR"]
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:

 That's odd.  Try it like this:

myField:IIf(Left$([Box],2)='TW' Or Left$([Box],2)='CR' Or Left$([Box],2)='DW' Or [Box]='0WT' Or [Box]='WWT','TW',Null)


The quantity of tables is immaterial ...

If you open the VBA window, Create a standard module (call it anything you want).

This is unnecessarily complicated if you are trying to put it into the query. IIFs are painful and usually unnecessary if the query is not a passthrough query (this does not appear to be).

Open the VBA Editor.
Create a Standard Module (Call it whatever you want)

In the module enter the following code:
Public Function f_MyResult(ByVal strInput As String) As String
    '   1. If the first 2 char of [Box] = "TW"
    '   2. If the first 2 char of [Box] = "CR"
    '   3. If the first 2 char of [Box] = "DW"
    '   4. If [Box] = "0WT"
    '   5. If [Box] = "WWT"
    Select Case Left$(strInput, 2)
        Case "TW", "CR", "DW"
            f_MyResult = "TW"
        Case Else
            Select Case strInput
                Case "OWT", "WWT"
                    f_MyResult = "TW"
                Case Else
                    f_MyResult = vbNullString
            End Select
    End Select
End Function

Open in new window

In Your Query, Call this function as you would anything else: MyField: f_MyResult([BOX])

This will work well and is relatively clear as to (precisely) what it is doing.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CPKGDevTeamAuthor Commented:

This was one of the best answers that I have received on EE.  Thank you for the notes and the code.

It was easy to understand and it works perfectly.  

I appreciate the time and the efforts.

You are entirely welcome! and thank you for the kind response ...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.