A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Need some query help.....



 
 
Thread Tools Display Modes
  #1  
Old May 5th, 2010, 04:11 PM posted to microsoft.public.access.queries
JAmes
external usenet poster
 
Posts: 904
Default Need some query help.....

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.

  #2  
Old May 5th, 2010, 04:26 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default Need some query help.....

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

James wrote:
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.

  #3  
Old May 5th, 2010, 04:29 PM posted to microsoft.public.access.queries
Dorian
external usenet poster
 
Posts: 542
Default Need some query help.....

In SQL mode, enter in your query:
IIF(TestType='Eng',Project.BadgeET,IIF(TestType='P kg',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".


"James" wrote:

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.

  #4  
Old May 5th, 2010, 07:02 PM posted to microsoft.public.access.queries
JAmes
external usenet poster
 
Posts: 904
Default Need some query help.....

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!
  #5  
Old May 5th, 2010, 11:25 PM posted to microsoft.public.access.queries
Dorian
external usenet poster
 
Posts: 542
Default Need some query help.....

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".


"James" wrote:

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!

  #6  
Old May 6th, 2010, 02:06 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default Need some query help.....

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

James wrote:
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!

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 10:26 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.