Solved

MS Access - merge multiple row values into 1 cell

Posted on 2006-11-21
7
330 Views
Last Modified: 2012-05-05
Below is my dataset from a query I run.


JOB_NAME                          ID_NUMERIC                  ANALYSIS
EARL-2006-02942          246875                                  COND_ANAL
EARL-2006-02942          246876                                  COND_ANAL
EARL-2006-02942          246877                                  COND_ANAL
EARL-2006-02942          246878                                  COND_ANAL
EARL-2006-02942          246879                                  COND_ANAL
EARL-2006-02942          246875                                  CYANIDE
EARL-2006-02942          246876                                  CYANIDE
EARL-2006-02942          246877                                  CYANIDE
EARL-2006-02942          246878                                  CYANIDE
EARL-2006-02942          246879                                  CYANIDE
EARL-2006-02942          246875                                  EOA
EARL-2006-02942          246876                                  EOA
EARL-2006-02942          246877                                  EOA
EARL-2006-02942          246878                                  EOA
EARL-2006-02942          246879                                  EOA

SELECT DISTINCT VGSM_SAMP_JOB_TEST_RESULT.JOB_NAME, VGSM_SAMP_JOB_TEST_RESULT.ANALYSIS, VGSM_SAMP_JOB_TEST_RESULT.SAMPLED_DATE, VGSM_SAMP_JOB_TEST_RESULT.ID_NUMERIC, VGSM_SAMP_JOB_TEST_RESULT.SAMPLING_POINT
FROM VGSM_DOW_COMP_LIMIT_ENTRY, VGSM_SAMP_JOB_TEST_RESULT
WHERE (((VGSM_SAMP_JOB_TEST_RESULT.JOB_NAME) Like "EARL-2006-0294%") AND ((VGSM_SAMP_JOB_TEST_RESULT.ANALYSIS) Not Like "%DUP"));

I would like to summarize the data in one row as below.  This would require merging multiple ANALYSIS values for each ID_NUMERIC.


JOB_NAME                          ID_NUMERIC      ANALYSIS
EARL-2006-02942          246875                      COND_ANAL, CYANIDE, EOA
EARL-2006-02942          246876                      COND_ANAL, CYANIDE, EOA
EARL-2006-02942          246877                      COND_ANAL, CYANIDE, EOA
EARL-2006-02942          246878                      COND_ANAL, CYANIDE, EOA
EARL-2006-02942          246879                      COND_ANAL, CYANIDE, EOA


Any help?
0
Comment
Question by:u247073
  • 3
  • 2
  • 2
7 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 17989465
Hi u247073,

There is a general function to do this here:
http://www.mvps.org/access/modules/mdl0008.htm

It is bit heavy for individual cases but it deals with lots of stuff and will do what you want.


Pete
0
 

Author Comment

by:u247073
ID: 17989635
This will work to contatenate multiple field from different fields into one.

Will it also work to contanenate multiple values from one field in MS Access?
0
 
LVL 77

Expert Comment

by:peter57r
ID: 17990262
'Will it also work to contanenate multiple values from one field in MS Access?'

That IS what the function does.

Calling your current query 'queryname', I think you create another query as....

SELECT job_name, id_numeric, fConcatFld("queryname","id_numeric","analysis","string",[id_numeric]) AS analyses
FROM queryname GROUP BY job_name, id_numeric;

Pete
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 58

Expert Comment

by:harfang
ID: 17990361
Hello u247073

Your query includes a table VGSM_DOW_COMP_LIMIT_ENTRY which is never used. It has no effect and can be removed. Then, if you need to concatenate fields, you can only include the distinct fields, and no others. Also, it's not very efficient to use DISTINCT, it's faster if you can use a GROUP BY clause instead. That way, your query will boil down to:

SELECT
 JOB_NAME,
 ID_NUMERIC,
 DConcat("ANALYSIS",
    "VGSM_SAMP_JOB_TEST_RESULT",
    "ID_NUMERIC=" & ID_NUMERIC & " AND ANALYSIS Not Like '%DUP' ")
FROM VGSM_SAMP_JOB_TEST_RESULT
WHERE JOB_NAME Like "EARL-2006-0294%"
GROUP BY ID_NUMERIC;

The function being:


Function DConcat(Expr As String, Domain As String, _
    Optional Criteria = Null, _
    Optional Delim As String = ", ")
'
' Similar to DLookup(), but returning all values found
'
On Error GoTo Failure

    DConcat = Null
    With New ADODB.Recordset
        .Open "Select " & Expr & " From " & Domain & " Where " + Criteria _
        , CurrentProject.Connection, adOpenStatic, adLockReadOnly
        If Not .EOF Then
            DConcat = .GetString(RowDelimeter:=Delim)
            DConcat = Left(DConcat, Len(DConcat) - Len(Delim))
        End If
    End With

Failure:
    If Err Then Err.Clear
End Function


Good luck!

(°v°)
0
 
LVL 58

Expert Comment

by:harfang
ID: 17990377
Sorry, forgot one field:

[...]
GROUP BY JOB_NAME, ID_NUMERIC;

(°v°)
0
 

Author Comment

by:u247073
ID: 17991057
I'm pretty new to Access - how do you create this function and apply it to the fields - in a MAcro like Excel?

Thanks
0
 
LVL 58

Accepted Solution

by:
harfang earned 250 total points
ID: 17991212
u247073

Yes, very similar to a user defined functions in Excel.
* find your modules tab
* click [new] -- this opens the VBA window.
* below "Option Explicit", paste the function above
* check it: menu "debug / compile ..."
* click save and name it "Module1" or "basFunctions", etc.

After that, you can use the new function in calculated fields, queries, or other VB code.

Good luck!
(°v°)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

856 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