473,467 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trying to use IIF with DlookUp in query

44 New Member
Writing a query for a database where in the query it's displaying the profile of a family. I want the dependents to appear in 1 field concatenated and separated by commas. So if there is more than 1 dependent in the family it appears in query in 1 field listed.

New to Access so i was trying to get 1 piece to work at a time.

what I have so far:
Dependents: IIf([Is Dependent]=True,DLookUp("[F_Name] & ' ' & [L_Name]","[tblPerson]"),'')

Table Sample:
Person Table
- PersonID (PK)
- F_Name
- L_Name
- DOB
- Email
- Mobile
- Sex
- Is Dependent
- FamilyID (FK)
etc....

Family Table
- FamilyID (PK)
- HeadID
- SpouseID
- Address
- City
- State
etc....
Mar 8 '19 #1

✓ answered by Seth Schrock

So you are going to need a query that joins tblRelationships to tblPerson and returns the person's name (concatenated if so desired) and the family ID (I'll call it qryPersonFamily). You can then use the ConcatRelated() function in a query like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT FamilyId
  2. , ConcatRelated("PersonName", "qryPersonFamily", "RelationshipType = 'D' And FamilyID_fk = " & [FamilyID_pk]) As Dependents
  3. , Address
  4. FROM tblFamily
How are you wanting to view your data? By the family and list the member in that family, or by the person and show the family(ies) that person is related to along with their spouse (if applicable)?

17 2109
Seth Schrock
2,965 Recognized Expert Specialist
DLookup() can only return one field, not a concatenated value ([F_Name] & ' ' & [L_Name]). Also, DLookup() only returns the first record which matches the criteria provided. In your case, no criteria is provided so it will return the first record in the family table. All this to say that you don't want to use DLookup() for this. Look at Allen Brown's Concatenate Related Records for a method to do what you are looking for.

I would also recommend changing your table structure. Consider the situation where a child grows older and gets married. He/she is now a child in one family and the head of household in another. It could get confusing where the relationships are in your current setup. Do you leave the FamilyID (FK) to be the parent's family and then put the personID in the HeadID or SpouseID?

Consider something like this:
Expand|Select|Wrap|Line Numbers
  1. tblPerson
  2. PersonID_pk
  3. F_Name
  4. L_Name
  5. ...
  6.  
  7.  
  8. tblFamily
  9. FamilyId_pk
  10. Address
  11. ...
  12.  
  13.  
  14. tblRelationships
  15. FamilyId_fk
  16. PersonId_fk
  17. RelationshipType
Relationship type could be a dropdown box with Head of Household, Spouse, Child, etc. options. This allows the same person to be in two families at the same time. This is called a Many-to-Many relationship. The table relationships go through the table tblRelationships to tie each person to the appropriate family.

Just something to consider.
Mar 8 '19 #2
brikusi
44 New Member
Thanks for you response, it has been helpful. I have changed my tables to your suggestion above using a Many to Many relationship. I also copied the function into my Access DB. I am still unsure now how to use the concatrelated function to put my dependents into 1 field in the query based on the FamilyID.
Mar 8 '19 #3
brikusi
44 New Member
I only want to concatenate the the fields where relationship Type = D

Example Result I want in Query:

PERSONID 0001
F_NAME John
L_NAME Doe
SPOUSE Jane Doe
ADDRESS 1234 Whatever Lane
MOBILE 123-456-7890
DEPENDANTS Brian, Lily
Mar 8 '19 #4
Seth Schrock
2,965 Recognized Expert Specialist
So you are going to need a query that joins tblRelationships to tblPerson and returns the person's name (concatenated if so desired) and the family ID (I'll call it qryPersonFamily). You can then use the ConcatRelated() function in a query like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT FamilyId
  2. , ConcatRelated("PersonName", "qryPersonFamily", "RelationshipType = 'D' And FamilyID_fk = " & [FamilyID_pk]) As Dependents
  3. , Address
  4. FROM tblFamily
How are you wanting to view your data? By the family and list the member in that family, or by the person and show the family(ies) that person is related to along with their spouse (if applicable)?
Mar 8 '19 #5
brikusi
44 New Member
I want show my data in the report by person and be able to list all relatives and show the address only once along with their spouse listed and dependents all in 1 field
Mar 8 '19 #6
Seth Schrock
2,965 Recognized Expert Specialist
Quick edit to qryPersonFamily: you will also need the RelationshipType field.

What is your Relationship type for spouse? S?
Mar 8 '19 #7
brikusi
44 New Member
Relationship Types:
S = Spouse
H = Head
D = Dependent
Mar 8 '19 #8
Seth Schrock
2,965 Recognized Expert Specialist
So if the person you are currently looking at is the spouse, do you want the Head to show up in the Spouse field?
Mar 8 '19 #9
brikusi
44 New Member
exactly! the form or report will view as like a person profile showing all the details about them.
Mar 8 '19 #10
brikusi
44 New Member
I still can't get the ConcatRelate function to work, getting: "Error 3061: Too few Parameters.Expected 2"

Expand|Select|Wrap|Line Numbers
  1. SELECT FamilyId
  2. , ConcatRelated("FullName", "qryPersonFamily", "RelationshipType = 'D' And FamilyID = " & [FamilyID]) As Dependents
  3. FROM tblFamily
I used pretty much your exact script.
It pulls the FamilyID values but the column with Dependents is blank
Mar 8 '19 #11
Seth Schrock
2,965 Recognized Expert Specialist
Here is some air code for a query (untested).

Expand|Select|Wrap|Line Numbers
  1. SELECT PersonId_pk
  2. , tblPerson.F_Name
  3. , tblPerson.L_Name
  4. , S.F_Name & " " & S.L_Name As SpouseName
  5. , tblFamily.Address
  6. , ConcatRelated("PersonName", "qryPersonFamily", "RelationshipType = 'D' And FamilyID_fk = " & tblRelationships.FamilyID_fk) As Dependents
  7. , Address
  8. FROM tblFamily
  9. FROM ((tblPerson INNER JOIN tblRelationships ON tblPerson.PersonId_pk  = tblRelationships.PersonId_fk)
  10.     INNER JOIN tblFamily ON tblRelationships.FamilyId_fk = tblFamily.FamilyId_pk)
  11.     INNER JOIN (SELECT PersonID_pk, F_Name, L_Name, FamilyId_fk 
  12.                 FROM tblPerson INNER JOIN tblRelationships
  13.                 ON tblPerson.PersonID_pk = tblRelationships.PersonId_fk
  14.                 WHERE RelationshipType = "S" Or RelationshipType = "H") As S
  15.                 ON tblRelationships.PersonId_fk = S.PersonID_pk
  16.                     AND tblPerson.PersonID_pk <> S.PersonID_pk
You may need to add PersonID_pk to qryPersonFamily if it doesn't have it already.
Mar 8 '19 #12
Seth Schrock
2,965 Recognized Expert Specialist
Can you run qryPersonFamily with no errors? Please provide the SQL for qryPersonFamily.
Mar 8 '19 #13
brikusi
44 New Member
Thanks so much for all the attention you're giving this. I really appreciate your help.

Yes I can run qryPersonFamily with no errors.

Expand|Select|Wrap|Line Numbers
  1. SELECT [F_Name] & " " & [L_Name] AS FullName, tblFamilyRelationship.Family_ID
  2. FROM tblPerson INNER JOIN tblFamilyRelationship ON tblPerson.PersonID = tblFamilyRelationship.PersonID;
Mar 8 '19 #14
twinnyfo
3,653 Recognized Expert Moderator Specialist
There is an easier method to do this concatenation. You can build a small VBA procedures that recceives the Family ID and returns a concatenate list f family members. You simply build a Recordset based upon the criteria, and concatenate all the desired fields.

Let me try to come back to this later.
Mar 9 '19 #15
Seth Schrock
2,965 Recognized Expert Specialist
In your ConcatRelated() function, you are referencing a RelationshipType field and a FamilyID field, but your qryPersonFamily doesn't have RelationshipType returned at all in it and you have a underscore in the Family_ID field.

@TwinnyFo, we are using a VBA procedure to concatenate the dependents. Allen Browne's to be exact. See post #2.
Mar 9 '19 #16
twinnyfo
3,653 Recognized Expert Moderator Specialist
Sorry Seth, I was skimming too fast on a Saturday. Thanks for the hepp!
Mar 9 '19 #17
brikusi
44 New Member
thanks guys!!! This has helped a lot!
Mar 11 '19 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Dan Baker | last post by:
I'm trying to write a stand-alone application which will query the contact database used in Outlook (or maybe Exchange Server). I have no idea where to start looking. Any advice? Dan Baker
0
by: Winterminute | last post by:
I am trying to read a list of install programs using WMI with ASP.NET/C#. However, it fails with "Invalid Class". I have confirmed that if I query LOCALHOST then it works fine, but if I query a...
3
by: jt | last post by:
This is my sql statement in my vb6 code: My date variable name is, Fax_Today_Date devired by cdate(string) Set rs = db.OpenRecordset("SELECT * from psusa WHERE PSUSA.fax_today and...
1
by: Learner | last post by:
Hi there, I have installed Sql server 2005 developer on my machine which already has a Sql server 2000 installed on. Now i am trying to query the Sqlserver 2005 data(Ex: from Person.Address...
12
by: Tom | last post by:
Hello, I have a database of employee data in access and I am trying to create a form with combo boxes for criteria (ex. gender, office, position, etc.) that let the user select criteria from...
1
by: Adam G | last post by:
Greetings. Sorry if this sounds pretty basic, but I'm having some issues with the following... We have an e-mail list of ~2M, contained in a table. Via some marketing efforts, we've determined...
1
by: dsbsnag | last post by:
I am using Access 2003 on Windows XP I'm a school teacher and need help achieving the following: I have three spreadsheets with various information in them. But, they have a common cell named...
1
by: bruce24444 | last post by:
Working on a database to calculate the value of a home. User inputs criteria, which is calculated in a query to produce a base factor. With this base factor I need to apply a predetermined...
6
by: deewe | last post by:
Hello, On an LostFocus event I'm trying to update another field that is tied to a DLookUp query. I'm able to update all the form fields but I don't find a way to update only one field. The...
13
by: James Bowyer | last post by:
Hopefully this should be simple...! I have the following code IsOnSite = Nz(DLookup("&", "tblFittersBooked", "< #" & datecheck & "# AND > #" & datecheck & "# AND =" & Me.ID), "") If...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.