Solved

MS Access - merge multiple row values into 1 cell

Posted on 2006-11-21
7
336 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
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…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

734 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