Solved Query duplicating client??? Maybe? (1 Viewer)

allen675

Member
Local time
Today, 11:16
Joined
Jul 13, 2022
Messages
124
Hello,

I have attached a screenshot of a query that I have put together. It wont be obvious from looking at it (because I've had to cover confidential information) however the two rows that are underlined are the same client record. I have a note system that stores notes for each client in a linked table under their customerID. This allows end user to make timestamped un-editable notes for that client. This query with some VBA then moves information to an Excel spreadsheet to send to directors. I was expecting the query to add all notes into one 'result field' in the query, however, It would appear that it has simply adding an additional row each time for clients depending on how many note records they have stored. What am I doing wrong and how do I get to my desired outcome?
 

Attachments

  • Query example.PNG
    Query example.PNG
    23.4 KB · Views: 69

plog

Banishment Pending
Local time
Today, 05:16
Joined
May 11, 2011
Messages
11,638
Its not duplicating anything. David has 1 record in clients and 2 records in notes. Number of results of a query based on an INNER JOIN is a simple calculation:

[Records In Table A] x [Records in Table B]

1 x 2 = 2

To get all notes into 1 note field I suggest you use Allen Brown's concate related function:

 

allen675

Member
Local time
Today, 11:16
Joined
Jul 13, 2022
Messages
124
Its not duplicating anything. David has 1 record in clients and 2 records in notes. Number of results of a query based on an INNER JOIN is a simple calculation:

[Records In Table A] x [Records in Table B]

1 x 2 = 2

To get all notes into 1 note field I suggest you use Allen Brown's concate related function:

Cheers @plog

That makes sense and the link that you have sent looks exactly as I was looking for. Don't have time to try it now but will do tomorrow and will report back to you how I got on.
 

allen675

Member
Local time
Today, 11:16
Joined
Jul 13, 2022
Messages
124
@Gasman funny you should say that! I've managed to follow Allen's instructions as far as adding the module but to be fair thats quite easy! This is the SQL that I have so far:

Code:
SELECT Client.Broker, Client.Lead_Date, [Client_FN] & " " & [Client_SN] AS LF, Client.Email_Address, Client.Mobile_No, Client.Email_Sent, Client.[SMS/WhatsApp_Sent], Client.[Phone_Call_#1], Client.[Phone_Call_#2], Client.[Phone_Call_#3], Client.ClientStatus, Client.Mortgage_Type, Client.Mortgage_Amount, [NoteDate] & ": " & [Note] AS Notes
FROM Client INNER JOIN NoteHistory ON Client.CustomerID = NoteHistory.CustomerID;

Which was giving me the result as it did when I opened this thread. Now I'm sitting hear scratching my bonce (head) trying to understand how I fit that into this OR this into that:

Code:
SELECT CompanyName,  ConcatRelated("OrderDate", "tblOrders", "CompanyID = " & [CompanyID])
FROM tblCompany;

Which is Allen's example.

HELP!
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:16
Joined
Sep 21, 2011
Messages
14,238
I thought you wanted to concatenate the notes?
 

allen675

Member
Local time
Today, 11:16
Joined
Jul 13, 2022
Messages
124
Yes I do but I also want said notes to then show at the end of the client record row under notes. An I getting confused or the wrong end of the stick? Sorry 😳
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:16
Joined
Sep 21, 2011
Messages
14,238
Read ALL of that link, it has a full explanation as to how it works.
Your FullNotes field will hold the result of the concatenation. Not sure if you can give it an alias of Notes, if you have a field by that name? Try it anyway.
 

allen675

Member
Local time
Today, 11:16
Joined
Jul 13, 2022
Messages
124
Yep looked at all of the link still don't get it! As you can probably tell I don't do this sort of thing for a living!
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:16
Joined
Sep 21, 2011
Messages
14,238
Try the other link then. It is simpler.
 

allen675

Member
Local time
Today, 11:16
Joined
Jul 13, 2022
Messages
124
@Gasman I have looked at the other link and it makes as about as much sense as the first. Its all very well telling someone to read something, its all there, but if they don't understand in the first instance they still wont understand after reading it. The links tell you what will happen and explains the arguments, agreed. Its a bit like me handing my daughter a sheet of French, not being her first language, she isn't going to be able to understand it just by reading it, she needs someone to teach and give her some guidance. Its no different with SQL. How do I make it work in my scenario and where does it go?
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:16
Joined
Sep 21, 2011
Messages
14,238
You would need to show your code so far and say where the Notes field is in which table and how they are linked.
 

mike60smart

Registered User.
Local time
Today, 11:16
Joined
Aug 6, 2017
Messages
1,904
@Gasman I have looked at the other link and it makes as about as much sense as the first. Its all very well telling someone to read something, its all there, but if they don't understand in the first instance they still wont understand after reading it. The links tell you what will happen and explains the arguments, agreed. Its a bit like me handing my daughter a sheet of French, not being her first language, she isn't going to be able to understand it just by reading it, she needs someone to teach and give her some guidance. Its no different with SQL. How do I make it work in my scenario and where does it go?
Why not just create a Report based on your query and then in the Report you can group by Client.
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:16
Joined
Sep 21, 2011
Messages
14,238
@Gasman I have looked at the other link and it makes as about as much sense as the first. Its all very well telling someone to read something, its all there, but if they don't understand in the first instance they still wont understand after reading it. The links tell you what will happen and explains the arguments, agreed. Its a bit like me handing my daughter a sheet of French, not being her first language, she isn't going to be able to understand it just by reading it, she needs someone to teach and give her some guidance. Its no different with SQL. How do I make it work in my scenario and where does it go?
No, it is more like telling someone how to make a cup of tea. Only in your case you want a cup of coffee.
However you need to understand what needs to be changed to get that cup of coffee. It is not the kettle, the cup or the water. :)

Effectively you are creating a new field from the results of whichever function you want to use.
let's go with theDBguy's function.
Code:
Public Function SimpleCSV(strSQL As String, _
            Optional strDelim As String = ",") As String
'Returns a comma delimited string of all the records in the SELECT SQL statement
'Source: http://www.accessmvp.com/thedbguy
'v1.0 - 8/20/2013

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strCSV As String

Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)

'Concatenate the first (and should be the only one) field from the SQL statement
With rs
    Do While Not .EOF
        strCSV = strCSV & strDelim & .Fields(0)
        .MoveNext
    Loop
    .Close
End With

'Remove the leading delimiter and return the result
SimpleCSV = Mid$(strCSV, Len(strDelim) + 1)

Set rs = Nothing
Set db = Nothing

End Function

Just seen your extra post. Well done (y). Nothing like accomplishing something yourself. :)
Plus if you want to continue with Access, that is the only way?

Try and understand it as well.

I'll stop what I was going to do now then.

Please post your solution, as that will help others in the future, who had the same difficulty you did.

FWIW I use AB's method in a report of mine (theDBguy's version was not available to me then) in a report control to get all the common data in that control. So not just for queries
1658504301928.png
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:16
Joined
Sep 21, 2011
Messages
14,238
This is the AB method
Code:
=ConcatRelated("CrewName","QryCrewStandby","QryCrewStandby.Rank = """ & [Rank] & """ AND QryCrewStandby.On_Date = #" & Format([Dated],"mm/dd/yyyy") & "#","CrewNameRev")

I wanted to return the crew name from QryCrewStandBy where the Rank was equal to the Rank for that particular report record and the QryCrewStandby.On_Date matched a Date in that Record. finally I wanted them presented in alphabetical order by surname then initials, hence the CrewName in reverse.
That is result in the above post.
 

Users who are viewing this thread

Top Bottom