473,395 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Many to Many Database - Displaying query results as one entry with multiple categorie

I have a database that is based on having recipes, I have a Many to Many relationship set up. I have following:

RecipeTable:
RecipeID
RecipeName

RecipeCategoryTable:
RecipeID
CategoryID

CategoryTabe:
CategoryID
CategoryName

When I run a query of RecipeName I get multiple results, the name is still the same but each result will have a different category next to it.
For example:

Recipe Name
BBQ Pork
Category Name
Chinese

Recipe Name
BBQ Pork
Category Name
BBQ

Recipe Name
BBQ Pork
Category Name
Dinner

Is there a way to join these to display as one result as below?? Any help would be greatly appreciated. :)

Recipe Name
BBQ Pork
Category Name
Chinese, BBQ, Dinner
May 22 '16 #1
3 934
PhilOfWalton
1,430 Expert 1GB
It will require a bit of VBA which I will let you have this evening.
In the mean time, I think you need an additional field SortOrder in your RecipeCategory Table because if you want the result
Chinese, BBQ, Dinner
not
BBQ, Dinner, Chinese
we need to tell the program the order to print these out.
So those 3 fields need to be a unique key.

Phil
May 22 '16 #2
PhilOfWalton
1,430 Expert 1GB
I hope I get this right, as my version is using an almost identical layout but I have to change names to coincide with your tables.

First we must create a query.
Expand|Select|Wrap|Line Numbers
  1. SELECT Recipe.RecipeName, Recipe.RecipeID, Ccat(Recipe.RecipeID,[CategoryName]) AS InterestStg
  2. FROM CategoryTable INNER JOIN (Recipe INNER JOIN RecipeCategoryTable ON Recipe.RecipeID = RecipeCategoryTable.RecipeID) ON Recipe.RecipeID = RecipeCategoryTable.RecipeID
  3. GROUP BY Recipe.RecipeName, Recipe.RecipeID,Ccat(Recipe.RecipeID,[CategoryName])
  4. ORDER BY RecipeName;
  5.  
Now for the VBA. What we are doing here is to read all the categories that apply to each recipe, and concatenating them (adding them together) with a comma and space.

Expand|Select|Wrap|Line Numbers
  1. Function Ccat(ID As Long, StgToConcat As String)
  2.  
  3.     Dim MyDb As Database
  4.     Dim Rst As Recordset
  5.     Dim SQLStg As String
  6.     Dim OutStg As String
  7.  
  8.    SELECT Recipe.RecipeName, Recipe.RecipeID
  9.    FROM CategoryTable INNER JOIN (Recipe INNER JOIN
  10.    RecipeCategoryTable ON Recipe.RecipeID = 
  11.  
  12. RecipeCategoryTable.RecipeID) ON Recipe.RecipeID = RecipeCategoryTable.RecipeID
  13. GROUP BY Recipe.RecipeName, Recipe.RecipeID,Ccat(Recipe.RecipeID,[CategoryName])
  14. ORDER BY RecipeName;
  15.     Set MyDb = CurrentDb
  16.     Set Rst = MyDb.OpenRecordset(SQLStg)
  17.  
  18.     With Rst
  19.         Do Until .EOF
  20.             OutStg = OutStg & !Interest & ", "          ' Agg the strings with a comma & space
  21.             .MoveNext
  22.         Loop
  23.         .Close
  24.         Set Rst = Nothing
  25.         Ccat = Left(OutStg, Len(OutStg) - 2)            ' Remove last Comma space
  26.  
  27.     End With
  28.  
  29. End Function
  30.  
May 22 '16 #3
PhilOfWalton
1,430 Expert 1GB
Whoops, sorry, for some reason the reply was posted before I had finished it.
Please ignore the previous VBA, I will try again.
Expand|Select|Wrap|Line Numbers
  1. Function Ccat(ID As Long, StgToConcat As String)
  2.  
  3.     Dim MyDb As Database
  4.     Dim Rst As Recordset
  5.     Dim SQLStg As String
  6.     Dim OutStg As String
  7.  
  8.    SELECT Recipe.RecipeName, Recipe.RecipeID
  9.    FROM CategoryTable INNER JOIN (Recipe INNER JOIN
  10.    RecipeCategoryTable ON Recipe.RecipeID =  
  11.    RecipeCategoryTable.RecipeID) ON Recipe.RecipeID = 
  12.    RecipeCategoryTable.RecipeID
  13.    ORDER BY SortOrder;
  14.  
  15.     Set MyDb = CurrentDb
  16.     Set Rst = MyDb.OpenRecordset(SQLStg)
  17.  
  18.     With Rst
  19.         Do Until .EOF
  20.             OutStg = OutStg & !Category & ", "          ' Add the strings with a comma & space
  21.             .MoveNext
  22.         Loop
  23.         .Close
  24.         Set Rst = Nothing
  25.         Ccat = Left(OutStg, Len(OutStg) - 2)            ' Remove last Comma space
  26.  
  27.     End With
  28.  
  29. End Function
  30.  
Phil
May 22 '16 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Francisco | last post by:
I have this question: I have a simple search to a db, something like: "select description from games where year = '1990'" suppose I get 300 results, I would like to display this in pages of 30...
1
by: Charlie | last post by:
Hello, I have data in an Access table that I would like to export to multiple HTML tables. I would like to split the data in the Access table (about 92,000 records) into multiple HTML...
6
by: Jeremy | last post by:
I have a datasheet subform that is based off an ADO recordset. All is fine excpet the query results are displayed oddly. Rather than displaying the results as say 95.43, it displays it as...
2
by: Stef | last post by:
Hi people, I'd like to know if it's possible to get DataRows containing data from different tables that are part of one single dataset? Example: Dim ds As New DataSet("myDataSet") 'Load 3...
28
by: kiqyou_vf | last post by:
I'm trying to pull data from 2 different tables and do a loop to retrieve more than one row. I'm having problems with aligning the information. Can someone lead me in the right direction? I've done...
3
by: simon | last post by:
hello, i have a form where there are multiple dropdown lists that will all be populated from the same initial data query. i have a vb class defined to make the stored proc call and that returns a...
5
by: Brad Baker | last post by:
I'm completely new to ASP.NET programming, so please accept my apologies in advance for asking what is probably an obvious question. :-) I'm trying to write a page which will display the...
1
by: dympna | last post by:
Hi I have created a form which is derived form a query the query has a prompt box which prompts the user to input the criteria they wish to see. I have worked out how to put a command button into...
0
by: Del | last post by:
Hello and thanks for any and all assistance! I have a database that is used by several users on several different machines. The backend database is housed on a file server. Each user has a...
4
by: CoreyReynolds | last post by:
Hey all, This may be the dumbest thing I've ever seen. I'm creating a queryDef in VBA for access. Here's a sample of the query it creates: SELECT tblEquipment.equipmentID,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.