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. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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 | |
|
|