ConcatRelated() Function to provide Unique Values on a Form

喜夏-厌秋 提交于 2020-01-24 10:41:07

问题


I have started developing an Access Database for my agency to utilize, starting with incident management. I have been attempting to develop a form that acts as a master index for all of our incidents, as well as a hub to open the investigation's associated form to be used by the investigator. I developed three regular tables and a junction table:

  • Investigations - General Information
  • Target(s)
  • Victim(s)
  • Target/Victim Joiner

The General Info Table has a one to many relationship to Targets, and Targets and Victims have a many to many relationship connected through the joiner table. This joiner table is also where the allegations and outcomes are so that each target and victim are have their own associated charge and outcome.

I ran two queries; one named Tar/VicQuery that features information gathered from the related tables above:

InternalIncidentID  TargetID    TargetFullName  VictimID    VictimFullName  Offense(s)  Outcome(s)
JAW-1               TAR-1       Smith Davie     VIC-1       Harris Michelle Neglect    Substantiated
JAW-1               TAR-1       Smith Davie     VIC-2       Jones Mary      Neglect    Substantiated
JAW-2               TAR-2       Thimble John    VIC-3       Man Joseph      Abuse      Substantiated
JAW-2               TAR-3       Rocket Sammy    VIC-3       Man Joseph      Abuse      Substantiated
JAW-2               TAR-4       Berkowitz Josh  VIC-3       Man Joseph      Abuse      Substantiated
JAW-3               TAR-5       McGowen Melissa VIC-4       Root James      Theft      Founded
JAW-3               TAR-5       McGowen Melissa VIC-5       Lopez Randy     Theft      Founded
JAW-3               TAR-5       McGowen Melissa VIC-6       Martino Bruno   Theft      Founded
JAW-3               TAR-6       Thimble John    VIC-4       Root James      Theft      Unfounded
JAW-3               TAR-6       Thimble John    VIC-5       Lopez Randy     Theft      Unfounded
JAW-3               TAR-6       Thimble John    VIC-6       Martino Bruno   Theft      Founded

And another Query that runs off the above mentioned Query called TargetQuery:

InternalIncidentID  TargetName
JAW-1               Smith Davie, Smith Davie
JAW-2               Thimble John, Rocket Sammy, Berkowitz Josh
JAW-3               McGowen Melissa, McGowen Melissa, McGowen Melissa, Thimble John, Thimble John, Thimble John

This above query uses Allen Browne's method of ConcatRelated to combine rows of data that have the same incident ID and concatenate the targets of the investigation. I have followed the instructions that Allen expresses on this page including creating the associated module, pasting in his function, and then attempting to utilize it as part of a query. I am also looking to do the same in another (or the same, if it's possible) query for victims attached to a case.

SQL Code for TargetQuery:

SELECT DISTINCT [Tar/Vic Query].InternalIncidentID, ConcatRelated("TargetFullName","[Tar/Vic Query]","InternalIncidentID= " & [Tar/Vic Query].[InternalIncidentID]) AS TargetName
FROM [Tar/Vic Query];

The results, as seen above, are very close to what I am hoping to achieve. The ideal would be that duplicate names do not appear again as part of the concatenate. How do I make this happen?

I attempted to use unique values to remedy this, which helped considerably in one way (reducing the number of records from 11 to 3), but did not solve the issue of "Davie Smith" appearing multiple times in the field for a case (in this case, he had two victims, as expressed in the first table). This is only part one of my conundrum unfortunately as I am fairly new to Access and SQL in general; I am hoping that, by the end of this, my form will look like this (with more fields prior to target and victim names but you get the idea):

  InternalIncidentID    TargetName                                 VictimName 
    JAW-1               Smith Davie                                Harris Michelle, Jones Mary
    JAW-2               Thimble John, Rocket Sammy, Berkowitz Josh Man Joseph
    JAW-3               McGowen Melissa Thimble John               Root James, Lopez Randy, Martino Bruno

Any help/education that can be provided on this would be much appreciated. I am well aware of the gap in my understanding of this program currently and am appreciative of all patience that is provided to me regarding this question. Below are the threads here on this website and elsewhere that I have reviewed prior to making this question:

Source 1 Source 2 Source 3 Source 4 Source 5


回答1:


Allen's procedure allows only to provide WHERE criteria to the function. Other versions I've seen allow to pass an entire SQL statement.

Will have to build 2 queries that return DISTINCT values for each InternalIncidentID - one for targets and one for victims. Those queries will be source for each of the calls to Allen's function.

qryIncTargets

SELECT DISTINCT InternalIncidentID, TargetFullName FROM [Tar/Vic Query];

qryIncVictims

SELECT DISTINCT InternalIncidentID, VictimFullName FROM [Tar/Vic Query];

qryConcatenate

SELECT Investigations.InternalIncidentID, 
ConcatRelated("TargetFullName","qryIncTargets","InternalIncidentID='" & [InternalIncidentID] & "'") AS Tars, 
ConcatRelated("VictimFullName","qryIncVictims","InternalIncidentID='" & [InternalIncidentID] & "'") AS Vics
FROM Investigations;

Could eliminate [Tar/Vic Query] and instead build the two DISTINCT queries with JOIN of [Target/Victim Joiner] to [Target(s)] and [Victim(s)].



来源:https://stackoverflow.com/questions/59329412/concatrelated-function-to-provide-unique-values-on-a-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!