Access convert multi-valued field table












1















I've taken over a database that has a table with mutli-valued fields e.g



Current multi valued field example



I'm trying to convert this into a single-valued field table e.g.



Desired single value field table



The number in both tables is just an ID that refers to a person named Contact_ID.



I've tried doing this in both Access using Crosstab queries and Excel with Transposing however I can't produce a new table with multiple examples of the same Contact_ID in one column and unique keywords in the second.



Appreciate any suggestions on the best way to do this.










share|improve this question

























  • So you want each comma separated entry on its own row with the same Id number as the original row, right?

    – Nick
    Nov 27 '18 at 12:29













  • Yes precisely. An option I'm looking into involves a new table with a separate FieldName (Field 1, Field 2 etc) for each keyword and then creating a query from that to show all keywords for each ID and copying that into a new table. It's not particularly elegant and I'm still holding out for a simpler solution.

    – TonyL
    Nov 27 '18 at 12:58
















1















I've taken over a database that has a table with mutli-valued fields e.g



Current multi valued field example



I'm trying to convert this into a single-valued field table e.g.



Desired single value field table



The number in both tables is just an ID that refers to a person named Contact_ID.



I've tried doing this in both Access using Crosstab queries and Excel with Transposing however I can't produce a new table with multiple examples of the same Contact_ID in one column and unique keywords in the second.



Appreciate any suggestions on the best way to do this.










share|improve this question

























  • So you want each comma separated entry on its own row with the same Id number as the original row, right?

    – Nick
    Nov 27 '18 at 12:29













  • Yes precisely. An option I'm looking into involves a new table with a separate FieldName (Field 1, Field 2 etc) for each keyword and then creating a query from that to show all keywords for each ID and copying that into a new table. It's not particularly elegant and I'm still holding out for a simpler solution.

    – TonyL
    Nov 27 '18 at 12:58














1












1








1








I've taken over a database that has a table with mutli-valued fields e.g



Current multi valued field example



I'm trying to convert this into a single-valued field table e.g.



Desired single value field table



The number in both tables is just an ID that refers to a person named Contact_ID.



I've tried doing this in both Access using Crosstab queries and Excel with Transposing however I can't produce a new table with multiple examples of the same Contact_ID in one column and unique keywords in the second.



Appreciate any suggestions on the best way to do this.










share|improve this question
















I've taken over a database that has a table with mutli-valued fields e.g



Current multi valued field example



I'm trying to convert this into a single-valued field table e.g.



Desired single value field table



The number in both tables is just an ID that refers to a person named Contact_ID.



I've tried doing this in both Access using Crosstab queries and Excel with Transposing however I can't produce a new table with multiple examples of the same Contact_ID in one column and unique keywords in the second.



Appreciate any suggestions on the best way to do this.







sql excel ms-access-2016






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 13:16









Lee Mac

4,78531541




4,78531541










asked Nov 27 '18 at 12:24









TonyLTonyL

154




154













  • So you want each comma separated entry on its own row with the same Id number as the original row, right?

    – Nick
    Nov 27 '18 at 12:29













  • Yes precisely. An option I'm looking into involves a new table with a separate FieldName (Field 1, Field 2 etc) for each keyword and then creating a query from that to show all keywords for each ID and copying that into a new table. It's not particularly elegant and I'm still holding out for a simpler solution.

    – TonyL
    Nov 27 '18 at 12:58



















  • So you want each comma separated entry on its own row with the same Id number as the original row, right?

    – Nick
    Nov 27 '18 at 12:29













  • Yes precisely. An option I'm looking into involves a new table with a separate FieldName (Field 1, Field 2 etc) for each keyword and then creating a query from that to show all keywords for each ID and copying that into a new table. It's not particularly elegant and I'm still holding out for a simpler solution.

    – TonyL
    Nov 27 '18 at 12:58

















So you want each comma separated entry on its own row with the same Id number as the original row, right?

– Nick
Nov 27 '18 at 12:29







So you want each comma separated entry on its own row with the same Id number as the original row, right?

– Nick
Nov 27 '18 at 12:29















Yes precisely. An option I'm looking into involves a new table with a separate FieldName (Field 1, Field 2 etc) for each keyword and then creating a query from that to show all keywords for each ID and copying that into a new table. It's not particularly elegant and I'm still holding out for a simpler solution.

– TonyL
Nov 27 '18 at 12:58





Yes precisely. An option I'm looking into involves a new table with a separate FieldName (Field 1, Field 2 etc) for each keyword and then creating a query from that to show all keywords for each ID and copying that into a new table. It's not particularly elegant and I'm still holding out for a simpler solution.

– TonyL
Nov 27 '18 at 12:58












2 Answers
2






active

oldest

votes


















0














The values in your multivalue field will be coming from a table in your database.



To return the individual items you need to add .Value to your query field.



For example,



If Table1 contains ID as an AutoNumber and MyLookUpItems as individual items.



| MyLookUpID | MyLookUpField |
|------------|---------------|
| 1 | Alcohol |
| 2 | Smoking |
| 3 | diet |
| 4 | Nutrition |


Table2 contains MyMultiValueField showing multiple values.



| MyMultiValueField |
|-------------------|
| Smoking, diet |
| Alcohol, diet |


To return the individual items you'd use:



SELECT MyMultiValueField.Value
FROM Table2


This would return:



| MyMultiValueField.Value |
|-------------------------|
| diet |
| Smoking |
| Alcohol |
| diet |


You can then link back to the original ID:



SELECT MyLookUpID, MyLookUpField
FROM Table2 INNER JOIN Table1 ON Table2.MyMultiValueField.Value = Table1.MyLookUpID


which would return:



| MyLookUpID | MyLookUpField |
|------------|---------------|
| 1 | Alcohol |
| 2 | Smoking |
| 3 | diet |


If you're not sure which is the source for the multivalue fields open the table in design view and look at the Lookup tab for the field.

The Row Source will look something like:



SELECT [Table1].[MyLookUpID], [Table1].[MyLookUpField] FROM Table1 ORDER BY [MyLookUpField]; 


Showing that Table1 is the source.



Edit:
Now, having written all that.... just look at the design for the table and the table in the Row Source is the single-value field table you were looking for in your question.






share|improve this answer





















  • 1





    This worked perfectly thank you.

    – TonyL
    Nov 27 '18 at 14:29



















0














With an unknown number of comma-delimited strings, it is likely easiest to iterate over the table using a recordset, use the Split function to separate the items, and populate another recordset, e.g.:



Function ConvertTable()
Dim strOldTable As String: strOldTable = "Table1"
Dim strNewTable As String: strNewTable = strOldTable & "_new"

Dim dbs As DAO.Database
Dim rst1 As DAO.Recordset
Dim rst2 As DAO.Recordset
Dim lngCID As Long
Dim itm

Set dbs = CurrentDb

DoCmd.CopyObject , strNewTable, acTablem, strOldTable
dbs.Execute "delete from [" & strNewTable & "]"

Set rst1 = dbs.OpenRecordset(strOldTable)
Set rst2 = dbs.OpenRecordset(strNewTable)
If Not rst1.EOF Then
rst1.MoveFirst
Do Until rst1.EOF
lngCID = rst1!Contact_ID
For Each itm In Split(rst1!Field1, ",")
If Trim(itm) <> "*" Then
rst2.AddNew
rst2!Contact_ID = lngCID
rst2!Field1 = Trim(itm)
rst2.Update
End If
Next itm
rst1.MoveNext
Loop
End If
rst1.Close
rst2.Close
Set rst1 = Nothing
Set rst2 = Nothing
Set dbs = Nothing
End Function





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499643%2faccess-convert-multi-valued-field-table%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The values in your multivalue field will be coming from a table in your database.



    To return the individual items you need to add .Value to your query field.



    For example,



    If Table1 contains ID as an AutoNumber and MyLookUpItems as individual items.



    | MyLookUpID | MyLookUpField |
    |------------|---------------|
    | 1 | Alcohol |
    | 2 | Smoking |
    | 3 | diet |
    | 4 | Nutrition |


    Table2 contains MyMultiValueField showing multiple values.



    | MyMultiValueField |
    |-------------------|
    | Smoking, diet |
    | Alcohol, diet |


    To return the individual items you'd use:



    SELECT MyMultiValueField.Value
    FROM Table2


    This would return:



    | MyMultiValueField.Value |
    |-------------------------|
    | diet |
    | Smoking |
    | Alcohol |
    | diet |


    You can then link back to the original ID:



    SELECT MyLookUpID, MyLookUpField
    FROM Table2 INNER JOIN Table1 ON Table2.MyMultiValueField.Value = Table1.MyLookUpID


    which would return:



    | MyLookUpID | MyLookUpField |
    |------------|---------------|
    | 1 | Alcohol |
    | 2 | Smoking |
    | 3 | diet |


    If you're not sure which is the source for the multivalue fields open the table in design view and look at the Lookup tab for the field.

    The Row Source will look something like:



    SELECT [Table1].[MyLookUpID], [Table1].[MyLookUpField] FROM Table1 ORDER BY [MyLookUpField]; 


    Showing that Table1 is the source.



    Edit:
    Now, having written all that.... just look at the design for the table and the table in the Row Source is the single-value field table you were looking for in your question.






    share|improve this answer





















    • 1





      This worked perfectly thank you.

      – TonyL
      Nov 27 '18 at 14:29
















    0














    The values in your multivalue field will be coming from a table in your database.



    To return the individual items you need to add .Value to your query field.



    For example,



    If Table1 contains ID as an AutoNumber and MyLookUpItems as individual items.



    | MyLookUpID | MyLookUpField |
    |------------|---------------|
    | 1 | Alcohol |
    | 2 | Smoking |
    | 3 | diet |
    | 4 | Nutrition |


    Table2 contains MyMultiValueField showing multiple values.



    | MyMultiValueField |
    |-------------------|
    | Smoking, diet |
    | Alcohol, diet |


    To return the individual items you'd use:



    SELECT MyMultiValueField.Value
    FROM Table2


    This would return:



    | MyMultiValueField.Value |
    |-------------------------|
    | diet |
    | Smoking |
    | Alcohol |
    | diet |


    You can then link back to the original ID:



    SELECT MyLookUpID, MyLookUpField
    FROM Table2 INNER JOIN Table1 ON Table2.MyMultiValueField.Value = Table1.MyLookUpID


    which would return:



    | MyLookUpID | MyLookUpField |
    |------------|---------------|
    | 1 | Alcohol |
    | 2 | Smoking |
    | 3 | diet |


    If you're not sure which is the source for the multivalue fields open the table in design view and look at the Lookup tab for the field.

    The Row Source will look something like:



    SELECT [Table1].[MyLookUpID], [Table1].[MyLookUpField] FROM Table1 ORDER BY [MyLookUpField]; 


    Showing that Table1 is the source.



    Edit:
    Now, having written all that.... just look at the design for the table and the table in the Row Source is the single-value field table you were looking for in your question.






    share|improve this answer





















    • 1





      This worked perfectly thank you.

      – TonyL
      Nov 27 '18 at 14:29














    0












    0








    0







    The values in your multivalue field will be coming from a table in your database.



    To return the individual items you need to add .Value to your query field.



    For example,



    If Table1 contains ID as an AutoNumber and MyLookUpItems as individual items.



    | MyLookUpID | MyLookUpField |
    |------------|---------------|
    | 1 | Alcohol |
    | 2 | Smoking |
    | 3 | diet |
    | 4 | Nutrition |


    Table2 contains MyMultiValueField showing multiple values.



    | MyMultiValueField |
    |-------------------|
    | Smoking, diet |
    | Alcohol, diet |


    To return the individual items you'd use:



    SELECT MyMultiValueField.Value
    FROM Table2


    This would return:



    | MyMultiValueField.Value |
    |-------------------------|
    | diet |
    | Smoking |
    | Alcohol |
    | diet |


    You can then link back to the original ID:



    SELECT MyLookUpID, MyLookUpField
    FROM Table2 INNER JOIN Table1 ON Table2.MyMultiValueField.Value = Table1.MyLookUpID


    which would return:



    | MyLookUpID | MyLookUpField |
    |------------|---------------|
    | 1 | Alcohol |
    | 2 | Smoking |
    | 3 | diet |


    If you're not sure which is the source for the multivalue fields open the table in design view and look at the Lookup tab for the field.

    The Row Source will look something like:



    SELECT [Table1].[MyLookUpID], [Table1].[MyLookUpField] FROM Table1 ORDER BY [MyLookUpField]; 


    Showing that Table1 is the source.



    Edit:
    Now, having written all that.... just look at the design for the table and the table in the Row Source is the single-value field table you were looking for in your question.






    share|improve this answer















    The values in your multivalue field will be coming from a table in your database.



    To return the individual items you need to add .Value to your query field.



    For example,



    If Table1 contains ID as an AutoNumber and MyLookUpItems as individual items.



    | MyLookUpID | MyLookUpField |
    |------------|---------------|
    | 1 | Alcohol |
    | 2 | Smoking |
    | 3 | diet |
    | 4 | Nutrition |


    Table2 contains MyMultiValueField showing multiple values.



    | MyMultiValueField |
    |-------------------|
    | Smoking, diet |
    | Alcohol, diet |


    To return the individual items you'd use:



    SELECT MyMultiValueField.Value
    FROM Table2


    This would return:



    | MyMultiValueField.Value |
    |-------------------------|
    | diet |
    | Smoking |
    | Alcohol |
    | diet |


    You can then link back to the original ID:



    SELECT MyLookUpID, MyLookUpField
    FROM Table2 INNER JOIN Table1 ON Table2.MyMultiValueField.Value = Table1.MyLookUpID


    which would return:



    | MyLookUpID | MyLookUpField |
    |------------|---------------|
    | 1 | Alcohol |
    | 2 | Smoking |
    | 3 | diet |


    If you're not sure which is the source for the multivalue fields open the table in design view and look at the Lookup tab for the field.

    The Row Source will look something like:



    SELECT [Table1].[MyLookUpID], [Table1].[MyLookUpField] FROM Table1 ORDER BY [MyLookUpField]; 


    Showing that Table1 is the source.



    Edit:
    Now, having written all that.... just look at the design for the table and the table in the Row Source is the single-value field table you were looking for in your question.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 27 '18 at 13:55

























    answered Nov 27 '18 at 13:46









    Darren Bartrup-CookDarren Bartrup-Cook

    13.9k11432




    13.9k11432








    • 1





      This worked perfectly thank you.

      – TonyL
      Nov 27 '18 at 14:29














    • 1





      This worked perfectly thank you.

      – TonyL
      Nov 27 '18 at 14:29








    1




    1





    This worked perfectly thank you.

    – TonyL
    Nov 27 '18 at 14:29





    This worked perfectly thank you.

    – TonyL
    Nov 27 '18 at 14:29













    0














    With an unknown number of comma-delimited strings, it is likely easiest to iterate over the table using a recordset, use the Split function to separate the items, and populate another recordset, e.g.:



    Function ConvertTable()
    Dim strOldTable As String: strOldTable = "Table1"
    Dim strNewTable As String: strNewTable = strOldTable & "_new"

    Dim dbs As DAO.Database
    Dim rst1 As DAO.Recordset
    Dim rst2 As DAO.Recordset
    Dim lngCID As Long
    Dim itm

    Set dbs = CurrentDb

    DoCmd.CopyObject , strNewTable, acTablem, strOldTable
    dbs.Execute "delete from [" & strNewTable & "]"

    Set rst1 = dbs.OpenRecordset(strOldTable)
    Set rst2 = dbs.OpenRecordset(strNewTable)
    If Not rst1.EOF Then
    rst1.MoveFirst
    Do Until rst1.EOF
    lngCID = rst1!Contact_ID
    For Each itm In Split(rst1!Field1, ",")
    If Trim(itm) <> "*" Then
    rst2.AddNew
    rst2!Contact_ID = lngCID
    rst2!Field1 = Trim(itm)
    rst2.Update
    End If
    Next itm
    rst1.MoveNext
    Loop
    End If
    rst1.Close
    rst2.Close
    Set rst1 = Nothing
    Set rst2 = Nothing
    Set dbs = Nothing
    End Function





    share|improve this answer




























      0














      With an unknown number of comma-delimited strings, it is likely easiest to iterate over the table using a recordset, use the Split function to separate the items, and populate another recordset, e.g.:



      Function ConvertTable()
      Dim strOldTable As String: strOldTable = "Table1"
      Dim strNewTable As String: strNewTable = strOldTable & "_new"

      Dim dbs As DAO.Database
      Dim rst1 As DAO.Recordset
      Dim rst2 As DAO.Recordset
      Dim lngCID As Long
      Dim itm

      Set dbs = CurrentDb

      DoCmd.CopyObject , strNewTable, acTablem, strOldTable
      dbs.Execute "delete from [" & strNewTable & "]"

      Set rst1 = dbs.OpenRecordset(strOldTable)
      Set rst2 = dbs.OpenRecordset(strNewTable)
      If Not rst1.EOF Then
      rst1.MoveFirst
      Do Until rst1.EOF
      lngCID = rst1!Contact_ID
      For Each itm In Split(rst1!Field1, ",")
      If Trim(itm) <> "*" Then
      rst2.AddNew
      rst2!Contact_ID = lngCID
      rst2!Field1 = Trim(itm)
      rst2.Update
      End If
      Next itm
      rst1.MoveNext
      Loop
      End If
      rst1.Close
      rst2.Close
      Set rst1 = Nothing
      Set rst2 = Nothing
      Set dbs = Nothing
      End Function





      share|improve this answer


























        0












        0








        0







        With an unknown number of comma-delimited strings, it is likely easiest to iterate over the table using a recordset, use the Split function to separate the items, and populate another recordset, e.g.:



        Function ConvertTable()
        Dim strOldTable As String: strOldTable = "Table1"
        Dim strNewTable As String: strNewTable = strOldTable & "_new"

        Dim dbs As DAO.Database
        Dim rst1 As DAO.Recordset
        Dim rst2 As DAO.Recordset
        Dim lngCID As Long
        Dim itm

        Set dbs = CurrentDb

        DoCmd.CopyObject , strNewTable, acTablem, strOldTable
        dbs.Execute "delete from [" & strNewTable & "]"

        Set rst1 = dbs.OpenRecordset(strOldTable)
        Set rst2 = dbs.OpenRecordset(strNewTable)
        If Not rst1.EOF Then
        rst1.MoveFirst
        Do Until rst1.EOF
        lngCID = rst1!Contact_ID
        For Each itm In Split(rst1!Field1, ",")
        If Trim(itm) <> "*" Then
        rst2.AddNew
        rst2!Contact_ID = lngCID
        rst2!Field1 = Trim(itm)
        rst2.Update
        End If
        Next itm
        rst1.MoveNext
        Loop
        End If
        rst1.Close
        rst2.Close
        Set rst1 = Nothing
        Set rst2 = Nothing
        Set dbs = Nothing
        End Function





        share|improve this answer













        With an unknown number of comma-delimited strings, it is likely easiest to iterate over the table using a recordset, use the Split function to separate the items, and populate another recordset, e.g.:



        Function ConvertTable()
        Dim strOldTable As String: strOldTable = "Table1"
        Dim strNewTable As String: strNewTable = strOldTable & "_new"

        Dim dbs As DAO.Database
        Dim rst1 As DAO.Recordset
        Dim rst2 As DAO.Recordset
        Dim lngCID As Long
        Dim itm

        Set dbs = CurrentDb

        DoCmd.CopyObject , strNewTable, acTablem, strOldTable
        dbs.Execute "delete from [" & strNewTable & "]"

        Set rst1 = dbs.OpenRecordset(strOldTable)
        Set rst2 = dbs.OpenRecordset(strNewTable)
        If Not rst1.EOF Then
        rst1.MoveFirst
        Do Until rst1.EOF
        lngCID = rst1!Contact_ID
        For Each itm In Split(rst1!Field1, ",")
        If Trim(itm) <> "*" Then
        rst2.AddNew
        rst2!Contact_ID = lngCID
        rst2!Field1 = Trim(itm)
        rst2.Update
        End If
        Next itm
        rst1.MoveNext
        Loop
        End If
        rst1.Close
        rst2.Close
        Set rst1 = Nothing
        Set rst2 = Nothing
        Set dbs = Nothing
        End Function






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 13:28









        Lee MacLee Mac

        4,78531541




        4,78531541






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499643%2faccess-convert-multi-valued-field-table%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks