Solved

If Statement in MS Access

Posted on 2011-03-02
8
337 Views
Last Modified: 2012-05-11
Using MS Access 97 how do you create a query that would produce a word instead of true or false (I will be using this to populate an Access report.  I have three fields (laborer, carpenter, other).  Laborer and Carpenter are yes/no fields, and other is text.  I'd like to create a report showing a field for TRADE and under that field display Laborer if the laborer field is a yes, or show Carpenter if the carpenter field is a yes, or display what is in the other field if there is something in it.  It should also display any combination (example:  If Carpenter is a yes and Laborer is a yes and there is something in other display
Carpenter
Laborer
Blah Blah Blah.
0
Comment
Question by:nplanek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 3

Expert Comment

by:JAMcDo
ID: 35022730
Create a query based on the following SQL statement.

SELECT TblTrades.ID, TblTrades.Laborer, IIf([Laborer]=True,"Laborer"," ") AS txtLaborer, TblTrades.Carpenter, IIf([Carpenter]=True,"Carpenter"," ") AS txtCarpenter, TblTrades.Other
FROM TblTrades;

My table is called TblTrades with fields ID (Autonumber), Laborer (Yes/No), Carpenter (Yes/No) and Other (Text).

To do this, open a blank Query in SQL view and paste the SELECT . . . statement (including the semi-colon) into the SQL window.

If your table and/or field names differ from mine, change them in the SQL statement before changing views.

For the report only use the txtLaborer, txtCarpenter and Other fields.

J.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 35023617


select IIf([laborer]=-1,"Laborer",iif([carpenter]=-1,"Carpenter",[Other])) as Trade
from  TableX
0
 
LVL 8

Expert Comment

by:Andrew_Webster
ID: 35024009
Cap's nearly got it, but the parentheses look wonky.  Here's my attempt!

SELECT ((Iif([laborer]=True, "Laborer", Null) & vbCrLf) & (Iif([carpenter]=True,"Carpenter", Null) & vbCrLf) & Nz([Other],"") As Trade 
FROM tblMyTableName

Open in new window


Each "Iif" statement will return either the word with a carriage return/linefeed or Null.  There's a trick called "Null concatenation where "string" + Null returns Null, but "string" & Null returns "string".  I've used that to concatenate a newline with each term if it's there.  i.e. When [laborer] is True, then Iif([laborer],"Laborer", Null) will return the string "Laborer".  Concatenate that with an "&" to vbCrLf gives your newline.  But if it's False, then it returns a Null.  Concatenate the Null with the next expression using "&" and you just get the next expression.

It's a handy trick for odd conditional concatenation.
0
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 

Author Comment

by:nplanek
ID: 35037550
This is the entire SQL Statement:
SELECT Information.FirstName, Information.LastName, Information.AppDate, Information.Address, Information.Ward, Information.City, Information.State, Information.Zip, Information.Phone,
((Iif([laborer]=True, "Laborer", Null) & vbCrLf) & (Iif([carpenter]=True,"Carpenter", Null) & vbCrLf) & Nz([Other],"") As Trade,
Information.Community, Information.Local, Information.Comments
FROM Information;

It's telling me my syntax is incorrect.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 35037823
test this



SELECT Information.FirstName, Information.LastName, Information.AppDate, Information.Address, Information.Ward, Information.City, Information.State, Information.Zip, Information.Phone,
IIf([laborer]=-1,"Laborer",Null) & IIf([Carpenter]=-1,Chr(13) & Chr(10) & "Carpenter",Null) & IIf(IsNull([Other]),Null,Chr(13) & Chr(10) & [Other]) AS Trade,
Information.Community, Information.Local, Information.Comments
FROM Information;
0
 
LVL 8

Accepted Solution

by:
Andrew_Webster earned 500 total points
ID: 35037859
Here you go.  It needed two tweaks: 1. a missing bracket before "As Trade" and 2. I was thinking in VBA so used vbCrLf instead of Chr$(10) & Chr$(13) - they both mean "new line".

SELECT 
    Information.FirstName,  
    Information.LastName,  
    Information.AppDate,  
    Information.Address,  
    Information.Ward,  
    Information.City,  
    Information.State,  
    Information.Zip,  
    Information.Phone,  
    ((IIf([laborer]=True,"Laborer",Null) & Chr$(10) & Chr$(13)) & (IIf([carpenter]=True,"Carpenter",Null) & Chr$(10) & Chr$(13)) & Nz([Other],"")) AS Trade,  
    Information.Community,  
    Information.Local,  
    Information.Comments
FROM  
    Information;

Open in new window

0
 

Author Closing Comment

by:nplanek
ID: 35038386
Love you experts!  Thanks!
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 35038502
nplanek,

looks like you ignored the earlier posted comment at http:#a35037823
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

636 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