Solved

MS Access - merge multiple row values into 1 cell

Posted on 2006-11-21
7
327 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
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…

773 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