From: James on 5 May 2010 11:11 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.
From: John Spencer on 5 May 2010 11:26 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. >
From: Dorian on 5 May 2010 11:29 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". "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. >
From: James on 5 May 2010 14:02 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!
From: Dorian on 5 May 2010 18:25
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! |