Need some query help.....

J

James

I'm very new to Access. I was wondering how I could display a field in a
query that is conditional. I will do my best to explain...

I want to add a field to my query that grabs the info from Project.Badge or
Project.BadgeET depending on if the field TestType is displaying "Eng" or
"Pkg"

so it would be something like this

If TestType = "Eng" then
Display Project.BadgeET
ElseIf TestType = "Pkg" then
Display Project.Badge
End if

im assuming I would type some formula in the Criteria section?? but under
field, i can only select Project.Badge OR Project.BadgeET, not both.

Thanks in advance for the help.
 
J

John Spencer

You would not use the criteria field to do this. You need a calculated field
using the IIF operator. Assuming that you want to show NULL if TestType in
not Eng or Pkg the expression in a field "cell" would look like the following
(all on one line).

Field: YourDesiredColumnName: IIF([TestType]="Eng",[Project].[BadgeET],
IIF([TestType]="Pkg",[Project].[Badge],NULL))

That is using two IIF statements - with one nested inside the other.

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
D

Dorian

In SQL mode, enter in your query:
IIF(TestType='Eng',Project.BadgeET,IIF(TestType='Pkg',Project.Badge,NULL))
As ProjBadge

This also outputs NULL if neither condition applies.
You should look up IIF in Access Help.
-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and they
eat for a lifetime".
 
J

James

Great, that worked beautifully! I ended up using IIF([Test
Stats].TestType="Eng",[Project].[BadgeET],[Project].[Badge]). Any other
condition would default to display [Project].Badge (instead of null by using
the nested IIF)

To add to this, now that I have either Badge or BadgeET, how can I use that
to display other information. ie.....

If TestType = "Eng" then
Display TES.LastName & ", " TES.FirstName Where TES.Bdg =
Project.Badge
ElseIf TestType = "Pkg" then
Display TES.LastName & ", " TES.FirstName Where TES.Bdg =
Project.Badge
End if

TES is the name of another table with fields named Bdg, FirstName, and
LastName so i should be able to use the "badge" to locate the name....

Thanks again!
 
D

Dorian

If you want to pull data from more than one table, you need to JOIN the two
tables based on a common field. E.g.
SELECT Project.*, TES.* FROM Project INNER JOIN TES ON Project.Badge =
TES.Bdg
-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and they
eat for a lifetime".
 
J

John Spencer

If the linkage to TES table is based on Badge in some cases and on BadgeET in
other cases, then the solution would involve nested queries.

OR if you only need the one set of data you could use another expression
(Dlookup) or a subquery.

Field: DLookUp("LastName & "", "" & FirstName","TES",
"Bdg=""" & IIF([Test Stats].TestType="Eng",
[Project].[BadgeET],[Project].[Badge]) & """")

OR using correlated subquery

Field: TheName: (SELECT First(LastName & ", " & FirstName) FROM Tes WHERE Bdg
= IIF([Test Stats].TestType="Eng", [Project].[BadgeET],[Project].[Badge]))


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top