Convert multiple records into a single record (1 Viewer)

JudyR

New member
Local time
Today, 05:06
Joined
Feb 14, 2024
Messages
4
OK, let me see if I can phrase this in an understandable manner. :)

I am working on a membership database.

I have a table that contains a single record for each person, with a unique Individual ID.
Each person has a 'Family ID' - all people with the same ID are part of one family.
In each family, there can be up to two adults (Member and Spouse), and any number of children (let's say no more than 6 for now).

So, for example, I have the three families below:

Family IDIndividual IDFirst NameLast NameFamily Position
ABC123100001JohnDoeMember
ABC123100002JaneDoeSpouse
ABC123100003DavidDoeChild
ABC123100004LucyDoeChild
BCD234100005BillJonesMember
CDE345100006KelseySmithMember
CDE345100007EvaSmithSpouse


What I need is to convert those seven records into three records, like so:

Family IDLast NameAdult 1Adult 2Child 1Child 2Child 3Child 4Child 5Child 6
ABC123DoeJohnJaneDavidLucy
BCD234JonesBill
CDE345SmithKelseyEva

My first instinct is that I'd need to write a module to do this - a query/report is probably not going to do it. But I'm so rusty at VBA I'm not sure where to start.

I did manage to get the first four columns of my result, with a bunch of queries. But a variable number of children has me stuck.

Ideally, the resulting data would spin out to a text file, but a table is fine, if that's easier.

Any thoughts (other than "You've lost your mind" - I have that one already ;))?

Thanks!
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:06
Joined
Oct 29, 2018
Messages
21,473
Hi. You might be able to get that using some helper queries and one final Crosstab query. Can you post a sample db with test data?
 

plog

Banishment Pending
Local time
Today, 04:06
Joined
May 11, 2011
Messages
11,646
Think this is going to be easier than you think. 2 words and 1 link:

Crosstab query:


You'll first need a subquery to order the adults (1&2) and the children (1-6)--you'll use a DCount or correlated subquery to get that. But once you have a query that can turn the Family Position values into the corect values to have as column headings, you will then be able to use that query as a basis for another query which will be your crosstab.

Give it a shot and then post back here any issues you have.
 

JudyR

New member
Local time
Today, 05:06
Joined
Feb 14, 2024
Messages
4
Ah! Crosstabs queries are a weak spot for me.

I did manage to build a crosstab query that gave me a count of each type of family member by their 'Role' code (Member=1, Spouse=2, Child=4). So now I need to use that number of Role '4' records to assign data to appropriate columns?
 

plog

Banishment Pending
Local time
Today, 04:06
Joined
May 11, 2011
Messages
11,646
Again, you need to build a query before the crosstab to assign the correct values that will become field names. Perhaps 2 queries.

Converting "Spouse" and "Member" is an easy IIf statement: if "Member"->"Adult 1", if "Spouse"->"Adult 2". The real trick is the kids. For them you will need to use a DCOUNT (or correlated subquery--google that). In that DCount you will count how many [Individual ID] values are lower than the one in the current record for each [Family ID] and suffix that to the string "Child".

Something like (but definitely not exactly because I am freehand writing) this:
Code:
ChildCount: 1 + DCount("[Individual ID]", "YourTableNAmeHere", "[Family ID]=" & [Family ID] & " AND [Family Position]='Child' AND [Individual ID]<" & [Individiaul ID])
Again, again--you are not ready for the crosstab part of this until you can figure out the above. You need to use a regular query to assign the appropriate family position to each record before you start to mess with the cross tab portion of this.
 

JudyR

New member
Local time
Today, 05:06
Joined
Feb 14, 2024
Messages
4
Thanks! I'll plod along and see what I can work out. :)
 

Users who are viewing this thread

Top Bottom