Access convert multi-valued field table
I've taken over a database that has a table with mutli-valued fields e.g
I'm trying to convert this into a single-valued field table e.g.
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
add a comment |
I've taken over a database that has a table with mutli-valued fields e.g
I'm trying to convert this into a single-valued field table e.g.
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
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
add a comment |
I've taken over a database that has a table with mutli-valued fields e.g
I'm trying to convert this into a single-valued field table e.g.
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
I've taken over a database that has a table with mutli-valued fields e.g
I'm trying to convert this into a single-valued field table e.g.
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
sql excel ms-access-2016
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
1
This worked perfectly thank you.
– TonyL
Nov 27 '18 at 14:29
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
1
This worked perfectly thank you.
– TonyL
Nov 27 '18 at 14:29
add a comment |
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.
1
This worked perfectly thank you.
– TonyL
Nov 27 '18 at 14:29
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 27 '18 at 13:28
Lee MacLee Mac
4,78531541
4,78531541
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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