Solved

custom aggregate function

Posted on 2001-07-06
6
775 Views
Last Modified: 2007-12-19
I am trying to do the following:
in a select query I want to use Group by,and for one of the fields I want to be able to do a custom aggregate function (or call a function in a module) that concatenate the values of that field.
example:
Table1

F1   F2    F3
A    v1    a
B    v2    b
B    v2    c

I want the result of the query using group by and the custom function to be (group by F1,F2):

F1     F2     F3(after applying the function)
A      V1      a
B      V2      b,c        ......  concatenate fields

in brief, I am trying to use a similar function to the Access aggregate functions (like sum) where I can use it with group by. Any help of outlining the code to do this and what values to pass to the function is appreciated.


0
Comment
Question by:khal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 4

Expert Comment

by:krzycz
ID: 6261056

Declare a public function (in any standard module):

Public Function funcConcatenate(txtFieldName As String, txtDomainName As String, txtCriteria As String, txtSeparator As String) As String
    Dim rs As DAO.Recordset
    Dim res As String
   
    res = ""
   
    Set rs = CurrentDb.OpenRecordset("SELECT " & txtFieldName & " FROM " & txtDomainName & " WHERE " & txtCriteria)
    If Not rs.BOF Then
        rs.MoveFirst
        While Not rs.EOF
            res = res & rs(txtFieldName) & txtSeparator
            rs.MoveNext
        Wend
        res = Left(res, Len(res) - Len(txtSeparator))
    End If
    rs.Close
   
    funcConcatenate = res
End Function



Usage:

SELECT F1, F2, funcConcatenate("F3", "TableName", "F1='" & F1 & "' AND F2='" & F2 & "'", ",") AS F3
FROM TableName
GROUP BY F1, F2;


HTH
Need help? - Just @skme!
0
 

Author Comment

by:khal
ID: 6261129
Thanks
it looks like this function will do it, but for some reason I am having an error when I am passing those variables from the query. It does not like the punctuation and it is giving error message
Compile error in query function funcConcatenate("F3", "Table1", "F1='" & F1 & "' AND F2='" & F2 & "'", ",") AS F3

do you see any problem with the punctuation?

0
 
LVL 4

Expert Comment

by:krzycz
ID: 6261641
Try to use semicolons ";" instead of commas "," to separate parameters.

What is the way you open your query?
Is it a saved query?
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:khal
ID: 6261707
semicolons did not work. I use a saved select query. What is the difference?

0
 

Author Comment

by:khal
ID: 6261712
This is the exact SQL for the query

SELECT Table1.f1, Table1.f2, funcConcatenate("F3", "Table1", "F1='" & F1 & "' AND F2='" & F2 & "'", ",") AS F3
FROM Table1
GROUP BY Table1.f1, Table1.f2;

 and this is the error message:

compile error in query expression 'funcConcatenate("F3", "Table1", "F1='" & F1 & "' AND F2='" & F2 & "'", ",")'.
0
 
LVL 4

Accepted Solution

by:
krzycz earned 100 total points
ID: 6261738
I have no idea.
I've just checked this function with one of my tables and it works fine.

Are you sure the function works? Did you try to compile all the modules?

Try to use this function in another simple query. i.e. just:

SELECT funcConcatenate("F3", "Table1", "F1='" & F1 & "' AND F2='" & F2 & "'",
",") AS F3
FROM Table1;

Maybe it will be easier to find an error.

Finally, you can send me a copy of your database (you can delete all the non-required objects). Just keep the table, the query and the module with the function. Then compress it and send me as a ZIP archive.
I'll try to help you.

krzycz@amg.gda.pl
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

615 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