Solved

MS Access - merge multiple row values into 1 cell

Posted on 2006-11-21
7
322 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server functions 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 Ac…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now