gorlaz
asked on
Concatenating records for export
Hi,
I have a table - CategoryTX's where I store ID, CategoryID and GHCode.
ID is an autonumber, CategoryID is a number which relates to a Categories_List table as a one to many type and Join 1 - all records, GHCode is a product code which is unique in other tables but not this one.
The Categories_List table contains the related CategoryID and a list of Categories. The Table looks like;
2|Xbox360
4|Xbox360/Games
5|Xbox360/Games/Action
The TX table is intended to hold multiple categories per product (ID, CategoryID, GHCode);
eg
ID|2|360Kameo
ID|4|360Kameo
My issue is that now I've got my categories in and a form created to create those multiple categories - I can't get the data back out in a form that's usable to me! (Doh!)
I need a CSV file that lists the product and brings the multiple records for a product's categories back into one string of text in format;
first entry|second entry|third entry (ie, records get combined and separated by the pipe character in actuality).
My issue is I can't figure out how to get the data back into one record; I know I need to concatenate the data once I can get it together, but its the getting it together that's causing me grief.
Any ideas?
I have a table - CategoryTX's where I store ID, CategoryID and GHCode.
ID is an autonumber, CategoryID is a number which relates to a Categories_List table as a one to many type and Join 1 - all records, GHCode is a product code which is unique in other tables but not this one.
The Categories_List table contains the related CategoryID and a list of Categories. The Table looks like;
2|Xbox360
4|Xbox360/Games
5|Xbox360/Games/Action
The TX table is intended to hold multiple categories per product (ID, CategoryID, GHCode);
eg
ID|2|360Kameo
ID|4|360Kameo
My issue is that now I've got my categories in and a form created to create those multiple categories - I can't get the data back out in a form that's usable to me! (Doh!)
I need a CSV file that lists the product and brings the multiple records for a product's categories back into one string of text in format;
first entry|second entry|third entry (ie, records get combined and separated by the pipe character in actuality).
My issue is I can't figure out how to get the data back into one record; I know I need to concatenate the data once I can get it together, but its the getting it together that's causing me grief.
Any ideas?
I think you need a function that loops through your recordset and does the concatenation. Pass the ID to this function, create a recordset based on this ID
Dim cnn as adodb.connection, rst as new adodb.recordset, strReturnValue As string
Set cnn = currentproject.connection
rst.open "Select * from CategoryTX Inner Join Category_List On CategoryTX.CategoryID = Category_List.CategoryID Where ID = " & lngID & " Order By ID", cnn, dbOpenStatic, dbLockReadOnly
Then you can loop through this and concatenate the way you want.
EA
Dim cnn as adodb.connection, rst as new adodb.recordset, strReturnValue As string
Set cnn = currentproject.connection
rst.open "Select * from CategoryTX Inner Join Category_List On CategoryTX.CategoryID = Category_List.CategoryID Where ID = " & lngID & " Order By ID", cnn, dbOpenStatic, dbLockReadOnly
Then you can loop through this and concatenate the way you want.
EA
you will need a function to do this something like this
Function ConcatThem(x ) As String
Dim rs As DAO.Recordset, strTemp As String
Set rs = CurrentDb.OpenRecordset("S elect * from CategoryTX, Category_List.Categories Inner Join Category_List On CategoryTX.CategoryID = Category_List.CategoryID Where ID = " & x & " Order By ID")
rs.MoveFirst
Do Until rs.EOF
strTemp = strTemp & rs("Categories") & "|"
rs.MoveNext
Loop
ConcatThem = Left(strTemp, Len(strTemp) - 1)
End Function
Function ConcatThem(x ) As String
Dim rs As DAO.Recordset, strTemp As String
Set rs = CurrentDb.OpenRecordset("S
rs.MoveFirst
Do Until rs.EOF
strTemp = strTemp & rs("Categories") & "|"
rs.MoveNext
Loop
ConcatThem = Left(strTemp, Len(strTemp) - 1)
End Function
ASKER
Thanks guys - I'm not in front of that data at the moment so I can't test the solutions straight away but will do so at first opportunity (about 8hrs in future, at work at mo).
Where do I enter the function information? In the Builder?
@ard65 - the way I'd like the data to be in the end is (using comma for a separator as pipe is actually necessary to be included in the data);
GHCode, CombinedCategory's
MyCode1,Xbox360|Xbox360/Ga mes|Xbox36 0/Games/Ac tion
MyCode2,Xbox360|Xbox360/Ac cessories
MyCode3,Xbox360|Xbox360/Ga mes|Xbox36 0/Games/Ad venture|Xb ox360/Game s/Family
Where do I enter the function information? In the Builder?
@ard65 - the way I'd like the data to be in the end is (using comma for a separator as pipe is actually necessary to be included in the data);
GHCode, CombinedCategory's
MyCode1,Xbox360|Xbox360/Ga
MyCode2,Xbox360|Xbox360/Ac
MyCode3,Xbox360|Xbox360/Ga
place the function in a module.
and use the function in a query
and use the function in a query
ASKER
Sorry Capricorn I haven't used any code in Access before - how do I use the function in the query? just a matter of adding another column to a select statement in the table/column reference area?
try this query, i may have to ask you to post sample data from both tables
select ID, concatthem([id])
from [CategoryTX]
group by ID, concatthem([id])
select ID, concatthem([id])
from [CategoryTX]
group by ID, concatthem([id])
gorlaz, capricorn1's got your solution.
Just one note. capricorn1, you forgot an alias.
select ID, concatthem([id]) As ConCatString from [CategoryTX] group by ID, concatthem([id])
Also, just curious, why are you using a Totals query?
One last question, you're using DOA and he's got A2007. Do 2007 default to ADO?
EA
Just one note. capricorn1, you forgot an alias.
select ID, concatthem([id]) As ConCatString from [CategoryTX] group by ID, concatthem([id])
Also, just curious, why are you using a Totals query?
One last question, you're using DOA and he's got A2007. Do 2007 default to ADO?
EA
DAO is the default in A2007 references.
<Also, just curious, why are you using a Totals query? >
so you will get only one line per record
<Also, just curious, why are you using a Totals query? >
so you will get only one line per record
ASKER
Hi capricorn1 - when I try the query you posted in combination with the function I get the error - 'Run time error '3135', Syntax error in Join Operation'
I've amended the function to reflect the real column names - I didn't realise I had been so liberal with them above I'm sorry; the table names are Categories_List and CategoriesTX. Categories_List contains Category_ID and Category_Path. CategoriesTX contains ID, CategoryID and GHCode.
Function ConcatThem(x) As String
Dim rs As DAO.Recordset, strTemp As String
Set rs = CurrentDb.OpenRecordset("S elect * from CategoriesTX, Categories_List.Category_P ath Inner Join Categories_List On CategoriesTX.CategoryID = Categories_List.Category_I D Where ID = " & x & " Order By ID")
rs.MoveFirst
Do Until rs.EOF
strTemp = strTemp & rs("Category_Path") & "|"
rs.MoveNext
Loop
ConcatThem = Left(strTemp, Len(strTemp) - 1)
End Function
Example of data from CategoriesTX;
ID CategoryID GHCode
1 16 360Madden06
992 2 360BIAHellsHighway
Example of data from Categories_List;
Category_ID Category_Path
2 Xbox 360
3 Xbox 360/Pre Orders
In my Categories_List table I have little '+' signs to the left where when I expand them I can see all the records that have been put under that category eg;
ID GHCode
992 360BIAHellsHighway
I won't be able to respond for two days as I'm away for work, but will respond as soon as I'm able on my return. Any insights appreciated guys.
I've amended the function to reflect the real column names - I didn't realise I had been so liberal with them above I'm sorry; the table names are Categories_List and CategoriesTX. Categories_List contains Category_ID and Category_Path. CategoriesTX contains ID, CategoryID and GHCode.
Function ConcatThem(x) As String
Dim rs As DAO.Recordset, strTemp As String
Set rs = CurrentDb.OpenRecordset("S
rs.MoveFirst
Do Until rs.EOF
strTemp = strTemp & rs("Category_Path") & "|"
rs.MoveNext
Loop
ConcatThem = Left(strTemp, Len(strTemp) - 1)
End Function
Example of data from CategoriesTX;
ID CategoryID GHCode
1 16 360Madden06
992 2 360BIAHellsHighway
Example of data from Categories_List;
Category_ID Category_Path
2 Xbox 360
3 Xbox 360/Pre Orders
In my Categories_List table I have little '+' signs to the left where when I expand them I can see all the records that have been put under that category eg;
ID GHCode
992 360BIAHellsHighway
I won't be able to respond for two days as I'm away for work, but will respond as soon as I'm able on my return. Any insights appreciated guys.
ASKER
Hi Guys,
Any ideas as to why I'm getting the error?
Thanks
Any ideas as to why I'm getting the error?
Thanks
The select statement should read:
"Select *.CategoriesTX, *.Categories_List from CategoriesTX Inner Join Categories_List On CategoriesTX.CategoryID = Categories_List.Category_I D Where ID = " & x & " Order By ID"
EA
"Select *.CategoriesTX, *.Categories_List from CategoriesTX Inner Join Categories_List On CategoriesTX.CategoryID = Categories_List.Category_I
EA
ASKER
Thanks Eantar, I'll try it in about 4 hrs.
ASKER
Hi guys, no dice unfortunately.
New error is 'Run time error '3075'. Syntax error (missing operator) in query expression '*.CategoriesTX'.
Module code is;
Option Compare Database
Function ConcatThem(x) As String
Dim rs As DAO.Recordset, strTemp As String
Set rs = CurrentDb.OpenRecordset("S elect *.CategoriesTX, *.Categories_List from CategoriesTX Inner Join Categories_List On CategoriesTX.CategoryID = Categories_List.Category_I D Where ID = " & x & " Order By ID")
rs.MoveFirst
Do Until rs.EOF
strTemp = strTemp & rs("Category_Path") & "|"
rs.MoveNext
Loop
ConcatThem = Left(strTemp, Len(strTemp) - 1)
End Function
Query code is;
select ID, concatthem([id])
from [CategoriesTX]
group by ID, concatthem([id])
That Select statement above looks ok to me, would there be a setting or something I need to tweak or have I copied it in wrong?
New error is 'Run time error '3075'. Syntax error (missing operator) in query expression '*.CategoriesTX'.
Module code is;
Option Compare Database
Function ConcatThem(x) As String
Dim rs As DAO.Recordset, strTemp As String
Set rs = CurrentDb.OpenRecordset("S
rs.MoveFirst
Do Until rs.EOF
strTemp = strTemp & rs("Category_Path") & "|"
rs.MoveNext
Loop
ConcatThem = Left(strTemp, Len(strTemp) - 1)
End Function
Query code is;
select ID, concatthem([id])
from [CategoriesTX]
group by ID, concatthem([id])
That Select statement above looks ok to me, would there be a setting or something I need to tweak or have I copied it in wrong?
The name of your first table is CategoryTX not CategoriesTX. Replace the wrong one with the right one and it should work.
EA
EA
ASKER
File uploaded to ee-stuff.com under this question number
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
carpicorn1 - You're deadset amazing! Exactly what I was after, many thanks!
Finalised solution for future ref was;
Module:
Function ConcatThem(x As String) As String
Dim rs As DAO.Recordset, strTemp As String
Set rs = CurrentDb.OpenRecordset("S ELECT CategoriesTx.GHCode, Categories_List.Category_P ath FROM CategoriesTx INNER JOIN Categories_List ON CategoriesTx.CategoryID = Categories_List.Category_I D Where CategoriesTx.GHCode='" & x & "' ORDER BY CategoriesTx.GHCode")
rs.MoveFirst
Do Until rs.EOF
strTemp = strTemp & rs("Category_Path") & "|"
rs.MoveNext
Loop
ConcatThem = Left(strTemp, Len(strTemp) - 1)
End Function
Query:
SELECT CategoriesTx.GHCode, ConcatThem([GHCode]) AS CombinedCategory
FROM Categories_List INNER JOIN CategoriesTx ON Categories_List.Category_I D = CategoriesTx.CategoryID
GROUP BY CategoriesTx.GHCode, ConcatThem([GHCode])
ORDER BY CategoriesTx.GHCode;
Now I've got some research to do to find out what it all means :)
Finalised solution for future ref was;
Module:
Function ConcatThem(x As String) As String
Dim rs As DAO.Recordset, strTemp As String
Set rs = CurrentDb.OpenRecordset("S
rs.MoveFirst
Do Until rs.EOF
strTemp = strTemp & rs("Category_Path") & "|"
rs.MoveNext
Loop
ConcatThem = Left(strTemp, Len(strTemp) - 1)
End Function
Query:
SELECT CategoriesTx.GHCode, ConcatThem([GHCode]) AS CombinedCategory
FROM Categories_List INNER JOIN CategoriesTx ON Categories_List.Category_I
GROUP BY CategoriesTx.GHCode, ConcatThem([GHCode])
ORDER BY CategoriesTx.GHCode;
Now I've got some research to do to find out what it all means :)
ASKER
Fantastic - very grateful for the help
Depending on how many categories you have, have a play with crosstab queries (even the wizard may help to make a query you can refine). It should give you something close to what you want.
The CSV transfer is no challenge once you have the data you want to export. There are plenty of solutions for that in EE.
Andrew