Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

IIf() in Query to return Yes/No if sub-records exist or not MS-Acces

Posted on 2009-04-04
5
Medium Priority
?
770 Views
Last Modified: 2013-11-27
I need to know how to get a field in a query to indicate by "yes" or "No" whether records exist in a child table of one of the tables in the Query. As yet the child table is not represented in the query.
I have tried wrapping dCount() in IIf() but no luck:
iif(dCount("ChildTable.Key","ChildTable","ChildTableKey = ParentTable.key") > 0,"Yes","No")
The query populates a listbox
0
Comment
Question by:zoomtrac
5 Comments
 
LVL 2

Expert Comment

by:pskeens
ID: 24070287
How about

Iif(Count([childTable].[field]) > 0, "YES", "NO")
0
 
LVL 2

Accepted Solution

by:
pskeens earned 1500 total points
ID: 24070290
You can do DCOUNT this way as well

HAS_CHILD: IIf(DCount("*","ChildTable",[MasterField]=[ChildField])>0,"YES","NO")
0
 
LVL 11

Expert Comment

by:mildurait
ID: 24070527
You can do this using EXISTS keyward

Column Definition:
HasChldren: EXISTS (SELECT * FROM CHILDTABLE WHERE PARENTTABLE.ID=CHILDTABLE.ID)

Full SQL Syntax:
SELECT
   *,
   EXISTS (SELECT * FROM CHILDTABLE WHERE PARENTTABLE.ID=CHILDTABLE.ID) as HasChildren
FROM PARENTTABLE
0
 
LVL 31

Expert Comment

by:hnasr
ID: 24071419
Try this:
tparent:
id
1
2
3
4
tchild:
id      idparent
1      1
2      1
3      2

SELECT tparent.id, IIf(IsNull([idparent]),"No","Yes") AS Expr1
FROM tparent LEFT JOIN tchild ON tparent.id = tchild.idparent;

id      Expr1
1      Yes
1      Yes
2      Yes
3      No
4      No
0
 

Author Closing Comment

by:zoomtrac
ID: 31566689
Well it was pretty close just a syntax issue:
HasRecords: IIf(DCount("*","tblMLMtrans","[tblMLMtrans].[MLMID] =" & [MLMID])>0,"Yes","No")
where tblMLMtrans is the child table NOT in the Query and the MLMID refers to parent table field in query (needed the ="& syntax) is this because it refers to a value in the query itself?
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

876 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