Solved

MS Access - merge multiple row values into 1 cell

Posted on 2006-11-21
7
338 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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.
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…

617 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